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

Experiment No. 8: Aim: WAP To Interface The 4x4 Keypad With Microcontroller and Display The Respective Digit On

You will get to know about keypad interfacing with 8051 microcontroller. Mainly for the students studying Embedded System.

Uploaded by

Sumeet Saini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Experiment No. 8: Aim: WAP To Interface The 4x4 Keypad With Microcontroller and Display The Respective Digit On

You will get to know about keypad interfacing with 8051 microcontroller. Mainly for the students studying Embedded System.

Uploaded by

Sumeet Saini
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Experiment No.

8
Aim: WAP to interface the 4x4 keypad with microcontroller and display the respective digit on
LCD.
Apparatus required: 89s5x trainer kit, connecting chords.
Circuit diagram:
VCC

40

VCC

10 uF
10K

EA/ VPP
30 pF

VCC

P0.5
31

P0.4

X1

11.06MHz
18

P0.0

9
10

GND

P0.7
P0.3

19

30 pF

P0.6

11
12
13
14
15
1
2
3
4

X2
RST

P0.1
P0.2

P3.0/RXD
P3.1/TXD

P2.0

P3.2/INT0
P3.3/INT1
P3.4/T0
P3.5/T1

P2.1

89s51

P2.2
P2.3
P2.4

P1.0

P2.5

P1.1

P2.6

P1.2

P2.7

P1.3
PSEN
ALE

P3.6/ WR

P1.4
P1.5
P1.6
8

P1.7

P3.7/ RD
GND

39

16x2

33

VEE
VSS

D6D7VCC

11 12 13 14 2
D4

9 10
D3

7 8
D1

D2

D0

RS

35
34

R/W

36

37

D5

38

10K

32
21
22
23
24
25
26
27
28
29
30
16
17
20

GND

Fig.1: Circuit diagram for interfacing the 4x4 keypad and LCD with microcontroller

Program:
#include <stdio.h>
#include <reg51.h>
#define DATA P1
void lcd_init(void);
void lcd_cmd(unsignedchar);
void lcd_display(unsigned ch);
void Key_Scan(void);
void DelayMs(int);
sbit RS = P3^4;
sbit RW = P3^5;
sbit lcd_e = P3^6;
unsigned char R,C,ch;
unsigned int i=0;
unsigned char Key[4][4] =
{'1','2','3','4',
'5','6','7','8',
'9','0','A','B',
'C','D','E','F',
};
unsigned char msg[] = ("keypad
interfacing");
unsigned char msg1[] = (" BOARD
");
void main()
{
lcd_init();
while(1)
{
Key_Scan();
ch = Key[C][R];
SBUF=ch;
DelayMs(35);
lcd_display(ch);
lcd_cmd(0x80);
DelayMs(35);
}
}
void Key_Scan(void)
{
unsigned int i = 0;
P2 = 0x0F;
while(P2==0x0F);
if(P2 == 0x0E)
R = 0;

else if(P2 == 0x0D)


R = 1;
else if(P2 == 0x0B)
R = 2;
else if(P2 == 0x07)
R = 3;
while(P2 == 0xF0);
if(P2 == 0xE0)
C = 0;
else if(P2 == 0xD0)
C = 1;
else if(P2 == 0xB0)
C = 2;
else if(P2 == 0x70)
C = 3;
DelayMs(50);
}
void lcd_cmd(unsigned char cmnd)
{
DATA = cmnd;
RS = 0;
RW = 0;
lcd_e = 1;
DelayMs(35);
lcd_e = 0;
}
void lcd_display(unsigned char dat)
{
DATA = dat;
RS = 1;
RW = 0;
lcd_e = 1;
DelayMs(35);lcd_e = 0;
}
void lcd_init(void)
{
unsigned char i;
lcd_cmd(0x38);
DelayMs(15);
lcd_cmd(0x0c);
DelayMs(15);
lcd_cmd(0x06);
DelayMs(15);
lcd_cmd(0x01);
DelayMs(15);
lcd_cmd(0x80);

DelayMs(35);
i=0;
while(msg[i]!='\0')
{
lcd_display(msg[i]);
i++;
}
DelayMs(50);
lcd_cmd(0xc0);
DelayMs(35);
i=0;
while(msg1[i]!='\0')
{
lcd_display(msg1[i]);
i++;
}
DelayMs(50);
lcd_cmd(0x06);
DelayMs(50);
}
void DelayMs(int k)
{
unsigned int a;
for(a=0;a<=k;a++);
}
Hardware:

Fig2:hardware interfacing.

Worksheet

1) Explain keypad interfacing in detail with 8051.


Interfacing keypad
Figure shows how to interface the 4 X 4 matrix keypad to two ports in microcontroller. The
rows are connected to an output port and the columns are connected to an input port.
To detect a pressed key, the microcontroller grounds all rows by providing 0 to the output
latch, and then it reads the columns. If the data read from the columns is D3-D0=1111, no key
has been pressed and the process continues until a key press is detected. However, if one of
the column bits has a zero, this means that a key press has occurred. For example, if D3D0=1101, this means that a key in the D1 column has been pressed.
After a key press is detected, the 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; then 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, the next task is to find out which column the
pressed key belongs to.

Fig. 1 interfacing keypad to Microcontroller

Interfacing keypad with 8051


We now want to scan a keypad in 8051 Slicker Board. In case of 4X4 matrix Keypad both
the ends of switches are connected to the port pin i.e. four rows and four columns. So in all
sixteen switches have been interfaced using just eight lines.
1Keypads arranged by matrix format, each row and column section pulled by high or low by
selection J5, all row lines(P2.4 P2.7) and column lines(P2.0 to P2.3) connected directly by
the port pins.
Pin Assignment with 8051
4x4 Matrix
Lines

8051 Lines

ROW-0

P2.0

ROW-1

P2.1

ROW-2

P2.2

ROW-4

P2.3

COLUMN-0

P2.4

COLUMN-1

P2.5

COLUMN-2

P2.6

COLUMN COLUMN-3

P2.7

ROW

Circuit Diagram to Interface keypad with 8051

4x4 Matrix Keypad

2) What is the difference between analog ground and digital ground?


Typically digital circuits will pump a lot of fast transient currents into the ground. Since
real grounds have resistance and inductance, this will produce a drop between the ground
of the digital circuits and the power supply ground. This is the so called "ground bounce".
If an analog circuit ground shares ground path between the digital ground and the power
supply ground, it will see this voltage drop. Depending on the common mode rejection of
the analog circuit in question and the frequency components in the "ground bounce this
may or may not be an issue.
If it's an issue, the easy solution is to connect the analog ground directly to the power supply
ground without sharing any of the path from digital ground to main power supply ground.
This way the current spike induced drops in the digital ground don't show up in the analog
ground. A good example would be ADC grounding for the digital and analog portions of the
ADC chip.
3). We need to operate the key in interrupt mode. How the key should be connected?
4. WAP to interface the 2x2 keypad with 8051 and display the respective digit on seven
segment display?
#include <stdio.h>
#include <reg51.h>
void Key_Scan(void);
void DelayMs(int);
sfr Data=0x80;
unsigned char R,C,ch;
unsigned int i=0;
unsigned char Key[2][2] = {'1','2','3','4'};
void main()
{
while(1)
{
Key_Scan();
ch = Key[C][R];
if(ch=='1')
Data=0x06;
else if(ch=='2')
Data=0x5B;
else if(ch=='3')
Data=0x4F;
else if(ch=='4')

Data=0x66;
}
}
void Key_Scan(void)
{
unsigned int i = 0;
P2 = 0x30;
while(P2==0x30);
if(P2 == 0x20)
R = 0;
else if(P2 == 0x10)
R = 1;
while(P2 == 0xC0);
if(P2 == 0x80)
C = 0;
else if(P2 == 0x40)
C = 1;
DelayMs(50);
}
void DelayMs(int k)
{
unsigned int a;
for(a=0;a<=k;a++);
}

You might also like