#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
sbit rs=P1^0; //寄存器选择位,将rs定义为p1.0
sbit rw=P1^1; //读写选择位,将rw定义为p1.1
sbit en=P1^2; //使能信号位将en定义为p1.2的引脚
sbit ledg=P1^3;
sbit ledr=P1^7;
sbit relay=P1^4;
sbit buzz=P1^6;
char table0[]="error";
char table1[]="open";
char table2[]="password:";
int temp,ch,m0,ml,p,n0,n1,n2,n3,n4,n5;
void delay(int Z)
{
int x,c;
for(x=Z;x>0;x--)
for(c=100;c>0;c--);
}
keyscan()
{
temp=P2&0xf0;
P2=0xfe;
delay(1);
temp=P2&0xf0;
while(temp!=0xf0)
{
switch(temp)
{
case 0xe0:ch='7';break;
case 0xd0:ch='8';break;
case 0xb0:ch='9';break;
default:ch=p; break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
P2=0xfd;
delay(1);
temp=P2&0xf0;
while(temp!=0xf0)
{
switch(temp)
{
case 0xe0:ch='4';break;
case 0xd0:ch='5';break;
case 0xb0:ch='6';break;
default:ch=p;break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
P2=0xfb;
delay(1);
temp=P2&0xf0;
while(temp!=0xf0)
{
switch(temp)
{
case 0xe0:ch='l';break;
case 0xd0:ch='2';break;
case 0xb0:ch='3';break;
default:ch=p;break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
P2=0xf7;
delay(1);
temp=P2&0xf0;
while(temp!=0xf0)
{
switch(temp)
{
case 0xe0:ch='A';break;
case 0xd0:ch='0';break;
case 0xb0:ch='B';break;
default:ch=p;break;
}
while(temp!=0xf0)
{
temp=P2;
temp=temp&0xf0;
}
}
return ch;
}
void wcom(uchar com)
{
rs=0;
P3=com;
delay(1); //命令延时为1
en=1;
delay(1);
en=0;
}
void wdat(uchar dat)
{
rs=1;
P3=dat;
delay(1); //此处写数据延时可以为1
en=1l;
delay(4); //此处写数据延时至少为4
en=0;
}
void init()
{
en=0;
wcom(0x38);
wcom(0x0c);
wcom(0x06);
wcom(0x01);
}
void error()
{
char m2;
wcom(0xc6);
for(m2=0;m2<5;m2++)
{
wdat(table0[m2]);
}
}
void open()
{
char m2;
wcom(0xc6);
for(m2=0;m2<4;m2++)
{
wdat(table1[m2]);
}
}
void pass()
{
char m2;
wcom(0x80);
for(m2=0;m2<9;m2++)
{
wdat(table2[m2]);
}
}
change(int m)
{
delay(500);
wcom(m);
wdat('*');
}
main()
{
rw=0;
ledg=0;
ledr=0;
relay=0;
buzz=0;
init();
delay(5);
pass();
wcom(0x89);
while(keyscan()==p)
{
delay(3);
keyscan();
}
wdat(keyscan());
n0=keyscan();
change(0x89);
delay(10);
ch=p;
while(keyscan()==p)
{
delay(3);
keyscan();
}
wdat(keyscan());
n1=keyscan();
change(0x8a);
ch=p;
while(keyscan()==p)
wdat(keyscan());
n2=keyscan();
change(0x8b);
ch=p;
while(keyscan()==p)
{
delay(3);
keyscan();
}
wdat(keyscan());
n3=keyscan();
change(0x8c);
ch=p;
while(keyscan()==p)
{
delay(3);
keyscan();
}
wdat(keyscan());
n4=keyscan();
change(0x8d);
ch=p;
while(keyscan()==p)
{
delay(3);
keyscan();
}
wdat(keyscan());
n5=keyscan();
change(0x8e);
if(n0=='2'&&n1=='0'&&n2=='1'&&n3=='4'&&n4=='1'&&n5=='9')
{
int m3=1;
open();
relay=1;
delay(3000);
relay=0;
while(m3)
{
int m4,m5;
ledg=0;
for(m4=200;m4>0;m4--)
{
keyscan();
if(keyscan()=='A')
{
m4=0;
m3=0;
}
}
ledg=1;
if(m3!=0)
{
for(m5=200;m5>0;m5--)
{
keyscan();
if(keyscan()=='A')
{
m3=0;
}
}
}
}
}
else
{
ledr=1;
error();
buzz=1;
delay(5000);
}
}
|