|
密码锁
输入初始密码666解锁成功,连续三次输错报警
单片机源程序如下:
- #include <reg51.h>
- #include <type.h>
- #include <intrins.h>
- #include <LCD12864.h>
- #include <math.h>
- #define GPIO_KEY P1 //P1口接矩阵按键
- long unsigned int password1 = 6666; //设置初始密码
- long unsigned int password2 = 0; //存储用户输入密码
- long unsigned int t = 0;
- uint8 d = 0,rst=0,err=0,rm=0,rg=0;//输入位数d,密码重置信号rst
- sbit lock = P2^7;
- sbit buzzer = P2^6;
- void delay(uint16 i)
- {
- while(i--);
- }
- void KeyScan()
- {
- uint8 a=0,k=0;
- GPIO_KEY=0x0f;
- if(GPIO_KEY!=0x0f)//读取按键是否按下
- {
- delay(1000);//延时10ms进行消抖
- if(GPIO_KEY!=0x0f)//再次检测键盘是否按下
- {
- //检测列数据
- GPIO_KEY=0X0F;
- switch(GPIO_KEY)
- {
- case(0X07): k=0;break;
- case(0X0b): k=1;break;
- case(0X0d): k=2;break;
- case(0X0e): k=3;break;
- }
- //检测行数据
- GPIO_KEY=0XF0;
- switch(GPIO_KEY)
- {
- case(0X70): k=k;break;
- case(0Xb0): k=k+4;break;
- case(0Xd0): k=k+8;break;
- case(0Xe0): k=k+12;break;
- }
- while((a<50)&&(GPIO_KEY!=0xf0)) //检测按键松手检测
- {
- delay(1000);
- a++;
- }
-
- if(k==13&&d>0)
- { rg=1;
- if(rm==1)t=password2%10;
- if(rm==2)t=password2/10%10;
- if(rm==3)t=password2/100;
- (rm==3)?show_ch(2,5,8+8*(d-1),ch1):show_ch(2,5,8+8*(d-rm-1),ch1);
- if(rm>0)
- { show_ch(2,5,8+8*(d-rm),ch+16*t);
- (rm==3)?rm=0:0;
- }
- rm++;
- }
- else if(rg==0)
- { switch(k)
- {
- case 0: show_ch(2,5,8+8*d,ch+16*0);d++;password2 = password2*10+0;
- if(rst){password1 = password2;}break;
- case 1: show_ch(2,5,8+8*d,ch+16*1);d++;password2 = password2*10+1;
- if(rst){password1 = password2;}break;
- case 2: show_ch(2,5,8+8*d,ch+16*2);d++;password2 = password2*10+2;
- if(rst){password1 = password2;}break;
- case 3: show_ch(2,5,8+8*d,ch+16*3);d++;password2 = password2*10+3;
- if(rst){password1 = password2;}break;
- case 4: show_ch(2,5,8+8*d,ch+16*4);d++;password2 = password2*10+4;
- if(rst){password1 = password2;}break;
- case 5: show_ch(2,5,8+8*d,ch+16*5);d++;password2 = password2*10+5;
- if(rst){password1 = password2;}break;
- case 6: show_ch(2,5,8+8*d,ch+16*6);d++;password2 = password2*10+6;
- if(rst){password1 = password2;}break;
- case 7: show_ch(2,5,8+8*d,ch+16*7);d++;password2 = password2*10+7;
- if(rst){password1 = password2;}break;
- case 8: show_ch(2,5,8+8*d,ch+16*8);d++;password2 = password2*10+8;
- if(rst){password1 = password2;}break;
- case 9:show_ch(2,5,8+8*d,ch+16*9);d++;password2 = password2*10+9;
- if(rst){password1 = password2;}break;
- case 10:break;
- case 11:break;
- case 12:if(lock){
- password1=0;rst = 1;
- show_ch2(1,5,0*16,ch2+128);show_ch2(1,5,1*16,ch2+160);
- show_ch2(1,5,2*16,ch2+64);show_ch2(1,5,3*16,ch2+96);
- show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);
- show_ch(2,5,32,ch1);
- if(rst==1)//蜂鸣器长鸣一声
- {
- rst++;
- buzzer=1;
- delay(20000);
- buzzer=0;
- }
- break;}//重置密码,仅当开锁状态下有效
- //13用于移位
- case 14:if(d>0){show_ch(2,5,8+8*(d-1),ch1);d--;password2 = password2/10;}//删除一位
- if(rst){password1 = password2;}break;
- case 15:if(rst==0)
- {lock=0;password2=0;d=0;err=0;
- show_ch2(1,5,0*16,ch2);show_ch2(1,5,1*16,ch2+32);show_ch2(1,5,2*16,ch2+64);show_ch2(1,5,3*16,ch2+96);//输入密码
- show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);show_ch(2,5,32,ch1);//星号
- show_im(1,1,1*16,image1);
- }break;//清零/重新上锁按键,重设状态下,按键无效
- }
- }
- if(rg==1&&k!=13)
- {
- if(rm==1){password2=password2/10*10+k;show_ch(2,5,8+8*(d-rm),ch+16*(k));}
- if(rm==2){password2=password2/100*100+k*10+password2%10;show_ch(2,5,8+8*(d-rm),ch+16*(k));}
- if(rm==3){password2=k*100+password2%100;show_ch(2,5,8+8*(d-rm),ch+16*(k));}
- rg=0;rm=0;
- }
-
- if(d>3 && rst==0)//输入完毕
- {
- d=0;
- if(password1==password2)
- {
- if(lock==0)//蜂鸣器响两声
- {
- buzzer=1;
- delay(10000);
- buzzer=0;
- delay(8000);
- buzzer=1;
- delay(6000);
- buzzer=0;
- }
-
- lock = 1;
- show_im(1,1,1*16,image2);
- }
-
- else
- {
- err++;
- ClearScreen(0); //清屏
- show_im(1,1,1*16,image1);
- show_ch2(1,5,0*16,ch2+64);show_ch2(1,5,1*16,ch2+96);show_ch2(1,5,2*16,ch2+192);show_ch2(1,5,3*16,ch2+224);//密码错误
- show_ch(2,5,8+8*d,ch+16*err);show_ch2(2,5,1*16,ch2+256);
- delay(50000);delay(100000); //小段延时
- if(err<=2){
- buzzer=1;delay(50000);
- buzzer=0;delay(10000);
- buzzer=1;delay(90000);
- buzzer=0;delay(10000);
- buzzer=1;delay(90000);
- buzzer=0;
- }
- show_ch2(1,5,0*16,ch2);show_ch2(1,5,1*16,ch2+32);show_ch2(1,5,2*16,ch2+64);show_ch2(1,5,3*16,ch2+96);//输入密码
- show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);show_ch(2,5,32,ch1);//星号
-
- } password2=0;
- }
- if(d>3 && rst)//重设完毕
- {
- d=0;password2 = 0;err=0;
- if(rst==2)//蜂鸣器响两声
- {
- rst=0;
- buzzer=1;delay(50000);
- buzzer=0;delay(10000);
- buzzer=1;delay(90000);
- buzzer=0;
- }
- lock = 0;//重设完毕后,重新上锁
- show_ch2(1,5,0*16,ch2);show_ch2(1,5,1*16,ch2+32);show_ch2(1,5,2*16,ch2+64);show_ch2(1,5,3*16,ch2+96);//输入密码
- show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);show_ch(2,5,32,ch1);//星号
- show_im(1,1,1*16,image1);
- }
-
- }
- }
- }
- void main()
- {
- lock = 0;
- buzzer = 0;
- InitLCD();
- ClearScreen(0); //清屏
- Set_line(0);
- show_im(1,1,1*16,image1);
- show_ch2(1,5,0*16,ch2);show_ch2(1,5,1*16,ch2+32);show_ch2(1,5,2*16,ch2+64);show_ch2(1,5,3*16,ch2+96);//输入密码
- show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);show_ch(2,5,32,ch1);//星号
- while(1)
- {
- if(err<3)
- {
- KeyScan();
- }
- else if(err==3)
- { err++ ;
- ClearScreen(0);
- show_im(1,1,1*16,image1);show_im64(2,0,0*16,image3);//显示图片
- show_ch2(1,5,0*16,ch3);show_ch2(1,5,1*16,ch3+32);
- show_ch2(1,5,2*16,ch3+64);show_ch2(1,5,3*16,ch3+96);//系统锁定
- buzzer=1;
- }
- else
- {
- buzzer=1;
- delay(50000);
- buzzer=0;
- delay(50000);
- buzzer=1;
- delay(100000);
- }
-
-
-
- }
- }
复制代码
仿真程序下载:
密码锁仿真.zip
(301.98 KB, 下载次数: 0)
|