Sie sind auf Seite 1von 10

//----- Smart Energy Meter----//

#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^3;
sbit rw=P3^4;
sbit en=P3^5;
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 printstring(unsigned char x[]);
void menu();
int keypad();
int count();
void displaycount();
void status(void);
void serTx(unsigned char x);
void gridTx();
unsigned char C_ount;

void ext() interrupt 0


{ unsigned int x;
x++;
C_ount=x;
}

void main(void)
{

lcdcmd(0x38);
lcdcmd(0x0F);
lcdcmd(0x06);
lcdcmd(0x01);
lcdcmd(0x80);
printstring("Welcome Home");
msdelay(1000);
IE=0x81;
TCON=0x03;
while(1){
menu();
}
}

/* 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(0);
en=0;
}
/* sending commnad to LCD to display characters*/
void lcddata(unsigned char value)
{
lcdready();
ldata=value;
rs=1;
rw=0;
en=1;

en=0;
}
/* checking LCD buffer for free */
void lcdready(void)
{
busy=1;
rs=0;
rw=1;
if(busy==1)
{
en=0;
msdelay(1);
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 LCDclear(void)
{
lcdcmd(0x01);
}
int keypad()
{
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(0x06);

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)
return(dat[rowloc][0]);
else if(colloc==0x0D)
return(dat[rowloc][1]);
else if(colloc==0x0B)
return(dat[rowloc][2]);
else
return(dat[rowloc][3]);
}
}
void menu(void)
{
unsigned char userID[4]={'9','8','7','6'};
unsigned char PWD[4]={'1','2','3','4'};
unsigned char user_id[4]={'0','0','0','0'};
unsigned char password[4];
unsigned int i=0;
while(1)
{
LCDclear();
lcdcmd(0x80);
printstring("Enter UserID:");
lcdcmd(0xC0);
msdelay(200);
do
{
user_id[i]=keypad();
lcddata(user_id[i]);
i++;
msdelay(250);
}while(i!=4);
i=0;
if(userID[0]==user_id[0] && userID[1]==user_id[1] && userID[2]==user_id[2] &&
userID[3]==user_id[3])
{
LCDclear();
lcdcmd(0x80);
printstring("Enter Password:");
lcdcmd(0xC0);
i=0;
do
{
password[i]=keypad();
lcddata('*');
i++;
msdelay(200);
}while(i!=4);
i=0;
if(PWD[0] == password[0] && PWD[1] == password[1] && PWD[2] ==
password[2] && PWD[3] == password[3])
{
LCDclear();
lcdcmd(0x80);
printstring("Authunticated");
msdelay(1000);
LCDclear();
status();
}
else
{
LCDclear();
lcdcmd(0x80);
printstring("Access Denied");
msdelay(1000);
}
}
else
{
LCDclear();
lcdcmd(0x80);
printstring("Invalid UserID");
msdelay(1000);
}
}
}

void printstring(unsigned char ch[])


{
unsigned int i;
for(i=0;ch[i]!='\0';i++)
lcddata(ch[i]);
}
void status(void)
{
unsigned char i;
while(1)
{
// LCDclear();
lcdcmd(0x80);
printstring("1.Reading");
lcdcmd(0xC0);
printstring("2.Settings");
do // here we can find
difference b/w while and do while
{
i=keypad();
}while(i != '1' && i != '2' && i != 'C');
LCDclear();
switch(i)
{
case '1' :while(1)
{
lcdcmd(0x80);
printstring("1.read Units");
lcdcmd(0xC0);
printstring("2.send to grid");
do{
i=keypad();
}while(i !='1' && i!='2' && i!=3);
switch(i)
{
case '1':while(1)
{
lcdcmd(0x80);
printstring("No.of Units:");
lcdcmd(0x8C);
displaycount();
lcdcmd(0xC0);
printstring("price/unit:6rs");
if('C' == keypad())
{
LCDclear();
status();
}
}
break;
case '2': gridTx();
LCDclear();
printstring(" Successfull");
if('C' == keypad())
{
LCDclear();
status();

break;
case 'C': LCDclear();
status();
break;
default : status();
break;
}
}

case '2': while(1)


{
lcdcmd(0x80);
printstring("1.change UserID:");
lcdcmd(0xC0);
printstring("2.change passwd:");
if('C' == keypad())
{
LCDclear();
status();
}
}
break;
case 'C': LCDclear();
menu();
break;
default :status();
break;
}
}

// Desplay number of units on LCD


void displaycount()
{
unsigned int Count;
Count=C_ount-1;
lcddata(Count/1000+0x30);
Count=Count%1000;
lcddata(Count/100+0x30);
Count=Count%100;
lcddata(Count/10+0x30);
Count=Count%10;
lcddata(Count/1+0x30);
Count=Count%1;
lcddata(Count);
}
void serTx(unsigned char x)
{
TMOD = 0x20;
TH1=0xFD;
SCON=0x50;
TR1=1;
SBUF=x;
while(TI == 0);
TR1=0;
TI=0;
}
void gridTx()
{
unsigned int i,Count;
unsigned char ID_no[]={" ID:95XXXXXXXX"};
unsigned char u[]={" Units:"};
for(i=0;ID_no[i]!='\0';i++)
serTx(ID_no[i]);
for(i=0;u[i]!='\0';i++)
serTx(u[i]);
Count=C_ount-1;
serTx(Count/1000+0x30);
Count=Count%1000;
serTx(Count/100+0x30);
Count=Count%100;
serTx(Count/10+0x30);
Count=Count%10;
serTx(Count/1+0x30);
Count=Count%1;
serTx(Count);
}

Das könnte Ihnen auch gefallen