15 Keyboard 15 03 2024
15 Keyboard 15 03 2024
Keyboard Interfacing
KEYBOARDS
❑ Keyboards are organized in a matrix of 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
➢ When a key is pressed, a row and a
column make a contact
▪ Otherwise, there is no connection
between rows and columns
❑ In IBM PC keyboards, a single
microcontroller takes care of hardware and
software interfacing
Scanning and Identifying the Key
❑ A 4x4 matrix connected to two ports
➢ The rows are connected to an output port and the columns are connected to an input port
Vcc
3 2 1 0
D0
7 6 5 4
D1 If no key has
been pressed,
B A 9 8
reading the input
If all the rows are D2 port will yield 1s
grounded and a key is for all columns
F E D C
pressed, one of the since they are all
columns will have 0 D3 connected to high
since the key pressed (Vcc)
provides the path to
ground Port 1
(Out)
D3 D2 D1 D0 Port 2
(In)
Example:
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 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.
Grounding Rows and Reading Columns
❑ 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
➢ 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
Steps for identification of key is pressed or not
• 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 (provided in stage 1), making them grounded
• After the key press detection, it waits 20 ms for the bounce and then
scans the columns again
Steps to identify row to which key belongs to
• It ensures that the first key press detection was not an erroneous one
due 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
• 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
Steps to identify which key is pressed
• 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
• 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
Flow Chart for identification of pressed key
1
2
Read all columns
Start Ground next row
no Any key
Ground all rows no
down? Key pressed
in this row?
yes
Read all columns yes
Wait for debounce
Find which key
All keys is pressed
open? Read all columns
2
Keyboard Program for identification of pressed key
ORG 0H
MOV P2, #0FFH MOV P1, #0BH
CLR C MOV R0, #8H
MOV A, P2
CHECK: MOV P1, #00H ANL A, #0FH
MOV A, P2 CJNE A, #0FH, ROW_FOUND
ANL A, #0FH
CJNE A, #0FH, PRESSED MOV P1, #07H
SJMP CHECK MOV R0, #0CH
MOV A, P2
PRESSED: MOV P1, #0EH
MOV R0, #0H ROW_FOUND: RRC A
MOV A, P2 JNC COLUMN_FOUND
ANL A, #0FH INC R0
CJNE A, #0FH, ROW_FOUND SJMP ROW_FOUND