• 基于51单片机的电子密码锁设计


    要求:

     设计一个电子密码锁

    1. 输入密码即可开锁,可删除信息,可通过输入123456789ABCDEF等字符
    2. 密码错误发出警报
    3. 用LCD1602显示相关信息

    当输入正确时 D2会亮灯,表示输入正确

    密码输入错误时蜂鸣器会发出警报

    部分程序:

    #include

    typedef unsigned char u8;

    typedef unsigned int u16;

    //sbit 特殊功能位声明:声明某特殊功能寄存器中的一位

    #define key P1                     //按键io口

    sbit fen=P3^0;                     //蜂鸣器io口

    sbit rs=P3^1;        //LCD控制口

    sbit rw=P3^2;

    sbit e=P3^3;

    sbit led=P3^4;       //LED灯io口

    //bit 位变量声明:定义一个位变量的值

    u8 keyz=17,j=0,biao1=0,a=10,b=10,c=10,d=10;

    bit biao=0;

    u16 zh;

    u8 shu[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,

              0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};

    u8 zi[]={"lnput password:"};

    u8 num[]={"0123456789ABCDEF"};

    void delay(u16 i){

              while(i--);

                              }

    /*蜂鸣器*/

    void Fen(u8 a,b)   //蜂鸣器发声函数

    {

             u8 i,j;       

             for(i=0;i

             {

                      for(j=0;j<100;j++)

                       {fen=~fen;delay(b);}

                                                }

                        }

    /*LCD*/

    void writecom(u8 com){                  //LCD写指令

              rs=0;

              rw=0;

              e=0;

              P0=com;

              delay(5);

              e=1;

              e=0;

                         }

    void writedat(u8 dat){             //LCD写数据

              rs=1;

              rw=0;

              e=0;

              P0=dat;

              delay(5);

              e=1;

              e=0;

                         }

    void initlcd(){                       //LCD初始化函数

                writecom(0x38);   //显示模式设置

           writecom(0x0c);

                writecom(0x06);

                                }

    void xian(){      //LCD显示数字函数

                          writedat(num[keyz]);

                                             if(a==10){a=keyz; }                 //对abcd,四位数的单独赋值

                                                  else if(b==10){b=keyz;}

                                                  else if(c==10){c=keyz;}

                                                  else{d=keyz;}

                                                  if(j!=3){j++;}biao=0;

    }

    /*键盘*/

    void Key(){u8 a=0;                    //矩形式键盘函数

              if(key!=0x0f){delay(1000);

              if(key!=0x0f){

                     key=0x0f;

                      switch(key){

                                 case 0x07: keyz=0;biao=1;break;

                                 case 0x0b: keyz=1;biao=1;break;

                                 case 0x0d: keyz=2;biao=1;break;

                                 case 0x0e: keyz=3;biao=1;break;

                      }

                      key=0xf0;

                      switch(key){

                                 case 0x70: keyz=keyz;biao=1;break;

                                 case 0xb0: keyz=keyz+4;biao=1;break;

                                 case 0xd0: keyz=keyz+8;biao=1;break;

                                 case 0xe0: keyz=keyz+12;biao=1;break;

                      }

              }

              while((a<50)&&(key!=0xf0)){a++;delay(10000);}

              }

                }

                                                        

    void shi(){u8 i;   // 按键值判断函数

              if(biao&&keyz!=15){  //判断标志位biao是否为1,为1则有按键操作并且按键的值不等于15            

                             

                               writecom(0x80+0x40+j);                          //数字显示位置进行累加操作

                               if(j<0){j=0;}                                         //数字显示位置边界

                        switch(keyz){                                                     //switch判断按键并执行对应操作

                            case 0:xian(); break;

                                       case 1:xian(); break;

                                       case 2:xian(); break;

                                       case 3:xian(); break;

                                       case 4:xian(); break;

                                       case 5:xian(); break;

                                       case 6:xian(); break;

                                       case 7:xian(); break;

                                       case 8:xian(); break;

                                       case 9:xian(); break;

                                       case 10:writedat(num[keyz]);

                                              

                                                         j++;biao=0;break;

                                       case 11:writedat(num[keyz]);

                                              

                                                         j++;biao=0;break;

                                       case 12:

                                                         biao1=0;                                               //重置标志位biao1,上锁

                                               biao=0;break;

                                       case 13:writecom(0x01);        delay(1000);      //全屏清除

                                                         for(i=0;i

                                 writecom(0x80+i);

                                 writedat(zi[i]);                 }

                                               j=0;biao=0;break;

                                       case 14:                                                                         //对当前数字位删除的操作

                                                         writedat(' ');

                                                         j--;

                                                         writecom(0x80+0x40+j);writedat(' ');

                                                         biao=0;break;

                                       case 15:                      //确定按键

                                               j++;biao=0;break;

                                            }

                                         }

                              if(keyz==15){                                                               //判断按键值如果等于15,便视为按下确定键

                              writecom(0x01);delay(1000);

                               zh=a*1000+b*100+c*10+d;                                            //将ABCD,四位数合并并进行判断是否为正确密码

                            if(zh==1234){  //设密码为1234

                              j=0;

                          biao1=1;                                                                       //将biao1标志位置1便为密码正确

                          keyz=17;                                                                      //使按键值跳出最大值15便不会循坏显示

                                                         }

                      else{

                                 j=0;

                        biao1=0;                                                                                     //反之密码错误

                           keyz=17; 

                              Fen(20,200);                               //蜂鸣器报警

                              }

                              zh=0;a=10,b=10,c=10,d=10;                                     //重置ABCD变量

                          for(i=0;i

                   writecom(0x80+i);

                   writedat(zi[i]);                         }

                                                                                   }        

                                  }

                       

    内容包括:

    需要完整的资料可在我的资源里下载,也可以加入我的纷传圈子,里面有资源压缩包的百度网盘下载地址及提取码。

    纷传点击用微信打开即可,过程有点繁琐请见谅。

     

  • 相关阅读:
    12 | 腾讯云代码分析快速部署
    skynet学习笔记01— skynet开发环境搭建(超详细)与第一个skynet程序
    SpringSecurity之UserDetailsService详解
    力扣20-有效的括号——栈实现
    【车载以太网测试从入门到精通】——DoIP BootLoader刷写测试(含CAPL源码)
    Feign 调用出现异常:feign.FeignException: status 401 reading xxx#xxx(xxx)
    构建汽车技术与装备交流平台,“中国汽研”开启汽车产业高质量发展新章
    03、MongoDB -- MongoDB 权限的设计
    如何对数字货币进行投资分析--基本面
    非对口专业测试人,婉拒猎头、放弃6份高薪offer,你敢信?
  • 原文地址:https://blog.csdn.net/qq_63964231/article/details/127721823