2. Interfacing of Keyboard with 8051
2. Interfacing of Keyboard with 8051
A 4x4 matrix connected to two ports .The rows are connected to an output port and the columns are
connected to an input port.
• It is the function of the microcontroller to scan the keyboard continuously to detect and identify the
key pressed
• 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 columns is D3 – D0 = 1111, no key has been pressed and
the process continues till key press is detected.
• If one of the column bits has a zero, this means that a key press has occurred, For example, if D3 – D0 =
1101, this means that a key in the D1 column has been pressed. After detecting a key press,
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 D0 only.
EE8551-MPMC Page 2
• It reads the columns, if the data read is all 1s, 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 of the row in which the key has been pressed
• Find out which column the pressed key belongs to.
Program for detection and identification of key activation goes through the following stages:
1. To make sure that the preceding key has been released, 0s 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. 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.
• Remember that the output latches connected to rows still have their initial zeros making them
grounded.
2. After the key press detection, it waits 20 ms for the bounce and then scans the columns again.
• It ensures that the first key press detection was not an erroneous one due to a spike noise.
• If after the 20-ms delay the key is still pressed, it goes back into the loop to detect a real key
press
3. To detect which row key press belongs to,it 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 ASCII) for that row.
4. To identify the key press, it 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
EE8551-MPMC Page 3
KEYBOARD INTERFACING WITH 8051:
5. Call debounce.
6. Is key really pressed? (Check at least one of the return lines shows „0‟ level). No Step 4 , Yes step 7.
7. Find key code and display the key pressed on 7-segment display.
8. Go to step 1.
PROGRAM:
From the above figure identify the row and column of the pressed key for each of the following.
(a) D3 – D0 = 1110 for the row, D3 – D0 = 1011 for the column
(b) D3 – D0 = 1101 for the row, D3 – D0 = 0111 for the column Solution:
From the above figure, the row and column can be used to identify the key.
(a) The row belongs to D0 and the column belongs to D2; therefore, key number 2 was pressed.
(b) The row belongs to D1 and the column belongs to D3; therefore, key number 7 was pressed.
; Keyboard subroutine.
; This program sends the ASCII code for pressed key to P0.1.
EE8551-MPMC Page 4
; P1.0 – P1.3 connected to rows P2.0 – P2.3 connected to columns.
EE8551-MPMC Page 5
SJMP FIND
MOV DPTR,
#KCODE 1 SJMP FIND
MOV DPTR,
#KCODE 2 SJMP FIND
MOV DPTR,
#KCODE 3 RRC A
JNC MATCH
INC DPTR
MATCH: SJMP FIND
CLR A
MOVC A, @A +
DPTR MOV P0, A
LJMP K1
2. Check if all the keys are released by writing „0‟ to P1.4-P1.7 and check if all return lines are in
state ‘1’. If not then wait.
3. Call debounce.
4. Wait for key closure. Ground all scan lines by writing „0‟ and then check if at least one of return
lines shows „0‟ level.
5. Call debounce.
EE8551-MPMC Page 6