找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 507|回复: 1
打印 上一主题 下一主题
收起左侧

51单片机密码锁程序与Proteus仿真原理图 连续三次输错报警

[复制链接]
跳转到指定楼层
楼主
密码锁

输入初始密码666解锁成功,连续三次输错报警



单片机源程序如下:
  1. #include <reg51.h>
  2. #include <type.h>
  3. #include <intrins.h>
  4. #include <LCD12864.h>
  5. #include <math.h>
  6. #define GPIO_KEY P1 //P1口接矩阵按键

  7. long unsigned int password1 = 6666; //设置初始密码
  8. long unsigned int password2 = 0; //存储用户输入密码
  9. long unsigned int t = 0;
  10. uint8 d = 0,rst=0,err=0,rm=0,rg=0;//输入位数d,密码重置信号rst
  11. sbit lock = P2^7;
  12. sbit buzzer = P2^6;
  13. void delay(uint16 i)
  14. {
  15.         while(i--);        
  16. }

  17. void KeyScan()
  18. {
  19.         uint8 a=0,k=0;
  20.         GPIO_KEY=0x0f;
  21.         if(GPIO_KEY!=0x0f)//读取按键是否按下
  22.         {
  23.                 delay(1000);//延时10ms进行消抖
  24.                 if(GPIO_KEY!=0x0f)//再次检测键盘是否按下
  25.                 {        
  26.                         //检测列数据
  27.                         GPIO_KEY=0X0F;
  28.                         switch(GPIO_KEY)
  29.                         {
  30.                                 case(0X07):        k=0;break;
  31.                                 case(0X0b):        k=1;break;
  32.                                 case(0X0d): k=2;break;
  33.                                 case(0X0e):        k=3;break;
  34.                         }
  35.                         //检测行数据
  36.                         GPIO_KEY=0XF0;
  37.                         switch(GPIO_KEY)
  38.                         {
  39.                                 case(0X70):        k=k;break;
  40.                                 case(0Xb0):        k=k+4;break;
  41.                                 case(0Xd0): k=k+8;break;
  42.                                 case(0Xe0):        k=k+12;break;
  43.                         }
  44.                         while((a<50)&&(GPIO_KEY!=0xf0))         //检测按键松手检测
  45.                         {
  46.                                 delay(1000);
  47.                                 a++;
  48.                         }
  49.                         
  50.                         if(k==13&&d>0)
  51.                         {        rg=1;        
  52.                                 if(rm==1)t=password2%10;
  53.                                 if(rm==2)t=password2/10%10;
  54.                                 if(rm==3)t=password2/100;
  55.                                 (rm==3)?show_ch(2,5,8+8*(d-1),ch1):show_ch(2,5,8+8*(d-rm-1),ch1);
  56.                                 if(rm>0)
  57.                                         { show_ch(2,5,8+8*(d-rm),ch+16*t);
  58.                                           (rm==3)?rm=0:0;
  59.                                         }
  60.                                 rm++;
  61.                         }
  62.                         else if(rg==0)
  63.                         {  switch(k)
  64.                                 {
  65.                                         case 0: show_ch(2,5,8+8*d,ch+16*0);d++;password2 = password2*10+0;
  66.                                                                         if(rst){password1 = password2;}break;
  67.                                         case 1: show_ch(2,5,8+8*d,ch+16*1);d++;password2 = password2*10+1;
  68.                                                                         if(rst){password1 = password2;}break;
  69.                                         case 2: show_ch(2,5,8+8*d,ch+16*2);d++;password2 = password2*10+2;
  70.                                                                         if(rst){password1 = password2;}break;
  71.                                         case 3: show_ch(2,5,8+8*d,ch+16*3);d++;password2 = password2*10+3;
  72.                                                                         if(rst){password1 = password2;}break;        
  73.                                         case 4: show_ch(2,5,8+8*d,ch+16*4);d++;password2 = password2*10+4;
  74.                                                                         if(rst){password1 = password2;}break;
  75.                                         case 5: show_ch(2,5,8+8*d,ch+16*5);d++;password2 = password2*10+5;
  76.                                                                         if(rst){password1 = password2;}break;
  77.                                         case 6: show_ch(2,5,8+8*d,ch+16*6);d++;password2 = password2*10+6;
  78.                                                                         if(rst){password1 = password2;}break;
  79.                                         case 7: show_ch(2,5,8+8*d,ch+16*7);d++;password2 = password2*10+7;
  80.                                                                         if(rst){password1 = password2;}break;
  81.                                         case 8: show_ch(2,5,8+8*d,ch+16*8);d++;password2 = password2*10+8;
  82.                                                                         if(rst){password1 = password2;}break;
  83.                                         case 9:show_ch(2,5,8+8*d,ch+16*9);d++;password2 = password2*10+9;
  84.                                                                         if(rst){password1 = password2;}break;
  85.                                         case 10:break;
  86.                                         case 11:break;                        
  87.                                         case 12:if(lock){
  88.                                                                         password1=0;rst = 1;
  89.                                                                         show_ch2(1,5,0*16,ch2+128);show_ch2(1,5,1*16,ch2+160);
  90.                                                                         show_ch2(1,5,2*16,ch2+64);show_ch2(1,5,3*16,ch2+96);
  91.                                                                         show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);
  92.                                                                         show_ch(2,5,32,ch1);
  93.                                                                                 if(rst==1)//蜂鸣器长鸣一声
  94.                                                                                 {
  95.                                                                                         rst++;
  96.                                                                                         buzzer=1;
  97.                                                                                         delay(20000);
  98.                                                                                         buzzer=0;
  99.                                                                                 }
  100.                                                                         break;}//重置密码,仅当开锁状态下有效
  101.                                                 //13用于移位                                                                                                                                
  102.                                         case 14:if(d>0){show_ch(2,5,8+8*(d-1),ch1);d--;password2 = password2/10;}//删除一位
  103.                                                                         if(rst){password1 = password2;}break;
  104.                                         case 15:if(rst==0)
  105.                                                 {lock=0;password2=0;d=0;err=0;
  106.                                                 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);//输入密码
  107.                                                 show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);show_ch(2,5,32,ch1);//星号
  108.                                                 show_im(1,1,1*16,image1);
  109.                                                 }break;//清零/重新上锁按键,重设状态下,按键无效
  110.                                    }
  111.                                 }
  112.                         if(rg==1&&k!=13)
  113.                         {
  114.                                 if(rm==1){password2=password2/10*10+k;show_ch(2,5,8+8*(d-rm),ch+16*(k));}
  115.                                 if(rm==2){password2=password2/100*100+k*10+password2%10;show_ch(2,5,8+8*(d-rm),ch+16*(k));}
  116.                                 if(rm==3){password2=k*100+password2%100;show_ch(2,5,8+8*(d-rm),ch+16*(k));}
  117.                                 rg=0;rm=0;
  118.                         }
  119.                         
  120.                         if(d>3 && rst==0)//输入完毕
  121.                         {
  122.                                 d=0;
  123.                                 if(password1==password2)
  124.                                 {
  125.                                         if(lock==0)//蜂鸣器响两声
  126.                                         {
  127.                                                 buzzer=1;
  128.                                                 delay(10000);
  129.                                                 buzzer=0;
  130.                                                 delay(8000);
  131.                                                 buzzer=1;
  132.                                                 delay(6000);
  133.                                                 buzzer=0;
  134.                                         }
  135.                                        
  136.                                         lock = 1;
  137.                                         show_im(1,1,1*16,image2);
  138.                                 }
  139.                                 
  140.                                 else
  141.                                 {
  142.                                         err++;
  143.                                         ClearScreen(0);        //清屏
  144.                             show_im(1,1,1*16,image1);
  145.                                                 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);//密码错误
  146.                                             show_ch(2,5,8+8*d,ch+16*err);show_ch2(2,5,1*16,ch2+256);
  147.                                                 delay(50000);delay(100000);        //小段延时
  148.                                         if(err<=2){
  149.                                                 buzzer=1;delay(50000);
  150.                                                 buzzer=0;delay(10000);
  151.                                                 buzzer=1;delay(90000);
  152.                                                 buzzer=0;delay(10000);
  153.                                                 buzzer=1;delay(90000);
  154.                                                 buzzer=0;
  155.                                         }
  156.                                                 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);//输入密码
  157.                                                 show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);show_ch(2,5,32,ch1);//星号
  158.                                        
  159.                             } password2=0;
  160.                         }   
  161.                         if(d>3 && rst)//重设完毕
  162.                         {
  163.                                         d=0;password2 = 0;err=0;
  164.                                 if(rst==2)//蜂鸣器响两声
  165.                                         {
  166.                                                 rst=0;
  167.                                                 buzzer=1;delay(50000);
  168.                                                 buzzer=0;delay(10000);
  169.                                                 buzzer=1;delay(90000);
  170.                                                 buzzer=0;
  171.                                         }
  172.                                         lock = 0;//重设完毕后,重新上锁
  173.                                         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);//输入密码
  174.                                         show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);show_ch(2,5,32,ch1);//星号
  175.                                         show_im(1,1,1*16,image1);
  176.                         }
  177.                         
  178.                  }
  179.          }
  180. }


  181. void main()
  182. {
  183.         lock = 0;
  184.         buzzer = 0;
  185.         InitLCD();
  186.         ClearScreen(0);        //清屏
  187.         Set_line(0);
  188.         show_im(1,1,1*16,image1);
  189.         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);//输入密码
  190.         show_ch(2,5,8,ch1);show_ch(2,5,16,ch1);show_ch(2,5,24,ch1);show_ch(2,5,32,ch1);//星号
  191.         while(1)
  192.         {
  193.                 if(err<3)
  194.                 {
  195.                         KeyScan();
  196.                 }
  197.                 else if(err==3)
  198.                 {   err++  ;
  199.                         ClearScreen(0);
  200.                         show_im(1,1,1*16,image1);show_im64(2,0,0*16,image3);//显示图片
  201.                         show_ch2(1,5,0*16,ch3);show_ch2(1,5,1*16,ch3+32);
  202.                         show_ch2(1,5,2*16,ch3+64);show_ch2(1,5,3*16,ch3+96);//系统锁定
  203.                         buzzer=1;
  204.                 }
  205.                 else
  206.                 {
  207.                         buzzer=1;
  208.                         delay(50000);
  209.                         buzzer=0;
  210.                         delay(50000);
  211.                         buzzer=1;
  212.                         delay(100000);
  213.                 }
  214.                
  215.                         
  216.                
  217.         }
  218. }
复制代码

仿真程序下载: 密码锁仿真.zip (301.98 KB, 下载次数: 0)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:1138076 发表于 2026-1-3 16:59 | 只看该作者
牛牛牛,图片博主用什么取的模
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表