基于单片机的电子密码锁
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit beep=P3^7;
uchar code table_d[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code table_w[]={
0x7f,0xbf,0xdf,0xef,
0xf7,0xfb,0xfd,0xfe};
uchar passport[]="11111111"; //密码是11111111
uchar code_in[8]={0}; //输入密码
int times=0,times2=0;
uchar temp,num = 0;
uchar flag3=2,flag4=2;
bit flag1=0,flag2=0; //清除、确认标志位
/*************************矩阵键盘输入函数*********************/
void delay(uint xms) //延迟函数
{
uchar a,b;
for(a=xms;a>0;a--)
for(b=200;b>0;b--);
}
bit pushkey() //判断是否有键按下
{
P0=0xf0;
if(P0!=0xf0)
return(1);
else
return(0);
}
void input() //从键盘上输入指令
{
P0=0xfe;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(50);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=15; //.
break;
case 0xd0: num=1;
break;
case 0xb0: num=4;
break;
case 0x70: num=7;
break;
}
}
}
P0=0xfd;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(50);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=0;
break;
case 0xd0: num=2;
break;
case 0xb0: num=5;
break;
case 0x70: num=8;
break;
}
}
}
P0=0xfb;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(50);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=14; //=
break;
case 0xd0: num=3;
break;
case 0xb0: num=6;
break;
case 0x70: num=9;
break;
}
}
}
P0=0xf7;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(50);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=10; // /
break;
case 0xd0: num=11; //*
break;
case 0xb0: num=12; //-
break;
case 0x70: num=13; //+
break;
}
}
}
delay(1000);
}
void display1()
{
if(flag1==0 && flag3 == 2)
{
if(times>=0)
{
P1=table_d[code_in[0]];
P2=table_w[0];
delay(5);
}
if(times>=1)
{
P1=table_d[code_in[1]];
P2=table_w[1];
delay(5);
}
if(times>=2)
{
P1=table_d[code_in[2]];
P2=table_w[2];
delay(5);
}
if(times>=3)
{
P1=table_d[code_in[3]];
P2=table_w[3];
delay(5);
}
if(times>=4)
{
P1=table_d[code_in[4]];
P2=table_w[4];
delay(5);
}
if(times>=5)
{
P1=table_d[code_in[5]];
P2=table_w[5];
delay(5);
}
if(times>=6)
{
P1=table_d[code_in[6]];
P2=table_w[6];
delay(5);
}
if(times>=7)
{
P1=table_d[code_in[7]];
P2=table_w[7];
delay(5);
}
}
if(flag1==1 && flag3 == 2)
{
P1=0xff;
P2=0xff;
flag1 = 0;
flag2 = 0;
flag3 = 2;
flag4 = 2;
times = 0;
}
if(flag3 == 0)//
{
P1=0x71;
//P2=0x00;
P2=0xc0;
flag4 = 2;
}
if(flag3 == 1)
{
P1=0x77;
P2=0xc0;
flag4 = 2;
}
}
void timer_init(void) //定时器初始化
{
TMOD=0x11;
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
uchar i,n=0;
timer_init();
while(1)
{
if(pushkey())
{
input();
if(num<=9 && times<=7)
{
code_in[times]=num;
times++;
}
if(num==10)
{
flag1 = 1;
flag2 = 0;
for(i=0;i<=7;i++)
code_in[i]=0;
}
if(num==11)
{
flag1 = 0;
flag2 = 1;
}
flag3 = 2;
flag4 = 2;
if(flag2==1)
{
for(i=0;i<=7;i++)
{
if(code_in[i]+48 != passport[i])
{
flag3 = 0;
flag4 = 0;
times2 = 900;
while(times2)
{
if(times2<=200)
beep = ~beep;
delay(1);
if(times2>200 && times<300)
{}
if(times2>300 && times<400)
beep = ~beep;
delay(1);
if(times2>400 && times<700)
{}
if(times2>700 && times<900)
beep = ~beep;
delay(1);
times2--;
}
beep = 1;
break;
}
else
n++;
}
if(n == 8)
{
flag3 = 1;
times2 = 900;
while(times2)
{
beep = ~beep;
delay(1);
times2--;
}
beep = 1;
}
}
}
}
}
void T0_timer() interrupt 1 //中断函数里处理频率,并且显示出来
{
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
display1();
}
希望大神来解释一些程序,重点是flag1,flag2,flag3,flag4中有关的程序,还有最后几段程序,谢谢
#define uchar unsigned char
#define uint unsigned int
sbit beep=P3^7;
uchar code table_d[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code table_w[]={
0x7f,0xbf,0xdf,0xef,
0xf7,0xfb,0xfd,0xfe};
uchar passport[]="11111111"; //密码是11111111
uchar code_in[8]={0}; //输入密码
int times=0,times2=0;
uchar temp,num = 0;
uchar flag3=2,flag4=2;
bit flag1=0,flag2=0; //清除、确认标志位
/*************************矩阵键盘输入函数*********************/
void delay(uint xms) //延迟函数
{
uchar a,b;
for(a=xms;a>0;a--)
for(b=200;b>0;b--);
}
bit pushkey() //判断是否有键按下
{
P0=0xf0;
if(P0!=0xf0)
return(1);
else
return(0);
}
void input() //从键盘上输入指令
{
P0=0xfe;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(50);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=15; //.
break;
case 0xd0: num=1;
break;
case 0xb0: num=4;
break;
case 0x70: num=7;
break;
}
}
}
P0=0xfd;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(50);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=0;
break;
case 0xd0: num=2;
break;
case 0xb0: num=5;
break;
case 0x70: num=8;
break;
}
}
}
P0=0xfb;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(50);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=14; //=
break;
case 0xd0: num=3;
break;
case 0xb0: num=6;
break;
case 0x70: num=9;
break;
}
}
}
P0=0xf7;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(50);
if(temp!=0xf0)
{
switch(temp)
{
case 0xe0: num=10; // /
break;
case 0xd0: num=11; //*
break;
case 0xb0: num=12; //-
break;
case 0x70: num=13; //+
break;
}
}
}
delay(1000);
}
void display1()
{
if(flag1==0 && flag3 == 2)
{
if(times>=0)
{
P1=table_d[code_in[0]];
P2=table_w[0];
delay(5);
}
if(times>=1)
{
P1=table_d[code_in[1]];
P2=table_w[1];
delay(5);
}
if(times>=2)
{
P1=table_d[code_in[2]];
P2=table_w[2];
delay(5);
}
if(times>=3)
{
P1=table_d[code_in[3]];
P2=table_w[3];
delay(5);
}
if(times>=4)
{
P1=table_d[code_in[4]];
P2=table_w[4];
delay(5);
}
if(times>=5)
{
P1=table_d[code_in[5]];
P2=table_w[5];
delay(5);
}
if(times>=6)
{
P1=table_d[code_in[6]];
P2=table_w[6];
delay(5);
}
if(times>=7)
{
P1=table_d[code_in[7]];
P2=table_w[7];
delay(5);
}
}
if(flag1==1 && flag3 == 2)
{
P1=0xff;
P2=0xff;
flag1 = 0;
flag2 = 0;
flag3 = 2;
flag4 = 2;
times = 0;
}
if(flag3 == 0)//
{
P1=0x71;
//P2=0x00;
P2=0xc0;
flag4 = 2;
}
if(flag3 == 1)
{
P1=0x77;
P2=0xc0;
flag4 = 2;
}
}
void timer_init(void) //定时器初始化
{
TMOD=0x11;
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
EA=1;
ET0=1;
TR0=1;
}
void main()
{
uchar i,n=0;
timer_init();
while(1)
{
if(pushkey())
{
input();
if(num<=9 && times<=7)
{
code_in[times]=num;
times++;
}
if(num==10)
{
flag1 = 1;
flag2 = 0;
for(i=0;i<=7;i++)
code_in[i]=0;
}
if(num==11)
{
flag1 = 0;
flag2 = 1;
}
flag3 = 2;
flag4 = 2;
if(flag2==1)
{
for(i=0;i<=7;i++)
{
if(code_in[i]+48 != passport[i])
{
flag3 = 0;
flag4 = 0;
times2 = 900;
while(times2)
{
if(times2<=200)
beep = ~beep;
delay(1);
if(times2>200 && times<300)
{}
if(times2>300 && times<400)
beep = ~beep;
delay(1);
if(times2>400 && times<700)
{}
if(times2>700 && times<900)
beep = ~beep;
delay(1);
times2--;
}
beep = 1;
break;
}
else
n++;
}
if(n == 8)
{
flag3 = 1;
times2 = 900;
while(times2)
{
beep = ~beep;
delay(1);
times2--;
}
beep = 1;
}
}
}
}
}
void T0_timer() interrupt 1 //中断函数里处理频率,并且显示出来
{
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
display1();
}
希望大神来解释一些程序,重点是flag1,flag2,flag3,flag4中有关的程序,还有最后几段程序,谢谢