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

CLR P2.2: Microcontroller & Embedded Prog. (MU-Sem 5-IT) 5-16 8051 Interfacing and Applications

The document discusses interfacing a 4x4 matrix keyboard with an 8051 microcontroller. It explains the working of a matrix keyboard and describes using the rows and columns of the keyboard as inputs and outputs to the microcontroller ports. The document provides a program logic and algorithm to detect which key is pressed and display the corresponding number on a 7-segment display. It also includes a sample program code and interface diagram.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

CLR P2.2: Microcontroller & Embedded Prog. (MU-Sem 5-IT) 5-16 8051 Interfacing and Applications

The document discusses interfacing a 4x4 matrix keyboard with an 8051 microcontroller. It explains the working of a matrix keyboard and describes using the rows and columns of the keyboard as inputs and outputs to the microcontroller ports. The document provides a program logic and algorithm to detect which key is pressed and display the corresponding number on a 7-segment display. It also includes a sample program code and interface diagram.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Microcontroller & Embedded Prog.

(MU-Sem 5-IT) 5-16 8051 Interfacing and Applications

CLR P2.2 ; Make Latch Enable = 0 (This produces a Falling Edge)


RET
Command : MOV P1, A ; Put Command value in P1 from A Register
CLR P2.0 ; Make Register Select = 0 for Command
CLR P2.1 ; Make Read/Write Select = 0 for Write
SETB P2.2 ; Make Latch Enable = 1
CLR P2.2 ; Make Latch Enable = 0 (This produces a Falling Edge)
ACALL Delay ; Call a delay of 10 milliseconds
RET
Data: MOV P1, A ; Put Data value (ASCII Code) in P1 from A Register
SETB P2.0 ; Make Register Select = 1 for Data
CLR P2.1 ; Make Read/Write Select = 0 for Write
SETB P2.2 ; Make Latch Enable = 1
CLR P2.2 ; Make Latch Enable = 0 (This produces a Falling Edge)
ACALL Delay ; Call a delay of 10 milliseconds
RET

! 5.5 8051 | 4x4 Matrix Keyboard Interfacing

Q. Draw interfacing of keyboard matrix with 8051 in detail with diagram. Write a

program to generate Hexadecimal values. (Q. 5(B), Dec. 19, 10 Marks)

! Working of a Key

A keyboard is the most basic input device.


When a Key is connected to a port pin, the port pin must be configured as an input pin. We read the
value of the pin to establish of a key is pressed.
Most circuits use an active low logic for a key.
The Keys are connected to Vcc via a pull up resister. The default value on the keys is a 1. When a
Key is pressed, it gets connected to GND making its value 0.

! Concept of a matrix Keyboard

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.

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontroller & Embedded Prog. (MU-Sem 5-IT) 5-17 8051 Interfacing and Applications

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.

! 4x4 Matrix Keyboard

We use four lines of a port, say P1.0… P1.3 as rows.

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

First off, we need to know IF A KEY IS PRESSED?

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.

This finally identifies the key.

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

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontroller & Embedded Prog. (MU-Sem 5-IT) 5-18 8051 Interfacing and Applications

! Interface

Fig. 5.5.1 : Matrix keyboard interface with 8051

! Algorithm

Initialise look up table for 7 segment codes at 0400H.


Initialise DPTR to point to look up table.
Initialise Port 2 as input port (for columns).
To know if A KEY IS PRESSED, send all 0 on P1 (rows) and read P2 (columns). If P2 = 0FH then
repeat else break for Row Check.
Call delay for de-bouncing and start Row Check
Send 0 on each row and keep R2 as the smallest key of the row.
Read the column. Break for column check if value not equal to 0FH. Else next row.
In column check, rotate right the column value.
If no carry then R2 is the key else increment R2 and repeat.
Once key is identified, get its 7 segment code from look up table, display the key and repeat the
whole program for the next key.

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture


Microcontroller & Embedded Prog. (MU-Sem 5-IT) 5-19 8051 Interfacing and Applications

Program : ORG 0400H

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

CJNE A, #0FH, Colcheck ; If A != 0FH that means row is identified,


; move to column check
MOV R0, #08H ; Smallest key of 3rd row
MOV P1, #0BH ; Send 0 only on 3rd row first row (0000 1011)
MOV A, P2 ; Read the columns
CJNE A, #0FH, Colcheck ; If A != 0FH that means row is identified,
; move to column check
MOV R0, #0CH ; Smallest key of 4th row
MOV P1, #07H ; Send 0 only on 4th row first row (0000 0111)
MOV A, P2 ; Read the columns
; No need to check, obviously 4th row is identified,
; move to column check
Colcheck : RR A ; rotate right the column value in A register and
; check the carry flag
JNC Display ; If CF = 0, R2 is the Key Pressed, go and display it
INC R2 ; Else increment R2
Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture
Microcontroller & Embedded Prog. (MU-Sem 5-IT) 5-20 8051 Interfacing and Applications

SJMP Colcheck ; Repeat. No need for a count. Loop will break in


; 4 iterations for sure.
Display : MOV A, R2 ; A ç Index from R2 (The Key that was Pressed)
MOVC A, @A+DPTR ; A ç 7 segment code from Look Up Table
MOV P3, A ; Call the “Data” function with the ASCII value in A register
ACALL Delay ; Optional. It is the “repeat delay” of a key board,
; typically 50 millisecond
SJMP Check ; Go back and check for the next key

! 5.6 8255 | Internal Block Diagram

Q. Draw and explain the functional block diagram of 8255 Programmable Peripheral

Interface. (Q. 3(a), May 19, 10 Marks)

(1C1)Fig. 5.6.1 : Architecture of 8255

Tech-Neo Publications...........Where Authors inspire innovation .....A SACHIN SHAH Venture

You might also like