Tuesday, 21 October 2014

Keypad Interfacing with 8051 Microcontroller

KEYPAD INTERFACING WITH 8051 + LCD DATA READ

Here LCD is interfaced with 4*3 keypad and whenever read button is pressed the entered number will be displayed on 2nd line of the LCD. Maximum length of the entered number is 16 digits. The code was tested in proteus software and circuit diagram  is also shown below.

#include<reg51.h>
sbit r1=P1^0;
sbit r2=P1^1;
sbit r3=P1^2;
sbit r4=P1^3;
sbit c1=P1^4;
sbit c2=P1^5;
sbit c3=P1^6;
sbit rs=P3^0;
sbit rw=P3^1;
sbit en=P3^2;

sbit del=P3^3;
sbit read=P3^4;
sbit shift_right=P3^5;

int m;

unsigned char temp[50];

void delay(unsigned int time)
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<1275;j++);
}

void cmd(unsigned char value)
{
P2=value;
rs=0;
rw=0;
en=1;
delay(2);
en=0;
}

void dat(unsigned char value)
{
P2=value;
rs=1;
rw=0;
en=1;
delay(2);
en=0;
}


void lcd_init()
{
cmd(0X38);
cmd(0X0C);
cmd(0X01);
cmd(0X06);
cmd(0X80);
}



void col1()
{
r1=r2=r3=r4=1;
r1=0;
if(c1==0)
{
dat('1');
temp[m]='1';
m=m+1;
r1=1;
}
r2=0;
if(c1==0)
{
dat('4');
temp[m]='4';
m=m+1;
r2=1;
}
r3=0;
if(c1==0)
{
dat('7');
temp[m]='7';
m=m+1;
r3=1;
}
r4=0;
if(c1==0)
{
dat('*');
temp[m]='*';
m=m+1;
r4=1;
}
delay(100);
}

void col2()
{
r1=r2=r3=r4=1;
r1=0;
if(c2==0)
{
dat('2');
temp[m]='2';
m=m+1;
r1=1;
}
r2=0;
if(c2==0)
{
dat('5');
temp[m]='5';
m=m+1;
r2=1;
}
r3=0;
if(c2==0)
{
dat('8');
temp[m]='8';
m=m+1;
r3=1;
}
r4=0;
if(c2==0)
{
dat('0');
temp[m]='0';
m=m+1;
r4=1;
}
delay(100);
}

void col3()
{
r1=r2=r3=r4=1;
r1=0;
if(c3==0)
{
dat('3');
temp[m]='3';
m=m+1;
r1=1;
}
r2=0;
if(c3==0)
{
dat('6');
temp[m]='6';
m=m+1;
r2=1;
}
r3=0;
if(c3==0)
{
dat('9');
temp[m]='9';
m=m+1;
r3=1;
}
r4=0;
if(c3==0)
{
dat('#');
temp[m]='#';
m=m+1;
r4=1;
}
delay(100);
}


void main()
{
c1=c2=c3=1;
lcd_init();
read=1;
while(1)
{
r1=r2=r3=r4=0;
if(c1==0)
{
col1();
}
if(c2==0)
{
col2();
}
if(c3==0)
{
col3();
}

if(del==0)
{
cmd(0x10);
dat(' ');
cmd(0x10);
m=m-1;
delay(50);
}

if(shift_right==0)
{
cmd(0X14);
temp[m]=' ';
m=m+1;
delay(30);
}

if(read==0)
{
cmd(0XC0);
for(m=0;m<5;m++)
{
dat(temp[m]);
delay(10);
}
}



}
}



No comments:

Post a Comment