KEYBOARD INTERFACING

SECTION 12.2: KEYBOARD INTERFACING
Keyboards and LCDs are the most widely used input/output devices of the 8051, and a basic understanding of them is essential. In this section, we first discuss keyboard fundamentals, along with key press and key detection mechanisms. Then we show how a keyboard is interfaced to an 8051.
Interfacing the keyboard to the 8051
At the lowest level, keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and columns through ports; therefore, with two 8-bit ports, an 8 x 8 matrix of keys can be connected to a microprocessor. When a key is pressed, a row and a column make a contact; otherwise, there is no connection between rows and columns. In IBM PC keyboards, a single microcontroller (consisting of a microprocessor, RAM and EPROM, and several ports all on a single chip) takes care of hardware and software interfacing of the keyboard. In such systems, it is the function of programs stored in the EPROM of the microcontroller to scan the keys continuously, identify which one has been activated, and present it to the motherboard. In this section we look at the mechanism by which the 8051 scans and identifies the key.
Scanning and identifying the key

Figure 12-6 shows a 4 x 4 matrix connected to two ports. The rows are connected to an output port and the columns are connected to an input port. If no key has been pressed, reading the input port will yield 1 s for all columns since they are all connected to high (Vcc). If all the rows are grounded and a key is pressed, one of the columns will have 0 since the key pressed provides the path to ground. It is the function of the microcontroller to scan the keyboard continuously to detect and identify the key pressed. How it is done is explained next.




Figure 12-6. Matrix Keyboard Connection to Ports
Grounding rows and reading the columns
To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output latch, then it reads the columns. If the data read from the columns is D3 – DO = 1111, no key has been pressed and the process continues until a key press is detected. However, if one of the column bits has a zero, this means that a key press has occurred. For example, if D3 – DO = 1101, this means that a key in the Dl column has been pressed. After a key press is detected, the microcontroller will go through the process of identifying the key. Starting with the top row, the microcontroller grounds it by providing a low to row DO only; then it reads the columns. If the data read is all Is, no key in that row is activated and the process is moved to the next row. It grounds the next row, reads the columns, and checks for any zero. This process continues until the row is identified. After identification
Example 12-3
From Figure 12-6, identify the row and column of the pressed key for each of the following.
  1. D3 – DO = 1110 for the row, D3 – DO = 1011 for the column
  2. D3 – DO = 1101 for the row, D3 – DO = 0111 for the column
    Solution:
From Figure 12-6 the row and column can be used to identify the key.
  1. The row belongs to DO and the column belongs to D2; therefore, key number 2 was
    pressed.
  2. The row belongs to Dl and the column belongs to D3; therefore, key number 7 was
    pressed.








Figure 12-7. Flowchart for Program 12-4


of the row in which the key has been pressed, the next task is to find out which column the pressed key belongs to. This should be easy since the microcontroller knows at any time which row and column are being accessed. Look at Example 12-3.
Program 12-4 is the 8051 Assembly language program for detection and identification of key activation. In this program, it is assumed that PI and P2 are initialized as output and input, respectively. Program 12-4 goes through the following four major stages:
  1. To make sure that the preceding key has been released, Os are output to all rows
    at once, and the columns are read and checked repeatedly until all the columns
    are high. When all columns are found to be high, the program waits for a short
    amount of time before it goes to the next stage of waiting for a key to be
    pressed.
  2. To see if any key is pressed, the columns are scanned over and over in an infi
    nite loop until one of them has a 0 on it. Remember that the output latches con
    nected to rows still have their initial zeros (provided in stage 1), making them
    grounded. After the key press detection, the microcontroller waits 20 ms for
    the bounce and then scans the columns again. This serves two functions: (a) it
    ensures that the first key press detection was not an erroneous one due to a
    spike noise, and (b) the 20-ms delay prevents the same key press from being
    interpreted as a multiple key press. If after the 20Tins delay the key is still
    pressed, it goes to the next stage to detect which row it belongs to; otherwise,
    it goes back into the loop to detect a real key press.
  3. To detect which row the key press belongs to, the microcontroller grounds one
    row at a time, reading the columns each time. If it finds that all columns are
    high, this means that the key press cannot belong to that row; therefore, it
    grounds the next row and continues until it finds the row the key press belongs
    to. Upon finding the row that the key press belongs to, it sets up the starting
    address for the look-up table holding the scan codes (or the ASCII value) for
    that row and goes to the next stage to identify the key.
  4. To identify the key press, the microcontroller rotates the column bits, one bit
    at a time, into the carry flag and checks to see if it is low. Upon finding the
    zero, it pulls out the ASCII code for that key from the look-up table; otherwise,
    it increments the pointer to point to the next element of the look-up table.
    Figure 12-7 flowcharts this process.
While the key press detection is standard for all keyboards, the process for determining which key is pressed varies. The look-up table method shown in Program 12-4 can be modified to work with any matrix up to 8×8. Figure 12-7 provides the flowchart for Program 12-4 for scanning and identifying the pressed key.
There are 1C chips such as National Semiconductor’s MM74C923 that incorporate keyboard scanning and decoding all in one chip. Such chips use combinations of counters and logic gates (no microcontroller) to implement the underlying concepts presented in Program 12-4. Example 12-4 shows keypad programming in 8051 C.




K2


Program 12-4: Keyboard Program








Program 12-4. (continued)


Example 12-4
Write a C program to read the keypad and send the result to the first serial port.
P1.0-P1.3 connected to rows
P2.0-P1.3 connected to columns
Configure the serial port for 9600 baud, 8-bit, and 1 stop bit.


Example 12-4 Cont.




SUMMARY
This chapter showed how to interface real-world devices such as LCDs and keypads to the 8051. First, we described the operation modes of LCDs, then described how to program the LCD by sending data or commands to it via its interface to the 8051.
Keyboards are one of the most widely used input devices for 8051 projects. This chapter also described the operation of keyboards, including key press and detection mechanisms. Then the 8051 was shown interfacing with a keyboard. 8051 programs were written to return the ASCII code for the pressed key.

Next post:

Previous post: