#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P3^5;
sbit lcden=P3^4;
sbit dula=P2^6;
sbit wela=P2^7;
uchar new1,new2,new3,new4;
uchar number[]="1234";
uchar password[]="4321";
uchar cuowu[]="Error";
uchar zhengque[]="right";
uchar kaisuo[]="open";
uchar jinzhi[]="wait a minute";
uchar temp,key,wei,wanbi,allow,times,reset;
void delay(uint i) //延时
{
uint m,n;
for(m=i;m>0;m--)
for(n=114;n>0;n--);
}
void write_com(uchar com) //写指令
{
P0=com;
rs=0;
lcden=1;
delay(10);
lcden=0;
}
void write_dat(uchar dat) //写数据
{
P0=dat;
rs=1;
lcden=1;
delay(10);
lcden=0;
}
void init() //初始化
{
dula=0;
wela=0; //关数码管
write_com(0x38);
write_com(0x0c);
write_com(0x06);
}
void getkey() //矩阵按键
{
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xbe:
key=1;wei++;
break;
case 0x7e:
key=2;wei++;
break;
}
while(temp!=0xf0) //弹起
{
temp=P3;
temp=temp&0xf0;
}
switch(key){
case 1:write_dat(number[0]);break; //按键后密码显示在液晶上
case 2:write_dat(number[1]);break;
}
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xbd:
key=3;wei++;
break;
case 0x7d:
key=4;wei++;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
switch(key){
case 3:write_dat(number[2]);;break;
case 4:write_dat(number[3]);;break;
}
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xbb:
wanbi=1;wei=5;
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
switch(wei){ //分配输入值
case 1:new1=key;break;
case 2:new2=key;break;
case 3:new3=key;break;
case 4:new4=key;break;
case 5:;break;
}
}
void getkey1()
{
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0x7b:
reset=1; //重置值
break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
}
}
}
}
void yenzheng()
{
unsigned char a;
if((new1==4)&(new2==3)&(new3==2)&(new4==1)) //校验密码
allow=1;
if(allow==1)
{
wanbi=0;
write_com(0x80+8); //在输入的密码后面显示
for(a=0;a<5;a++){
write_dat(zhengque[a]); //显示right
delay(10);
}
delay(400);
write_com(0xc0);
for(a=0;a<4;a++){
write_dat(kaisuo[a]); //显示open
}
}
else
{
times++;
write_com(0x80+8);
for(a=0;a<5;a++){
write_dat(cuowu[a]); //显示error
delay(10);
}
while(reset==0)
{
getkey1();
}
if(reset==1) //重置输入密码
{
reset=0;
wei=0;
wanbi=0;
write_com(0x01);
init();
}
}
}
void main()
{
uchar a;
uchar b;
uchar c;
init();
while(1)
{
while(wanbi==0)
{
getkey();
}
yenzheng();
if(times==3)
{
TMOD=0x10; //定时器T1
TR1=1; //开T1
TH1=0x28;
TL1=0x00;
delay(200);
write_com(0x80);
for(a=0;a<13;a++)
write_dat(jinzhi[a]); //显示等一分钟
while(1)
{
if(TF1==1)
{
b++;
TH1=0x28;
TL1=0x00;
TF1=0;
}
if(b==250) //15秒
{
b=0;
c++;
if(c==4) //60秒
{
c=0;
times=0;
write_com(0x01);
break;
}
}
}
}
}
}
|