利用矩阵按键 蜂鸣器 数码管 定时器制作的一款电子锁
输入密码时显示为8.
密码正确显示-------------
密码错误显示ERROR
每次正确和错误蜂鸣器都会有不同的响声
错误达到三次键盘会被锁定20秒
在锁定中如果还有按键按下 则锁定时间重置
源代码如下:
- #include <reg52.h>
- #include <stdlib.h>
- #define GPIO_KEY P1
- #define num 8 //密码位数
- sbit bee=P1^5;
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- unsigned int answer[8]={1,2,3,4,5,6,7,8}; //已设置密码
- unsigned int int_answer[8]={0,0,0,0,0,0,0,0}; //键盘输入的密码
- int key=-1; //键值
- unsigned int error_times=0; //已错误次数
- unsigned char key_flag=0,a_flag=-1;
- unsigned int wei[8]={0};
- unsigned int T0Count=0;
- int *as;
- bit timer_flag=0;
- bit stop_flag=0;
- unsigned int code smgduan[]={0x00,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
- /*0x3f , 0x06 , 0x5b , 0x4f , 0x66 , 0x6d
- 0 1 2 3 4 5
- 0x7d , 0x07 , 0x7f , 0x6f , 0x77 , 0x7c
- 6 7 8 9 A B
- 0x39 , 0x5e , 0x79 , 0x71 , 0x00
- C D E F NULL
- */
- void Timer0Init(void) //10毫秒@11.0592MHz
- {
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0x01; //设置定时器模式
- TL0 = 0x00; //设置定时初值
- TH0 = 0xDC; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- ET0=1;
- EA=1;
-
- }
- void delay(unsigned int a)
- {
- while(a--);
- }
- int keyDown()
- {
- unsigned int a=0;
- GPIO_KEY=0x0f;
- if(GPIO_KEY!=0x0f)
- {
- delay(1000);
-
- if(GPIO_KEY!=0x0f)
- {
- GPIO_KEY=0x0f;
- switch(GPIO_KEY)
- {
- case 0x07:key=0;break;
- case 0x0b:key=1;break;
- case 0x0d:key=2;break;
- case 0x0e:key=3;break;
- }
- GPIO_KEY=0xf0;
- switch(GPIO_KEY)
- {
- case 0x70:key=key;break;
- case 0xb0:key=key+4;break;
- case 0xd0:key=key+8;break;
- case 0xe0:key=key+12;break;
- }
- while((a<50)&&(GPIO_KEY!=0xf0))
- {
- delay(1000);
- a++;
- }
- key_flag=1;
- }
- }
- return key;
- }
- void display() //数码管显示
- {
- LSA=0;LSB=0;LSC=0;P0=smgduan[wei[7]];delay(10);P0=0x00;
- LSA=1;LSB=0;LSC=0;P0=smgduan[wei[6]];delay(10);P0=0x00;
- LSA=0;LSB=1;LSC=0;P0=smgduan[wei[5]];delay(10);P0=0x00;
- LSA=1;LSB=1;LSC=0;P0=smgduan[wei[4]];delay(10);P0=0x00;
- LSA=0;LSB=0;LSC=1;P0=smgduan[wei[3]];delay(10);P0=0x00;
- LSA=1;LSB=0;LSC=1;P0=smgduan[wei[2]];delay(10);P0=0x00;
- LSA=0;LSB=1;LSC=1;P0=smgduan[wei[1]];delay(10);P0=0x00;
- LSA=1;LSB=1;LSC=1;P0=smgduan[wei[0]];delay(10);P0=0x00;
- }
- void display_no()
- {
- wei[0]=15;
- wei[1]=11;
- wei[2]=11;
- wei[3]=1;
- wei[4]=11;
- wei[5]=0;
- wei[6]=0;
- wei[7]=0;
- display();
- }
- void display_yes()
- {
- int i;
- for(i=0;i<8;i++)
- {
- wei[i]=17;
- }
- display();
- }
- /*void display_ans()
- {
-
- wei[7]=int_answer[0];
- wei[6]=int_answer[1];
- wei[5]=int_answer[2];
- wei[4]=int_answer[3];
- wei[3]=int_answer[4];
- wei[2]=int_answer[5];
- wei[1]=int_answer[6];
- wei[0]=int_answer[7];
-
- display();
- }*/
- void beep(unsigned int mode)
- {
- int k=0;
- if(mode==1) //错误提示音
- {
- do
- {
- bee=~bee;
- delay(200);
- k++;
- }while(k<=1000);
- k=0;
- }
- else if(mode==2) //正确提示音
- {
- do
- {
- bee=~bee;
- delay(100);
- k++;
- }while(k<=1000);
- k=0;
- }
- }
- void stop() //暂停键盘三秒
- {
- stop_flag=1;
- do
- {
- key=-1;
- keyDown();
- if(key!=-1)
- T0Count=0;
- wei[0]=10;
- display();
- }while(timer_flag==0);
- }
- /*void convert()
- {
- switch(key)
- {
- case 0:key_flag=0;break;
- case 1:key=0;key_flag=1;break;
- case 2:key_flag=0;break;
- case 3:key_flag=0;break;
- case 4:key=1;key_flag=1;break;
- case 5:key=2;key_flag=1;break;
- case 6:key=3;key_flag=1;break;
- case 7:key_flag=0;break;
- case 8:key=4;key_flag=1;break;
- case 9:key=5;key_flag=1;break;
- case 10:key=6;key_flag=1;break;
- case 11:key=10;key_flag=0;break;
- case 12:key=7;key_flag=1;break;
- case 13:key=8;key_flag=1;break;
- case 14:key=9;key_flag=1;break;
- case 15:key_flag=0;break;
- }
- }*/
- void no()
- {
- int j=0;
- error_times++; //错误次数+1
- display_no();
- beep(1);
- for(j=0;j<num;j++)
- {
- int_answer[j]=0;
- }
- }
- void yes()
- {
- beep(2);
- while(1)
- {
- display_yes();
- keyDown();
- if(key==3)
- {
- break;
- }
- }
-
- }
- void main()
- {
- Timer0Init();
- while(1)
- {
- if(error_times<3)
- {
-
- unsigned int i;
- i=0;
- wei[0]=16;
- do
- {
- key=-1;
- if((int_answer[i]=keyDown())!=-1)
- i++;
- }while(key!=15);
- if(key==15)
- {
- for(i=0;i<num;i++)
- {
- if(answer[i]!=int_answer[i])
- {
- a_flag=1;
- break;
- }
- else
- a_flag=0;
- }
- }
- if(a_flag==1)
- {
- no();
- }
- else
- {
- yes();
- }
- }
- else
- {
- stop();
- error_times=0;
- }
-
- }
- }
- void Timer0_Routine() interrupt 1 //10ms溢出一次
- {
- TL0 = 0x00; //设置定时初值
- TH0 = 0xDC;
- T0Count++;
- display();
- if(stop_flag==1)
- {
- T0Count=0;
- if(T0Count==2000)
- {
- T0Count=0;
- timer_flag=1;
- stop_flag=0;
- }
- }
-
- }
复制代码 Keil代码工程51hei附件下载:
电子锁.7z
(21.11 KB, 下载次数: 40)
|