1、可设置4位密码
2、用户可自行设定和修改密码
3、若密码错误,报警5S,连续错3次,报警1分钟
4、锁由继电器模拟
#include <AT89X52.H>
unsigned char ps[]={1,2,3,4,5}; //设定密码
unsigned char code dispbit[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f};
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f, //数字0~9
0x77,0x7c,0x39,0x5e,0x79,0x71, //字母A~F
0x00,0x40,0x73,0xff}; //字符 -P8.
unsigned char dispbuf[8]={18,16,16,16,16,16,16,16};
unsigned char dispcount; //显示计数
unsigned char flashcount; //闪烁计数
unsigned char temp; //临时变量
unsigned char key; //按键
unsigned char keycount; //按键计数
unsigned char pslen=5; //密码长度
unsigned char getps[6]; //得到的密码
bit keyoverflag; //密码输入超位
bit errorflag; //错误标志位
bit rightflag; // 正确标志位
unsigned int second3;
unsigned int aa,bb;
unsigned int cc;
bit okflag;
bit al
ARMflag; //报警标志
bit hibitflag; //高位
unsigned char oka,okb;
void main(void)
{
unsigned char i,j;
TMOD=0x01;
TH0=(65536-500)/256; //TH0初始化
TL0=(65536-500)%256; //TL0初始化
TR0=1;
ET0=1;
EA=1;
while(1)
{
P3=0xff; //P3赋值255
P3_4=0;
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
for(i=10;i>0;i--)
for(j=248;j>0;j--);
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
key=7;
break;
case 0x0d:
key=8;
break;
case 0x0b:
key=9;
break;
case 0x07:
key=10;
break;
}
temp=P3;
// P1_1=~P1_1;
if((key>=0) && (key<10))
{
if(keycount<6)
{
getps[keycount]=key;
dispbuf[keycount+1]=19; //向后显示8. ?
}
keycount++;
if(keycount==6)
{
keycount=6;
}
else if(keycount>6)
{
keycount=6;
keyoverflag=1;//key overflow
}
}
else if(key==12)//delete key
{
if(keycount>0)
{
keycount--;
getps[keycount]=0; //最近1次输入的数值清0
dispbuf[keycount+1]=16; //?
}
else
{
keyoverflag=1; //未输入密码按功能键,报错
}
}
else if(key==15)//enter key
{
if(keycount!=pslen) //密码输入出错
{
errorflag=1;
rightflag=0;
second3=0; //
}
else
{
for(i=0;i<keycount;i++) //密码比较
{
if(getps[i]!=ps[i])
{
i=keycount;
errorflag=1;
rightflag=0;
second3=0;
goto a;
}
}
errorflag=0;
rightflag=1;
a: i=keycount;
}
}
temp=temp & 0x0f;
while(temp!=0x0f) //判断按键是否弹回
{
temp=P3;
temp=temp & 0x0f;
}
keyoverflag=0;
}
}
P3=0xff;
P3_5=0;
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
for(i=10;i>0;i--)
for(j=248;j>0;j--);
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
key=4;
break;
case 0x0d:
key=5;
break;
case 0x0b:
key=6;
break;
case 0x07:
key=11;
break;
}
temp=P3;
// P1_1=~P1_1;
if((key>=0) && (key<10))
{
if(keycount<6)
{
getps[keycount]=key;
dispbuf[keycount+1]=19;
}
keycount++;
if(keycount==6)
{
keycount=6;
}
else if(keycount>6)
{
keycount=6;
keyoverflag=1;//key overflow
}
}
else if(key==12)//delete key
{
if(keycount>0)
{
keycount--;
getps[keycount]=0;
dispbuf[keycount+1]=16;
}
else
{
keyoverflag=1;
}
}
else if(key==15)//enter key
{
if(keycount!=pslen)
{
errorflag=1;
rightflag=0;
second3=0;
}
else
{
for(i=0;i<keycount;i++)
{
if(getps[i]!=ps[i])
{
i=keycount;
errorflag=1;
rightflag=0;
second3=0;
goto a4;
}
}
errorflag=0;
rightflag=1;
a4: i=keycount;
}
}
temp=temp & 0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
}
keyoverflag=0;
}
}
P3=0xff;
P3_6=0;
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
for(i=10;i>0;i--)
for(j=248;j>0;j--);
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
key=1;
break;
case 0x0d:
key=2;
break;
case 0x0b:
key=3;
break;
case 0x07:
key=12;
break;
}
temp=P3;
// P1_1=~P1_1;
if((key>=0) && (key<10))
{
if(keycount<6)
{
getps[keycount]=key;
dispbuf[keycount+1]=19;
}
keycount++;
if(keycount==6)
{
keycount=6;
}
else if(keycount>6)
{
keycount=6;
keyoverflag=1;//key overflow
}
}
else if(key==12)//delete key
{
if(keycount>0)
{
keycount--;
getps[keycount]=0;
dispbuf[keycount+1]=16;
}
else
{
keyoverflag=1;
}
}
else if(key==15)//enter key
{
if(keycount!=pslen)
{
errorflag=1;
rightflag=0;
second3=0;
}
else
{
for(i=0;i<keycount;i++)
{
if(getps[i]!=ps[i])
{
i=keycount;
errorflag=1;
rightflag=0;
second3=0;
goto a3;
}
}
errorflag=0;
rightflag=1;
a3: i=keycount;
}
}
temp=temp & 0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
}
keyoverflag=0;
}
}
P3=0xff;
P3_7=0;
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
for(i=10;i>0;i--)
for(j=248;j>0;j--);
temp=P3;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
key=0;
break;
case 0x0d:
key=13;
break;
case 0x0b:
key=14;
break;
case 0x07:
key=15;
break;
}
temp=P3;
// P1_1=~P1_1;
if((key>=0) && (key<10))
{
if(keycount<6)
{
getps[keycount]=key;
dispbuf[keycount+1]=19;
}
keycount++;
if(keycount==6)
{
keycount=6;
}
else if(keycount>6)
{
keycount=6;
keyoverflag=1;//key overflow
}
}
else if(key==12)//delete key
{
if(keycount>0)
{
keycount--;
getps[keycount]=0;
dispbuf[keycount+1]=16;
}
else
{
keyoverflag=1;
}
}
else if(key==15)//enter key
{
if(keycount!=pslen)
{
errorflag=1;
rightflag=0;
second3=0;
}
else
{
for(i=0;i<keycount;i++)
{
if(getps[i]!=ps[i])
{
i=keycount;
errorflag=1;
rightflag=0;
second3=0;
goto a2;
}
}
errorflag=0;
rightflag=1;
a2: i=keycount;
}
}
temp=temp & 0x0f;
while(temp!=0x0f)
{
temp=P3;
temp=temp & 0x0f;
}
keyoverflag=0;
}
}
}
}
void t0(void) interrupt 1 using 0
{
TH0=(65536-500)/256;
TL0=(65536-500)%256;
flashcount++;
if(flashcount==8)
{
flashcount=0;
P0=dispcode[dispbuf[dispcount]];
P2=dispbit[dispcount];
dispcount++;
if(dispcount==8)
{
dispcount=0;
}
}
if((errorflag==1) && (rightflag==0))
{
bb++;
if(bb==800)
{
bb=0;
alarmflag=~alarmflag;
}
if(alarmflag==1)//sound alarm signal
{
P1_7=~P1_7;
}
aa++;
if(aa==800)//light alarm signal
{
aa=0;
P1_0=~P1_0;
}
second3++;
if(second3==6400)
{
second3=0;
errorflag=0;
rightflag=0;
alarmflag=0;
bb=0;
aa=0;
}
}
else if((errorflag==0) && (rightflag==1))
{
P1_0=0;
cc++;
if(cc<1000)
{
okflag=1;
}
else if(cc<2000)
{
okflag=0;
}
else
{
errorflag=0;
rightflag=0;
P1_7=1;
cc=0;
oka=0;
okb=0;
okflag=0;
P1_0=1;
}
if(okflag==1)
{
oka++;
if(oka==2)
{
oka=0;
P1_7=~P1_7;
}
}
else
{
okb++;
if(okb==3)
{
okb=0;
P1_7=~P1_7;
}
}
}
if(keyoverflag==1)
{
P1_7=~P1_7;
}
}
一周热门 更多>