LCD and Keyboard Interfacing
LCD and Keyboard Interfacing
[Source: “The 8051Microcontroller and Embedded Systems: Using Assembly and C” by Mohamed Ali Mazidi,
Janice Gillispie Mazidi, Rolin McKinlay]
Vss and VDD provide +5v and ground, V0 is used for controlling LCD contrast.
If RS=0, the instruction command register is selected, allowing the user to send a
command such as clear display, cursor at home, etc.
If RS=1 the data register is selected, allowing the user to send data to be displayed
on the LCD.
R/W input allows the user to Read/ Write the information to the LCD.
The enable pin is used by the LCD to latch information presented to its data pins.
The 8-bit data pins are used to send information to LCD.
Section 5.1 discusses about command codes for writing the instructions on the
LCD register. Section 5.2 gives an example program for displaying a character on
the LCD.
LCD COMMAND CODES
The LCD’s internal controller can accept several commands and modify the
display accordingly. These commands would be things like:
✓ Clear screen
✓ Return home
✓ Decrement/Increment cursor
After writing to the LCD, it takes some time for it to complete its internal operations.
During this time, it will not accept any new commands or data. Figure 5.4.1 shows the
command codes of LCD and Figure 5.4.2 shows the LCD interfacing. We need to insert
a time delay between any two commands or data sent to LCD.
Keys in a keyboard are arranged in a matrix of rows and columns. The controller
access both rows and columns through ports. Using two ports, we can connect to an 8x8
or a 4x4 matrix keyboard. When a key is pressed, a row and column make a contact,
otherwise there is no contact. We will look at the details using a 4x4 keyboard.
4X 4 KEYBOARD
Figure 5.4.31 shows a 4 x4 matrix connected to two ports.
The rows are connected to an output port(Port 1) and the columns are connected to
an input port. (Port 2)
If no key has been pressed, reading the input port will yield 1s 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.
ii) After the 20-ms delay, if the key is still pressed, then it goes to the loop (step 3) to
detect the actual key pressed.
3. To detect which row the key pressed 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 does not belong
to that row. Therefore, it grounds the next row and continues until it finds the row, that
the key pressed belongs to.
• Upon finding the row that the key pressed belongs to, it sets up the starting
address for the lookup table holding the scan codes for that row.
4. To identify the key pressed, 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.
Figure 5.4.4 provides the flowchart for keyboard interfacing Program for scanning and
identifying the pressed key.
Figure 5.4.4 Flowchart for Keyboard Interfacing
[Source: “The 8051Microcontroller and Embedded Systems: Using Assembly and C” by Mohamed Ali Mazidi,
Janice Gillispie Mazidi, Rolin McKinlay, pg.no.365]
PROGRAM:
;keyboard subroutine. This program sends the ASCII code for pressed key to P0.1
;P1.0-P1.3 connected to rows, P2.0-P2.3 to column
MOV P2,#0FFH ;make P2 an input port
K1: MOV P1,#0 ;ground all rows at once
MOV A,P2 ;read all col
;(ensure keys open)
ANL A,00001111B ;masked unused bits
CJNE A,#00001111B,K1 ;till all keys release
K2: ACALL DELAY ;call 20 msec delay
MOV A,P2 ;see if any key is pressed
ANL A,00001111B ;mask unused bits
CJNE A,#00001111B,OVER ;key pressed, find row
SJMP K2 ;check till key pressed
OVER: ACALL DELAY ;wait 20 msec debounce time
MOV A,P2 ;check key closure
ANL A,00001111B ;mask unused bits
CJNE A,#00001111B,OVER1 ;key pressed, find row
SJMP K2 ;if none, keep polling
OVER1: MOV P1, #11111110B ;ground row 0
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_0 ;key row 0, find col.
MOV P1,#11111101B ;ground row 1
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_1 ;key row 1, find col.
MOV P1,#11111011B ;ground row 2
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_2 ;key row 2, find col.
MOV P1,#11110111B ;ground row 3
MOV A,P2 ;read all columns
ANL A,#00001111B ;mask unused bits
CJNE A,#00001111B,ROW_3 ;key row 3, find col.
LJMP K2 ;if none, false input,
ROW_0: MOV DPTR,#KCODE0 ;set DPTR=start of row 0
SJMP FIND ;find col. Key belongs to
ROW_1: MOV DPTR,#KCODE1 ;set DPTR=start of row
SJMP FIND ;find col. Key belongs to
ROW_2: MOV DPTR,#KCODE2 ;set DPTR=start of row 2
SJMP FIND ;find col. Key belongs to
ROW_3: MOV DPTR,#KCODE3 ;set DPTR=start of row 3
FIND: RRC A ;see if any CY bit low
JNC MATCH ;if zero, get ASCII code
INC DPTR ;point to next col. addr
SJMP FIND ;keep searching
MATCH: CLR A ;set A=0 (match is found)
MOVC A,@A+DPTR ;get ASCII from table
MOV P0,A ;display pressed key
LJMP K1