Thursday, March 31, 2016

8051 Keyboard Interfacing

Keyboards are organized in a matrix of rows and columns. When a key is pressed, a row and a column make a contact; otherwise, there is no connection between 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. The rows are connected to an output port and the columns are connected to an input port.

8051 Keyboard Interfacing

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 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.
ORG 0H
MOV P2, #0FFH ; Make P2 an input port
KEYS1 :MOV P1, #0 ; make p1 as output port
MOV A, P2 ; read all columns ensure all keys are open
ANL A, #00001111B ; mask unused bits
CJNE A, #00001111B, KEYS1 ; check till all keys are released
KEYS2 :ACALL DELAY ; call 20ms delay
MOV A, P2 ; see if any key is pressed
ANL A, #00001111B ; mask unused bits
CJNE A, #00001111B, OVER ; key pressed, await closure
SJMP KEYS2
OVER :ACALL DELAY ; wait 20ms debounce time
MOV A, P2 ; check key closure
ANL A, #00001111B ; mask unused bits
CJNE A, #00001111B, OVER1 ; key pressed find row
SJMP KEYS2 ; 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 the column
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 the column
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 the column
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 the column
LJMP KEYS2 ; if none, false input, repeat
ROW_0 :MOV DPTR, #KCODE0 ; set DPTR = start of row 0
SJMP FIND ; find column key belongs to
ROW_1 :MOV DPTR, #KCODE1 ; set DPTR = start of row 1
SJMP FIND ; find column key belongs to
ROW_2 :MOV DPTR, #KCODE2 ; set DPTR = start of row 2
SJMP FIND ; find column key belongs to
ROW_3 :MOV DPTR, #KCODE3 ; set DPTR = start of row 3
FIND :RRC A ; see if any CY bit is low
JNC MATCH ; if zero, get the ASCII code
INC DPTR ; point to next column address
SJMP FIND ; keep searching
MATCH :CLR A ; set A = 0, match is found
MOVC A, @A+DPTR ; get ASCII code from table
MOV P0, A ; display pressed key
LJMP KEYS1
; some delay
DELAY :MOV R3, #50
HERE2 :MOV R4, #255
HERE :DJNZ R4, HERE ; stay until R4 = 0
DJNZ R3, HERE2 ; stay until R3 = 0
RET
; ASCII look-up table for each row
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



Related topics:
8051 External Program Memory Interfacing   |   8051 External Data Memory Interfacing   |   8051 Memory Mapped IO   |   8051 LED Interfacing   |   8051 Switch Interfacing   |   8051 7-Segment Display Interfacing   |   8051 LCD Interfacing   |   8051 ADC Interfacing   |   8051 DAC Interfacing   |   8051 Relay Interfacing   |   8051 Sensor Interfacing   |   8051 Stepper Motor Interfacing   |   8051 DC Motor Interfacing

List of topics: 8051

No comments:

Post a Comment