Experiment No. 7 Interfacing Keys, Keypad To Microcontroller
Experiment No. 7 Interfacing Keys, Keypad To Microcontroller
Experiment No. 7 Interfacing Keys, Keypad To Microcontroller
7
Interfacing Keys, Keypad to Microcontroller
-------------------------------------------------------------------------------------------------------------------Name of Student:
-------------------------------------------------------------------------------------------------------------------Day & Date of Submission:
Checked and verified by:
--------------------------------------------------------------------------------------------------------------------
Experiment No. 7
Aim: To study, understand operation of a keys, Keypad, Problems and interfacing of
Keys, keypad to 8051 Microcontroller.
Objectives:
i.
To understand the basic operation of a keys such as push button, key pads
and different types of Keys and keyboards
ii.
iii.
iv.
Theory:
Keyboards andLCD's are the most widely used input/output devices of the 8051.At the
lowest level, 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 toa microprocessor.When a key is pressed,a row and a
column make a contact; otherwise, there is no connection between rows and columns.
The rows are connected to an output port and the columns are connected to an input
port.If no key has been pressed ,reading the input port will yield 1s for all columns
since they are all connected to high (Vcc).If all the rows are grounded and a key is
pressed, one of the column will have 0 since the key pressed provides the path to
ground.It is the function of the microcontroller to scan the keyboard continuously to
detect and identify the key pressed.
In IBM PC keyboards, a single microcontroller (consisting ofa microprocessor, RAM
and EPROM, and several ports all on a single chip) takes care of hardware and software
interfacing of the keyboard.In such systems, it is the function of programs stored in the
EPROM of the microcontroller to scan the keys continuously, identify which one has
been activated, and presentit to the motherboard.
Problem Statements:
1. Interface the keypad with 8051 and display the pressed key on the LCD .
sbit c1=P2^4;
sbit c2=P2^5;
sbit c3=P2^6;
sbit c4=P2^7;
sbit rs=P0^0;
sbit rw=P0^1;
sbit e=P0^2;
for(i=0;i<6000;i++);
void ms_delay(int k)
{
for(j=0;j<k;j++)
{ for(i=0;i<922;i++); }
void lcdcmd()
{
rs=0; //RS
rw=0; //RW}
void lcddata()
{ rs=1; rw=0;
void enable()
{
e=0; // ENABLE
e=1;e=0;
void lcd_init()
{
lcdcmd();
P1=0x38;
enable();delay();
P1=0x06;
enable();delay();
P1=0x0e;
enable();delay();
P1=0x01;
enable();delay();
void lcd_display(char x)
{
lcddata();P1=x;
enable();delay();
lcd_init();
count=0;
while(1)
{ P2=0xfe;
if(c1==0)
{ms_delay(30);lcd_display('7');count++; }
if(c2==0)
{ms_delay(30);lcd_display('8');count++;}
if(c3==0)
{ms_delay(30);lcd_display('9');count++;}
if(c4==0)
{ms_delay(30);lcd_display('/');count++;}
P2=0xfd;
if(c1==0)
{ms_delay(30);lcd_display('4');count++; }
if(c2==0)
{ms_delay(30);lcd_display('5');count++;}
if(c3==0)
{ms_delay(30);lcd_display('6');count++;}
if(c4==0)
{ms_delay(30);lcd_display('*');count++;}
P2=0xfb;
if(c1==0)
{ms_delay(30);lcd_display('1');count++; }
if(c2==0)
{ms_delay(30);lcd_display('2');count++;}
if(c3==0)
{ms_delay(30);lcd_display('3');count++;}
if(c4==0)
{ms_delay(30);lcd_display('-');count++;}
P2=0xf7;
if(c1==0)
{ms_delay(30);lcd_display('C');count++; }
if(c2==0)
{ms_delay(30);lcd_display('0');count++;}
if(c3==0)
{ms_delay(30);lcd_display('=');count++;}
if(c4==0)
{ms_delay(30);lcd_display('+');count++;}
if(count==16)
{lcdcmd();
P1=0xc0;
enable();
delay();}
}
}
Design:
Conclusion: