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

5.2 Keyboard and display interface

The document discusses interfacing a keyboard and a seven-segment LED display with the 8051 microcontroller. It explains the matrix keyboard setup, key detection algorithm, and the assembly language program for key press detection. Additionally, it covers the seven-segment display configurations, interfacing details, and a program for displaying a count from 0 to 9.

Uploaded by

yasoraji23
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)
12 views

5.2 Keyboard and display interface

The document discusses interfacing a keyboard and a seven-segment LED display with the 8051 microcontroller. It explains the matrix keyboard setup, key detection algorithm, and the assembly language program for key press detection. Additionally, it covers the seven-segment display configurations, interfacing details, and a program for displaying a count from 0 to 9.

Uploaded by

yasoraji23
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/ 12

5.2.

1 Interfacing Keyboard with 8051 controller:

The key board are arranged with switches that forms a matrix keyboard. Keys or switches
in key board is designed with a particular rows and columns. These rows and columns are
connected to the microcontroller through its ports of the micro controller 8051. Normally
8*8 matrix key board or 4 x4 matrix keyboard are used . So only two ports of 8051 can be
easily connected to the rows and columns of the key board. Hex key pad is essentially a
collection of 16 keys arranged in the form of a 4×4 matrix. Hex key pad usually have keys
representing numeric 0 to 9 and characters A to F. The simplified diagram of a typical hex
key pad is shown in the figure below

The hex keypad has 8 communication lines namely R0, R1, R2, R3, C0, C1, C2 and C3. R0
to R3 represents the four rows and C0 to C3 represents the four columns. When a particular
key is pressed the corresponding row and column to which the terminals of the key are
connected gets shorted.

For example if key 1 is pressed row R0 and column C0 gets shorted and so on. The program
identifies which key is pressed by a method known as column scanning. In this method a
particular row is kept low (other rows are kept high) and the columns are checked for low. If
a particular column is found low then that means that the key connected between that column
and the corresponding row (the row that is kept low) is been pressed. For example if row R0
is initially kept low and column C0 is found low during scanning, that means key 1 is
pressed.
Algorithm to detect a key press using a keypad

To detect the key that’s pressed, two ports of the microcontroller are connected to the rows
and columns of the matrix respectively. The port which is connected to the rows of the matrix
is configured as an output port, hence making each row logic 0. On the contrary, the port
which is connected to the columns is configured as an input port, making the column at logic
1.

When a button is pressed, it changes the logic of that particular column to 0 because the
button causes a short circuit between the row and the column. As an example, let us say the
key with the number 1 is pressed. This causes the D1 column to go to logic 0 as it shorts row
D0 with it.
Referring the picture above, that the same output would be produced if any of the keys in the
column D1 were pressed. To find the row which has the pressed key, the microcontroller
grounds the rows one by one and checks in which case the column is logic zero. This helps
the microcontroller to find which key was pressed. Flow chart for key press detection is
shown below.
To summarize, when a key is pressed, the column containing the key is driven to logic 0. The
microcontroller then checks each row and identifies the row where the button is pressed.

8051 Assembly language program for detection and identification of key activation. In this
program, it is assumed that PI and P2 are initialized as output and input, respectively.
Program goes through the following four major stages:

1. To make sure that the preceding key has been released, 0’s 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.
2. 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, the microcontroller waits 20 ms for the bounce and then scans the columns again.
This serves two functions: (a) it ensures that the first key press detection was not an
erroneous one due to a
spike noise, and (b) the 20-ms delay prevents the same key press from being interpreted as a
multiple key press. If after the 20Tins delay the key is still pressed, it goes to the next stage
to detect which row it belongs to; otherwise, it goes back into the loop to detect a real key
press.
3. To detect which row the key press belongs to, the microcontroller 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 the
ASCII value) for
that row and goes to the next stage to identify the key.
4. To identify the key press, the microcontroller 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. Figure flowcharts this process.
5.2.2 -8051 interfacing with 7 segmented LED display:

A seven segment display module is an electronic device used to display digital numbers and
it is made up of seven LED segments. Because of the small size of the LEDs, it is really easy
for a number of them to be connected together to make a unit like seven segment display. In
the seven segment display module, seven LED s are arranged in a rectangle. Sometimes, an
additional LED is seen in a seven segment display unit which is meant for displaying a
decimal point. ​Each LED segment has one of its pins brought out of the rectangular package.
Other pins are connected together to a common terminal. Seven segment displays can only
display 0 to 9 numbers. These seven LEDs indicate seven segments of the numbers and a dot
point as shown below.

Types of Seven segment displays:​The seven segment displays come up with two different
configurations. They are the common anode and a common cathode.

Common Cathode 7-segment display:​ As the name indicates, its cathode is connected to a
common terminal. Below is the schematic diagram to indicate its common cathode structure.
It should be connected to the ground while operating the display. If a high voltage is given to
the anode, then it will turn on the corresponding segment as shown below.
Common Anode 7-segment display: In this type, the anode is common. It should be
connected to a high voltage (to the supply through a resistor to limit current). In order to turn
on a particular segment, a ground level voltage is given to the corresponding pin. Since logic
circuits can sink more current than they can source, common anode connection is used most
widely as shown below.

Display codes

Display codes are the voltages to be applied to the segments to display a number. It is in the
order of segments abcdefg(dp), total 8 bits. For example. If number 0 has to be displayed,
then the segments A through F are turned on. In order to turn on the segments, in common
cathode mode, the anode terminals are subjected to a high voltage while in common anode
mode, the cathode terminals are given a low voltage.
Interfacing with 8051 Microcontroller:

The following table represent the function of each port pin in terms of segments of LED
display.
Segment Data port connection

a P1.0

b P1.1

c P1.2

d P1.3

e P1.4

f P1.5

g P1.6

Dp or h P1.7

To avoid drawing high current from controller port, we can use a transistor driver in each of
the segment lines. We can use either PNP or NPN transistors. In the case of PNP, the display
codes should be inverted because PNP transistors turn ON by applying a LOW signal (i.e.,
for common cathode, use common anode’s display codes). The transistor acts as a high
current switch when voltage and current levels are in the correct range with the switch being
controlled by the lower current digital logic signal. In most cases, the transistor used is a BJT,
especially in the case of low voltage circuits.
Here the segment by giving a high in transistor base. In the case of a PNP transistor, it
provides a low voltage for turning on.

Interfacing Diagram :
The circuit diagram shown above is of an 8051 microcontroller based 0 to 9 counter which
has a 7 segment LED display interfaced to it in order to display the count. This simple circuit
illustrates two things. How to setup simple 0 to 9 up counter using 8051 and more
importantly how to interface a seven segment LED display to 8051 in order to display a
particular result. The common cathode seven segment display D1 is connected to the Port 1
of the microcontroller (8051) as shown in the circuit diagram. R3 to R10 are current limiting
resistors. S3 is the reset switch and R2,C3 forms a denouncing circuitry. C1, C2 and X1 are
related to the clock circuit. The software part of the project has to do the following tasks.

● Form a 0 to 9 counter with a predetermined delay (around 1/2 second here).


● Convert the current count into digit drive pattern.
● Put the current digit drive pattern into a port for displaying.
Program.

ORG 0000H //initial starting address

​START:​ MOV A,#00001001B // initial value of accumulator


MOV DPTR, #8000H
MOV R0,#0AH //​Register R0 initialized as counter which counts from 10 to 0
MOV A,#00H
​LABEL:​ ​ INC A
MOVC A, @A+DPTR / / adds the byte in A to the dptr address
MOV P1, A
ACALL ​DELAY​ // calls the delay of the timer
DEC R0 //Counter R0 decremented by 1
MOV A,R0 ​ // R0 moved to accumulator to check if it is zero in next instruction.
JZ ​START​ //Checks accumulator for zero and jumps to START
SJMP ​LABEL
DELAY: MOV R4,#05H // subroutine for delay
WAIT1: MOV R3,#00H
WAIT2: MOV R2,#00H
WAIT3: DJNZ R2, ​WAIT3
DJNZ R3, ​WAIT2
DJNZ R4, ​WAIT1
RET
DB: 8000:​ ​3FH // digit drive pattern for 0
DB 8001:​ ​06H // digit drive pattern for 1
DB 8002:​ ​5BH // digit drive pattern for 2
DB 8003:​ ​4FH // digit drive pattern for 3
DB 8004:​ ​66H // digit drive pattern for 4
DB 8005:​ ​6DH // digit drive pattern for 5
DB 8006:​ ​7DH // digit drive pattern for 6
DB 8007:​ ​07H // digit drive pattern for 7
DB 8008:​ ​ 7FH // digit drive pattern for 8
DB8009:​ ​ 6FH // digit drive pattern for 9

You might also like