|
密碼鎖
輸入初始密碼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)
2025-12-4 16:12 上傳
點擊文件名下載附件
|