该密码锁控制器 ,键盘上有0-9个数字按键,功能键:确认和取消等,可设置复合键。密码的位数及密码可以任意设定,,当输入数字和设置的密码相同的时候,锁打开,否则无法打开。采用IIC通信方式,密码锁的密码掉电不丢失。
#include
#include
#define uint unsigned int
#define uchar unsigned char
uchar old1,old2,old3,old4,old5,old6;
uchar new1,new2,new3,new4,new5,new6;
uchar a=10,b=10,c=10,d=10,e=10,f=10;
uchar wei,key,temp;
bit allow,genggai,ok,wanbi,retry,close;
sbit dula=P2^6;
sbit wela=P2^7;
sbit beep=P2^3;
sbit led=P1^0;
sbit sda=P2^0;
sbit scl=P2^1;
unsigned char code table[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x08};
/*****************i2c芯片24C02存储器驱动程序************************************/
void nop()
{
_nop_();
_nop_();
}
void delay1(unsigned int m)
{ unsigned int n;
for(n=0;nvoid init()
{
scl=1;
nop();
sda=1;
nop();
}
void start()
{
sda=1;
nop();
scl=1;
nop();
sda=0;
nop();
scl=0;
nop();
}
void stop()
{
sda=0;
nop();
scl=1;
nop();
sda=1;
nop();
}
void writebyte(unsigned char j)
{
unsigned char i,temp;
temp=j;
for (i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
nop();
sda=CY;
nop();
scl=1;
nop();
}
scl=0;
nop();
sda=1;
nop();
}
unsigned char readbyte()
{
unsigned char i,j,k=0;
scl=0; nop(); sda=1;
for (i=0;i<8;i++)
{
nop(); scl=1; nop();
if(sda==1)
j=1;
else
j=0;
k=(k<<1)|j;
scl=0;
}
nop();
return(k);
}
void clock()
{
unsigned char i=0;
scl=1;
nop();
while((sda==1)&&(i<255))
i++;
scl=0;
nop();
}
unsigned char read24c02(unsigned char address)
{
unsigned char i;
start();
writebyte(0xa0);
clock();
writebyte(address);
clock();
start();
writebyte(0xa1);
clock();
i=readbyte();
stop();
delay1(100);
return(i);
}
void write24c02(unsigned char address,unsigned char info)
{
start();
writebyte(0xa0);
clock();
writebyte(address);
clock();
writebyte(info);
clock();
stop();
delay1(5000);
}
/****************************密码锁程序模块********************************************************/
void delay(unsigned char i)
{
uchar j,k;
for(j=i;j>0;j--)
for(k=114;k>0;k--);
}
void display(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f)
{
dula=0;
P0=table[a];
dula=1;
dula=0;
wela=0;
P0=0xfe;
wela=1;
wela=0;
delay(5);
P0=table[b];
dula=1;
dula=0;
P0=0xfd;
wela=1;
wela=0;
delay(5);
P0=table[c];
dula=1;
dula=0;
P0=0xfb;
wela=1;
wela=0;
delay(5);
P0=table[d];
dula=1;
dula=0;
P0=0xf7;
wela=1;
wela=0;
delay(5);
P0=table[e];
dula=1;
dula=0;
P0=0xef;
wela=1;
wela=0;
delay(5);
P0=table[f];
dula=1;
dula=0;
P0=0xdf;
wela=1;
wela=0;
delay(5);
}
void keyscan()
{
{
P3=0xfe;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
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=P3;
temp=temp&0xf0;
led=0;
}
led=1;
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
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=P3;
temp=temp&0xf0;
led=0;
}
led=1;
}
}
P3=0xfb;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
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=P3;
temp=temp&0xf0;
led=0;
}
led=1;
}
}
P3=0xf7;
temp=P3;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(10);
if(temp!=0xf0)
{
temp=P3;
switch(temp)
{
case 0xe7:retry=1;break;
case 0xd7:close=1;break;
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
led=0;
}
led=1;
}
}
}
}
void shumima()
{
if(!wanbi)
{
switch(wei)
{
case 1:new1=key;
if(!allow) a=11;
else a=key; break;
case 2:new2=key;
if(a==11) b=11;
else b=key; break;
case 3:new3=key;
if(a==11) c=11;
else c=key; break;
case 4:new4=key;
if(a==11) d=11;
else d=key; break;
case 5:new5=key;
if(a==11) e=11;
else e=key; break;
case 6:new6=key;
if(a==11) f=11;
else f=key;
wanbi=1; break;
}
}
}
void yanzheng()
{
if(wanbi)
{
if((new1==old1)&(new2==old2)&(new3==old3)&(new4==old4)&(new5==old5)&(new6==old6))
allow=1;
}
}
void main()
{
init();
old1=read24c02(110);
old2=read24c02(111);
old3=read24c02(112);
old4=read24c02(113);
old5=read24c02(114);
old6=read24c02(115);
while(1)
{
keyscan();
shumima();
yanzheng();
if(allow)
{
P1=0x00;
if(!genggai)
wanbi=0;
}
if(genggai)
{
if(allow)
{
while(!wanbi)
{
keyscan();
shumima();
if(retry|close)
{ wanbi=1;
break;
}
display(a,b,c,d,e,f);
}
}
}
if(ok)
{
ok=0;
wei=0;
genggai=0;
old1=new1;old2=new2;old3=new3;
old4=new4;old5=new5;old6=new6;
write24c02(110,old1);
write24c02(111,old2);
write24c02(112,old3);
write24c02(113,old4);
write24c02(114,old5);
write24c02(115,old6);
a=10;b=10;c=10;d=10;e=10;f=10;
}
if(retry)
{
retry=0;
wei=0;
wanbi=0;
a=10;b=10;c=10;d=10;e=10;f=10;
new1=0;new2=0;new3=0;new4=0;new5=0;new6=0;
}
if(close)
{
close=0;
genggai=0;
wei=0;
wanbi=0;
allow=0;
P1=0xff;
a=10;b=10;c=10;d=10;e=10;f=10;
new1=0;new2=0;new3=0;new4=0;new5=0;new6=0;
}
display(a,b,c,d,e,f);
}
}