#include<reg51.h>
void lcd_cmd(char);
void lcd_init();
void lcd_data(char);
void delay(int);
sbit rs=P2^0;
sbit oe=P2^1;
void column1();
void column2();
void column3();
char getkey();
sbit c1=P1^0;
sbit c2=P1^1;
sbit c3=P2^2;
sbit r1=P2^3;
sbit r2=P2^4;
sbit r3=P2^5;
sbit r4=P2^6;
int x,y;
char num[4][3]={{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}};
void main()
{
char c;
r1=r2=r3=r4=rs=oe=0;
c1=c2=c3=1;
P3=0x00;
TMOD=0x01;
lcd_init();
while(1)
{
while(c1==1 && c2==1 && c3==1);
c=getkey();
lcd_data(c);
}
}
char getkey()
{
if(c1==0 || c2==0 || c3==0)
{
if(c1==0)
{
column1();
while(!c1);
x=0;
}
if(c2==0)
{
column2();
while(!c2);
x=1;
}
if(c3==0)
{
column3();
while(!c3);
x=2;
}
}
return num[y][x];
}
void column1()
{
r1=r2=r3=r4=1;
r1=0;
if(c1==0)
y=0;
r1=1;r2=0;
if(c1==0)
y=1;
r1=1;r2=1;r3=0;
if(c1==0)
y=2;
r1=1;r2=1;r3=1;r4=0;
if(c1==0)
y=3;
r1=r2=r3=r4=0;
}
void column2()
{
r1=r2=r3=r4=1;
r1=0;
if(c2==0)
y=0;
r1=1;r2=0;
if(c2==0)
y=1;
r1=1;r2=1;r3=0;
if(c2==0)
y=2;
r1=1;r2=1;r3=1;r4=0;
if(c2==0)
y=3;
r1=r2=r3=r4=0;
}
void column3()
{
r1=r2=r3=r4=1;
r1=0;
if(c3==0)
y=0;
r1=1;r2=0;
if(c3==0)
y=1;
r1=1;r2=1;r3=0;
if(c3==0)
y=2;
r1=1;r2=1;r3=1;r4=0;
if(c3==0)
y=3;
r1=r2=r3=r4=0;
}
void lcd_cmd(char x)
{
rs=0;
P3=x;
oe=1; //creating a pulse
delay(1);
oe=0;
}
void lcd_init()
{
lcd_cmd(0x38);
lcd_cmd(0x01);
lcd_cmd(0x06);
lcd_cmd(0x0E);
}
void delay(int n)
{
int i=0;
for(i=0;i<n;i++)
{
TR0=1;
while(TF0!=1);
TF0=0;
TR0=0;
}
}
void lcd_data(char x)
{
rs=1;
P3=x;
oe=1;
delay(1);
oe=0;
}
Saturday, February 25, 2012
Keypad Interfacing with 8051 Program
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment