Thursday, July 5, 2012



source code:-


//----- reading the char or no from the keyboard----//
#include<reg51.h>
sfr ROW=0x80; //assigning PORT-0 to read rows
sfr COL=0xA0; //assigning PORT-2 to read colomns
sfr ldata=0x90; //assigning PORT-1 for LCD data
sbit rs=P3^0;
sbit rw=P3^1;
sbit en=P3^2;
sbit busy=P2^7;
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void lcdready(void);
void msdelay(unsigned int itime);
void lcddisplay(unsigned char ch);
  void clear(void);

void main()
{
unsigned char dat[4][4]={'7','8','9','%', // assigning key matrix
                          '4','5','6','*',
  '1','2','3','-',
  'C','0','=','+'};
 unsigned char colloc,rowloc;
 COL=0xFF;
 ROW=0x00;
 rs=0;
 rw=0;
 en=0;
 busy=0;
 /* setting LCD screen*/
 ldata=0x00;
 lcdcmd(0x38);
 lcdcmd(0x0E);
 lcdcmd(0x01);
 lcdcmd(0x06);
 lcdcmd(0x80);
 while(1)
 {
/* reading character from keyboard */
   do
   {
    ROW=0x00;
    colloc=COL;
colloc&=0x0F;
}while(colloc!=0x0F);
do
{
do
{
 msdelay(25);
 colloc=COL;
 colloc&=0x0F;
 }while(colloc==0x0F);
 msdelay(25);
 colloc=COL;
 colloc&=0x0F;
 }while(colloc==0x0F);
    while(1)
{
ROW=0xFE;
colloc=COL;
colloc&=0x0F;
if(colloc!=0x0F)
{
 rowloc=0;
 break;
 }
 ROW=0xFD;
 colloc=COL;
 colloc&=0x0F;
  if(colloc!=0x0F)
  {
   rowloc=1;
break;
}
 ROW=0xFB;
 colloc=COL;
 colloc&=0x0F;
  if(colloc!=0x0F)
  {
   rowloc=2;
break;
}
 ROW=0xF7;
 colloc=COL;
 colloc&=0x0F;
if(colloc!=0x0F)
 {
     rowloc=3;
     break;
   }
  }
   if(colloc==0x0E)
   lcddisplay(dat[rowloc][0]);
   else if(colloc==0x0D)
   lcddisplay(dat[rowloc][1]);
   else if(colloc==0x0B)
   lcddisplay(dat[rowloc][2]);
   else
    lcddisplay(dat[rowloc][3]);
}
  }
  /*displaying char on LCD screen*/
void lcddisplay(unsigned char x)
{
 if(x=='C')
  {
   clear();
    }

 else
 lcddata(x);
   }
 /* sending commands to  LCD display to act in command mode */
void lcdcmd(unsigned char value)
{
 lcdready();
 ldata=value;
 rs=0;
 rw=0;
 en=1;
 msdelay(1);
 en=0;
 }
 /* sending commnad to LCD to display characters*/
void lcddata(unsigned char value)
{
 lcdready();
 ldata=value;
 rs=1;
 rw=0;
 en=1;
 msdelay(20);
 en=0;
 }
 /* checking LCD buffer for free */
void lcdready(void)
{
 busy=1;
 rs=0;
 rw=1;
 if(busy==1)
  {
   en=0;
   msdelay(20);
   en=1;
   }
}
/* generating delay*/
void msdelay(unsigned int value)
{
 unsigned int i,j;
 for(i=0;i<value;i++)
 for(j=0;j<100;j++);
 }
 void clear(void)
 {
  lcdcmd(0x01);
  }