0% found this document useful (0 votes)
9 views

2. Interfacing of Keyboard with 8051

The document explains the keyboard and display interface, detailing how keyboards are organized in a matrix and how a microcontroller scans for key presses. It outlines the process for detecting and identifying key activations, including debouncing and checking rows and columns to determine which key was pressed. Additionally, it provides an algorithm for interfacing a keyboard with an 8051 microcontroller, including initialization steps and a program for detecting key presses and displaying the corresponding ASCII code.

Uploaded by

Bhishan Wadhai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

2. Interfacing of Keyboard with 8051

The document explains the keyboard and display interface, detailing how keyboards are organized in a matrix and how a microcontroller scans for key presses. It outlines the process for detecting and identifying key activations, including debouncing and checking rows and columns to determine which key was pressed. Additionally, it provides an algorithm for interfacing a keyboard with an 8051 microcontroller, including initialization steps and a program for detecting key presses and displaying the corresponding ASCII code.

Uploaded by

Bhishan Wadhai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

2.

KEY BOARD AND DISPLAY INTERFACE

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.

A 4x4 matrix connected to two ports .The rows are connected to an output port and the columns are
connected to an input port.

• 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.

EE8551-MPMC Page 2
• 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.

Program for detection and identification of key activation goes through the following stages:
1. 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 making them
grounded.
2. After the key press detection, it waits 20 ms for the bounce and then scans the columns again.
• It ensures that the first key press detection was not an erroneous one due to 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
3. 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.
• 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.
4. 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

EE8551-MPMC Page 3
KEYBOARD INTERFACING WITH 8051:

The steps in algorithm are as follows:


1. Initialize P1.0, P1.1, P1.2 and P1.3 as inputs.
2. Check if all the keys are released by writing „0‟ to P1.4-P1.7 and check if all return lines are in state “1”. If
not then wait.
3. Call debounce.
4. Wait for key closure. Ground all scan lines by writing „0‟ and then check if at least one of return lines shows
„0‟ level.

5. Call debounce.

6. Is key really pressed? (Check at least one of the return lines shows „0‟ level). No Step 4 , Yes step 7.
7. Find key code and display the key pressed on 7-segment display.

8. Go to step 1.

PROGRAM:
From the above figure 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 the above 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.
; Keyboard subroutine.
; This program sends the ASCII code for pressed key to P0.1.

EE8551-MPMC Page 4
; P1.0 – P1.3 connected to rows P2.0 – P2.3 connected to columns.

LOOK-UP TABLE FOR EACH ASCII


ROW ORG 300H
KCODE 0: DE ‘0’, ‘1’, ‘2’, ‘3’ ; Row 0
KCODE 1: DE ‘4’, ‘5’, ‘6’, ‘7’ ; Row 1
KCODE 2: DE ‘8’, ‘9’, ‘A’, ‘B’ ; Row 2
KCODE 3: DE ‘C’, ‘D’, ‘E’, ‘F’ ; Row 3

MOV P2, #0FFH ; make P2 an input port


K1: MOV P1, #0 ; ground all rows at once
MCV A, P2 ; read all column ensure all keys open.
ANL A, #00001111B ; masked unused bits
CJNE A, #00001111B, K1 ; check till all keys released
K2: ACALL DELAY ; call 20 ms delay
MCV A, P2 ; see if any key is pressed
ANL A, #00001111B ; mask unused bits
CJNE A, #00001111B, OVER ; key pressed, await closure
SJMP K2 ; check if key pressed
OVER: ACALL DELAY ; wait 20 ms debounce time
MCV A, P2 ; check key closure
ANL A, #00001111B ;
CJNE A, #00001111B, OVER1 ;
SJMP K2 ;
OVER1: MOV P1, #11111110B ;
MOV A, P2 ;
ANL A, #00001111B ;

CJNE A, #00001111B, ROW_0


MOV P1, #11111101B
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW_1
MOV P1, #11111011B
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW_2
MOV P1, #11110111B
MOV A, P2
ANL A, #00001111B
CJNE A, #00001111B, ROW_3
LJMP F2
MOV DPTR, #KCODE 0

EE8551-MPMC Page 5
SJMP FIND
MOV DPTR,
#KCODE 1 SJMP FIND
MOV DPTR,
#KCODE 2 SJMP FIND
MOV DPTR,
#KCODE 3 RRC A
JNC MATCH
INC DPTR
MATCH: SJMP FIND
CLR A
MOVC A, @A +
DPTR MOV P0, A
LJMP K1

The steps in algorithm are as follows:


1. Initialize P1.0, P1.1, P1.2 and P1.3 as inputs.

2. Check if all the keys are released by writing „0‟ to P1.4-P1.7 and check if all return lines are in
state ‘1’. If not then wait.

3. Call debounce.

4. Wait for key closure. Ground all scan lines by writing „0‟ and then check if at least one of return
lines shows „0‟ level.
5. Call debounce.

EE8551-MPMC Page 6

You might also like