
Chapter 8. Advanced Experiments
8.1. Experiment with Matrix Key
The 4*4 matrix key, also called key array, consists of 4 I/O rows and 4 I/O columns. Every key cross over a junction of row and column. In this way, I/O port can be used effectively.
Principle of Matrix Key
The most common matrix key is like the below picture. Composed by 16 keys, 1 I/O port can be used to implement the matrix.

The circuit of matrix key.

When no key is depressed, P1.0 ~ P1.3 and P1.4 ~ P1.7 are open-circuit. When any key is depressed, those I/O lines which are connected with the key will be short-cut.
Method to recognize the status of keys:
Step 1, set column P1.4 ~ P1.7 to be input, and set row P1.0 ~ P1.3 to be low level. Then read the status of column P1.4 ~ P1.7, if there is one column low level, there must be a key depressed. Now move on to Step 2 to locate the key.
Step 2, set row P1.0 ~ P1.3 to be low level in turn, and read from column P1.4 ~ P1.7. If a column is low level, the depressed key is then located.
By the result of 2 steps, the depressed key is found. Remember, one pressing of the key only causes one action which calls a key processing function, therefore, key processing function must be invoked after the key is released.
The example illustrates how to program the matrix key in the K8MDP. When 1 key is pressed, the key number will be displayed on the LED display, from 0 to F.


Below is the schematic diagram.

Row P1.0 ~ P1.3 are output lines. Column P1.4 ~ P1.7 are input lines. At the beginning, row P1.0 ~ P1.3 output low level. Then read the column P1.4 ~ P1.7. If they are all high level, there must be no key depressed. If one of the columns is low level, a certain period of time delay is needed to get rid of the impact from key pressing noise. After the delay, if that column is still low level. Then it is certain that there is one key depressed.
To know which key is depressed, we can scan the whole matrix. Set row P1.0 ~ P1.3 to be 0111, 1011, 1101, 1110 in turn, at the same time, read the column P1.4 ~ P1.7. If the value read from P1.4 ~ P1.7 is the 1111, then there is no key depressed, otherwise, the value is the key number.
Now let’s take the S5 key for example. When we pressed S5, and scan row P1.0 ~ P1.3, we can read 1011 from P1.4 ~ P1.7. 1011 is 0x0B in octal. So, jumper to S5 key processing subroutine to react the S5 key action.
|