Professional Documents
Culture Documents
Lecture Notes-25
Keyboard Interfacing
Once you add a keyboard for your system, you allow the user to input information to the
microcontroller in real time. In this tutorial, you will learn how to connect a keyboard to
your microcontroller and the basic principle to decode keyboard input.
In general, keyboards are organized as a matrix of rows and columns; two side of this matrix are
connected to Vcc through resistors while the third side is connected to the microcontroller port
and configured as an output; and the last side is connected to the microcontroller port and
configured as an input as shown in fig. 1.
Microcontroller keep scanning the keyboard, when all inputs are high ("ones") that mean no key
is pressed; if one bit is low ("zero") that mean there is a pressed key. System designer setup
a Look-Up Table contain the ASCII code for each key, in this project we will use 16 keys to
represent the hex number from 0 to F arranged according to keyboard arrangement.
To detect which key is pressed; microcontroller ground all rows, then reads all columns, if all the
columns=1's no key is pressed, if one columns=0 it's indicate that a key is pressed. To identify
the exact pressed key, microcontroller will starting with the top row by grounding it; then read
the columns. If the data read is all ones, no key that row activated, and the process will move to
G.Vijaya Santhi,ECE,GMRIT
Embedded Systems Unit-5
Lecture Notes-25
the next row, until reach the row that has a pressed key. At this stage, microcontroller knows the
row that has a pressed key, and can setup the starting address in the look-up table for that row.
The last step is finding the column that has a pressed key by rotating the columns bits; one bit at
a time to locate a low bit, the most efficient way is rotating column bits through the carry flag by
using RRC instruction. When the 0 bit found, microcontroller pulls the corresponding code from
the look-up table.
To be sure that no key is still down from the previous session, microcontroller send 0's to all
rows at on time and check the columns, if all columns are high then the normal scanning start,
otherwise, it will wait until all columns become high.
At the left, you will find the assembly code for scanning 4x4 keyboard, assuming that the input
data will be a hex number form 0 to F, ports P1.0 to P1.3 of the 8051 microcontroller connected
to rows, P2.0 to P2.3 connected to columns. The pressed key ASCII code will be stored in A. At
the right is the flowchart that describes the code.
Tip:
Flow Chart:
G.Vijaya Santhi,ECE,GMRIT
Embedded Systems Unit-5
Lecture Notes-25
G.Vijaya Santhi,ECE,GMRIT
Embedded Systems Unit-5
Lecture Notes-25
Program:
G.Vijaya Santhi,ECE,GMRIT
Embedded Systems Unit-5
Lecture Notes-25
ROW_1:
MOV DPTR, #KCODE1 ;SET DPTR=START OR ROW 1
SJMP FIND FIND COLUMN BELONGS TO
ROW_2:
MOV DPTR, #KCODE2 ;SET DPTR=START OR ROW 2
SJMP FIND FIND COLUMN BELONGS TO
ROW_3:
MOV DPTR, #KCODE3 ;SET DPTR=START OR ROW 3
FIND:
RRC A SEE IF ANY CY BIT LOW
JNC MATCH IF ZERO GET ASCII CODE
INC DPTR POINT TO NEXT COLUMN
SJMP FIND KEEP SEARCHING
MATCH:
CLR A
MOVC A,@A+DPTR GET ASCII CODE FROM LOOK-UP
LJMP K1 LOOP
Delay:
MOV R4,#40
repeat:
MOV R5,#230
repeatt:
DJNZ R5,repeatt
DJNZ R4,repeat
RET
;LOOK-UP TABLE
ORG 300H
KCODE0:
DB '0' , '1' , '2' , '3' ROW 0
KCODE1:
DB '4' , '5' , '6' , '7' ROW 1
KCODE2:
DB '8' , '9' , 'A' , 'B' ROW 2
KCODE3:
DB 'C' , 'D' , 'E' , 'F' ROW 3
END
G.Vijaya Santhi,ECE,GMRIT