CLR P2.2: Microcontroller & Embedded Prog. (MU-Sem 5-IT) 5-16 8051 Interfacing and Applications
CLR P2.2: Microcontroller & Embedded Prog. (MU-Sem 5-IT) 5-16 8051 Interfacing and Applications
Q. Draw interfacing of keyboard matrix with 8051 in detail with diagram. Write a
! Working of a Key
For a small keypad, like a car remote key, we need only 2-3 keys. We could simply connect them
on each line of a port.
But for a large keyboard like a TV Remote or the computer’s keyboard, the number of keys is
large. We cannot afford one port pin each for every key.
So we connect the keys in a matrix form. This gives us more keys using less lines. By identifying
the row and the column we can figure out which key is pressed.
We use for lines of a different port, say P2.0… P2.3 as columns. Rows will be Output whereas
Columns will be Input.
The columns will have a default value of 1 due to the Vcc with a pull up resister. This gives us 16
intersecting points, on each we plant a key.
When the key is “Pressed”, the corresponding row and column will come into contact.
! Program Logic
For this we keep sending 0s on all rows and read the columns.
If columns are still all 1’s it means no key is pressed. Repeat this process. Else move to Row
Check. Once we are sure that a key is pressed, we check each row individually.
This is done by sending a 0 on every row and checking the columns till the 0 comes on the
columns. Once the row is identified we check the column by rotating the column data till a 0 is
found.
! Key De-bounce
Practically every key uses a spring mechanism and hence will bounce.
This means when a key is pressed there are microscopic vibrations which fluctuate the key value
between 0 and 1 till it finally settles to a stable 0. Reading the key during this bouncing period may
give you a wrong result. Avoid this bouncing period by calling a delay before performing row and
column checking. This allows the key to stabilize in the “pressed” state of logic 0. The delay is
typically 10-20 milliseconds. This is called key de-bouncing.
Hence write a program to identify the key pressed and display the key on a 7 segment display.
! Interface
! Algorithm
Table DB C0H, F9H, A4H, B0H, 99H, 92H, 82H, F8H, 80H, 90H
MOV DPTR, #0400H ; Initialise DPTR as a pointer to starting of the
; look up table = 0400H
MOV P2, #0FFH ; Initialise P2 as an input port for the columns
Check : MOV P1, #00H ; Send a 0 on all rows
MOV A, P2 ; Read the columns
CJNE A, #0FH, Pressed ; If A != 0FH that means a key is pressed
SJMP Check ; Else repeat this process
Pressed: ACALL Delay ; 10 millisecond Delay to let the bouncing settle down
MOV R0, #00H ; Smallest key of 1st row
MOV P1, #0EH ; Send 0 only on 1st row first row (0000 1110)
MOV A, P2 ; Read the columns
CJNE A, #0FH, Colcheck ; If A != 0FH that means row is identified,
; move to column check
MOV R0, #04H ; Smallest key of 2nd row
MOV P1, #0DH ; Send 0 only on 2nd row first row (0000 1101)
MOV A, P2 ; Read the columns
Q. Draw and explain the functional block diagram of 8255 Programmable Peripheral