A keypad is the most widely used input devices of a microcontroller. In order to use it effectively, we need a basic understanding of them. In this section, we discuss keyboard fundamentals, along with key press and key detection mechanisms, Then we show how a keyboard is interfaced to an AT89S51 microcontroller.
At the lowest level, keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and column through ports; therefore, with a port of microcontroller, a 4X3 matrix of keys can be connected. When a key pressed, a row and column make a connection; otherwise, there is no connection between row and column.
Scanning and Identifying the Key Pressed
Figure1 shows a 4X3 matrix connected to port 1. The rows (R1 through R4) are connected to an output port and the columns (C1 through C3) are connected to an input port. Note that, we only use 7 pins of port 1.
To detect a pressed key, first, the microcontroller set pin 0 through 7 of port 1 and initiate the variable ‘Digit’ to zero. The value of ‘Digit’ represents the digit of key pressed.
Then it sends 0111 to R1 R2 R3 R4 and it reads the columns. If the data read from the columns is C1 C2 C3 =111, no key has been pressed and the process continues to next step.
If the data read from the columns is C1 C2 C3 =011, this means that a key in the R1 row and C1 column has been pressed. That is ‘1’.
In the subroutine ‘CheckColumn’, the value of ‘Digit’ is increased. The value of ‘Digit’ represents the key pressed. Before leave this subroutine, microcontroller set ‘KeyPressed’ to indicate there is a key pressed.
If the data read from the columns is C1 C2 C3 =101, this means that a key in the R1 row and C2 column has been pressed. That is ‘2’. Table 1 represents the meaning of each combination of data received at C1 C2 C3.
Someone can not press two key at the same time. There is time different between press a key with another key. ‘KeyPressed’ indicates there is a key pressed in this time. Before leave the entire subroutine, if ‘KeyPressed’ has been set, the microcontroller set the variable ‘KeyAlreadyPressed’. This variable will not be cleared until this subroutine detect that there is no key pressed. So, if press more than two key, only the first key will be read.
To see if any key is pressed, the columns are scanned over and over in an infinite loop until one of them has a 0 on it. The listing below is only for example. In fact, it’s no efficient if we scan the keypad without any intervals of time. We usually use the intervals time for scanning more than 10 ms. This interval can be generated by timer.
Listing Program.
No comments:
New comments are not allowed.