#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar old1,old2,old3,old4,old5,old6; //原始密码000000
uchar new1,new2,new3,new4,new5,new6; //每次MCU采集到的密码输入
uchar a=16,b=16,c=16,d=16,e=16,f=16; //送入数码管显示的变量
uchar wei,key, chongxin,shurugeshu=0, genggaiwanbi=0;
uint num, temp, zhengque, eee, hhh=1;
bit allow,genggai,ok,wanbi,retry,close; //各个状态位
sbit lcden = P3^4;
sbit lcdrs = P3^5; //高 写数据 ;低 写指令
sbit lcdwr = P3^6; // 0 write 1 read
sbit rest = P3^7;
sbit dula = P2^6;
sbit wela = P2^7;
sbit beep = P2^3;
uchar code tableA[] = {"欢迎回家!!! "};
uchar code tableB[] = {"请输入密码: "};
uchar code tableC[] = {"*********** "};
uchar code tableD[] = {"更改密码: "};
uchar code tableK[] = {" "};
uchar code tableN[] = {" "};
uchar code table_zhengque[] = {"密码正确! "};
uchar code table_genggaiwanbi[] = {"更改完毕! "};
uchar code table_huifu[] = {"已恢复默认! "};
void delay( uint z )
{
uint x,y;
for(x=z; x>0; x--)
for(y=110; y>0; y--);
}
void write_com( uchar com ) //指令
{
delay( 5 );
lcdrs = 0;
P0 = com;
delay( 5 );
lcden = 1;
delay( 5 );
lcden = 0;
}
void write_data( uchar date ) //数据
{
delay( 5 );
lcdrs = 1;
P0 = date;
delay( 5 );
lcden = 1;
delay( 5 );
lcden = 0;
}
void init()
{
rest = 1;
rest = 0;
rest = 1;
delay( 5 );
dula = 1;
P0 = 0x00;
dula = 0;
wela = 1;
P0 = 0x00;
wela = 0;
lcden = 0;
lcdwr = 0;
write_com( 0x30 ); //显示设置指令
write_com( 0x0c ); //光标设置(开)
write_com( 0x01 ); //清屏
write_com( 0x06 );
write_com( 0x80 ); //数据指针初始化
}
void lcd_display(uchar line, uchar * string, uchar j)
{
uchar addr,i;
if( line==1 )
addr = 0x80;
else if( line==2 )
addr = 0x90;
else if( line==3 )
addr = 0x88;
else if( line==4 )
addr = 0x98;
write_com( addr );
if( j==0 )
{
for(i=0; i<16; i++)
{
write_data( * string++ );
}
}
else
{
for(i=0; i<j; i++)
{
write_data( * string++ );
}
}
}
void keyscan()
{
{
P1=0xfe;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0) //NO1
{
delay(10);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xee:
key=0;
wei++;
break;
case 0xde:
key=1;
wei++;
break;
case 0xbe:
key=2;
wei++;
break;
case 0x7e:
key=3;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
P1=0xfd;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0) //NO2
{
delay(10);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xed:
key=4;
wei++;
break;
case 0xdd:
key=5;
wei++;
break;
case 0xbd:
key=6;
wei++;
break;
case 0x7d:
key=7;
wei++;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
P1=0xfb;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0) //NO3
{
delay(10);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xeb:
key=8;
wei++;
break;
case 0xdb:
key=9;
wei++;
break;
case 0xbb:
genggai=1;
wei=0;
break;
case 0x7b:
if(allow)
ok=1;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
P1=0xf7;
temp=P1;
temp=temp&0xf0; //NO4
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P1;
switch(temp)
{
case 0xe7:
retry=1;
break;
case 0xd7:
close=1;
break;
case 0xb7:
chongxin=1;
break;
}
while(temp!=0xf0)
{
temp=P1;
temp=temp&0xf0;
beep=0;
}
beep=1;
}
}
}
}
void shumima() //对按键采集来的数据进行分配
{
if(!wanbi)
{
/* if( retry==1 )
{
shurugeshu = 0;
} */
switch(wei)
{
case 1:new1=key;
if(!allow) a=17;
else a=key;
shurugeshu = 1; break;
case 2:new2=key;
if(a==17) b=17;
else b=key;
shurugeshu = 2; break;
case 3:new3=key;
if(a==17) c=17;
else c=key;
shurugeshu = 3; break;
case 4:new4=key;
if(a==17) d=17;
else d=key;
shurugeshu = 4; break;
case 5:new5=key;
if(a==17) e=17;
else e=key;
shurugeshu = 5; break;
case 6:new6=key;
if(a==17) f=17;
else f=key;
wanbi=1;
shurugeshu = 6;
genggaiwanbi = 1; break;
}
if( genggai==0 )
{
if( shurugeshu!=0 )
{
if( shurugeshu > 6 )
{
shurugeshu = 1;
}
lcd_display(3, tableC, shurugeshu);
}
}
if( genggai==2 )
{
// shurugeshu = 1;
if( shurugeshu!=0 )
{
// shurugeshu = 1;
if( shurugeshu > 6 )
{
shurugeshu = 1;
}
lcd_display(3, tableC, shurugeshu);
}
}
}
}
void yanzheng() //验证密码是否正确
{
if(wanbi) //只有当六位密码均输入完毕后方进行验证
{
if((new1==old1)&(new2==old2)&(new3==old3)&(new4==old4)&(new5==old5)&(new6==old6))
allow=1;zhengque=1; //当输入的密码正确,会得到allowe置一
}
}
void main()
{
again :
hhh=1;
init();
lcd_display(1, tableA, 0);
lcd_display(2, tableB, shurugeshu);
// lcd_display(3, tableC, 6);
while(1)
{
keyscan();
shumima();
yanzheng();
if(allow) //验证完后,若allow为1,则开锁
{
beep = 0;
// lcd_display(3, tableD, 0);
// if( (zhengque==1) & (eee = 0) )
// {
/* zhengque = 0;
eee = 1;
write_com( 0x01 );
lcd_display(1, tableA, 0); */
if( (genggai!=2) & (hhh!=0) )
lcd_display(2, table_zhengque, 0);
// }
P1=0x00;
if(!genggai)
wanbi=0;
}
if(genggai==1) //当S33更改密码键被按下,genggai会被置一
{
genggai = 2;
// genggaiwanbi = 0;
if(allow) //若已经把锁打开,才有更改密码的权限
{
if( (wanbi==1) & (genggaiwanbi==1) )
{
}
else
{
write_com( 0x01 );
lcd_display(1, tableD, 0); //显示“更改密码”
shurugeshu = 0;
}
while(!wanbi) //当新的六位密码没有设定完,则一直在这里循环
{
keyscan();
shumima();
if(retry|close) //而当探测到重试键S41或者关闭密码锁键S42被按下时,则跳出
{
wanbi=1;genggaiwanbi=1;
break;
}
// display(a,b,c,d,e,f);
}
}
}
if(ok) //更改密码时,当所有六位新密码均被按下时,可以按下此键,结束密码更改
{ //其他时间按下此键无效
ok=0; wei=0;
genggai=0;hhh=0;
old1=new1;old2=new2;old3=new3; //此时,旧的密码将被代替
old4=new4;old5=new5;old6=new6;
a=16;b=16;c=16;d=16;e=16;f=16;
write_com( 0x01 );
lcd_display(2, table_genggaiwanbi, 0); //显示更改完毕
}
if(retry) //当重试按键S41被按下,retry会被置位
{
retry=0; wei=0;wanbi=0;shurugeshu=0;
a=16;b=16;c=16;d=16;e=16;f=16;
new1=0;new2=0;new3=0;new4=0;new5=0;new6=0;
lcd_display(3, tableK, 0);
}
if(close) //当关闭密码锁按键s42被按下,close会被置位
{
key42:
close=0;genggai=0;//所有变量均被清零。
wei=0; wanbi=0;
allow=0;
P1=0xff;
a=16;b=16;c=16;d=16;e=16;f=16;
new1=0;new2=0;new3=0;new4=0;new5=0;new6=0;
shurugeshu = 0;
write_com( 0x01 );
goto key43;
// llcd_display(2, table_huifu, 0);
}
if(chongxin)
{
wei=0;key=0; chongxin=0;shurugeshu=0, genggaiwanbi=0;
num=0; temp=0; zhengque=0; eee=0; hhh=0;
goto key42;
key43:
goto again;
}
// display(a,b,c,d,e,f); //实时显示
}
}
|