找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机密码锁程序 怎么改才能完成这些功能?

[复制链接]
跳转到指定楼层
楼主
ID:765911 发表于 2020-6-30 17:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
protues      
利用51单片机、24c02、键盘及LCD1602/12864液晶显示器件设计一个密码锁;
设置初始密码为“123456”;在锁已开状态下,能通过矩阵键盘重置密码;输入密码连续错误三次,则禁止输入密码一分钟。
/**************************************************************

二、单片机密码锁的设计
设计要求:1)利用51单片机、键盘及LCD1602/12864液晶显示器件设计一个密码锁;
2)密码允许3~6位;设置初始密码为“123456”;
3)在锁已开状态下,能通过矩阵键盘重置密码;
4)输入密码时,LCD显示“*”。密码输入正确在LCD1602显示“OPEN”,并用LED亮显示开锁的状态;
5)密码输入错误在LCD1602显示“ERROR”,并用LED熄灭表示关锁;
6)输入密码连续错误三次,则禁止输入密码一分钟。

**************************************************************/

  1. #include<reg52.h>
  2. #include <intrins.h>
  3. #define uint unsigned int
  4. #define uchar unsigned char
  5.         
  6. void shuru();

  7. sbit sda=P2^0;                          //IO口定义
  8. sbit scl=P2^1;
  9. sbit rs=P2^2;
  10. sbit lcden=P2^3;
  11. sbit led=P1^0;
  12. bit flag;

  13. uchar password[]={1,2,3,4,5,6};
  14. uchar enter[]={0,0,0,0,0,0,0};

  15. uchar str[]="0123456789";
  16. uchar wei,key,temp;
  17. uchar new1,new2,new3,new4,new5,new6;  
  18. bit allow,change,sure,wanbi,retry,close,reset;        

  19. char key_buf[] = {0xee, 0xde, 0xbe, 0x7e,0xed, 0xdd, 0xbd, 0x7d,//定义键值
  20.                                   0xeb, 0xdb, 0xbb, 0x7b};
  21. uchar p,time,a;

  22. void delay(uint x)
  23. {
  24.         uint a,b;
  25.         for(a=x;a>0;a--)
  26.                 for(b=10;b>0;b--);
  27. }

  28. /*****************************************************
  29.                                 LCD1602初始化
  30. *****************************************************/

  31. void write_com(uchar com)         //些命令,RS=0
  32. {
  33.         P0=com;
  34.         rs=0;
  35.         lcden=0;
  36.         delay(10);
  37.         lcden=1;
  38.         delay(10);
  39.         lcden=0;
  40.         
  41. }

  42. void write_data(uchar dat)           //写数据,RS=1
  43. {
  44.         P0=dat;
  45.         rs=1;
  46.         lcden=0;
  47.         delay(10);
  48.         lcden=1;
  49.         delay(10);
  50.         lcden=0;
  51.         
  52. }

  53. void lcdinit()
  54. {

  55.         write_com(0x38);   //00111000工作方式设置:16×2显示,5×7点阵,8位数据接口

  56.         write_com(0x0c);   

  57.         write_com(0x06);   //00000110输入方式设置:光标右移,字符不移

  58.         write_com(0x01);   //清屏幕指令,将以前的显示内容清除

  59.         write_com(0x80);
  60.         
  61. }

  62. /******************************************************
  63.           24c02初始化
  64. *******************************************************/

  65. void nop()
  66. {
  67.         _nop_();
  68.         _nop_();
  69. }

  70. void init()  //24c02初始化子程序
  71. {
  72.         scl=1;
  73.         nop();
  74.         sda=1;
  75.         nop();
  76. }

  77. void start()        //启动I2C总线
  78. {
  79.         sda=1;
  80.         nop();
  81.         scl=1;
  82.         nop();
  83.         sda=0;
  84.         nop();
  85.         scl=0;
  86.         nop();
  87. }

  88. void stop()         //停止I2C总线
  89. {
  90.         sda=0;
  91.         nop();
  92.         scl=1;
  93.         nop();
  94.         sda=1;
  95.         nop();
  96. }

  97. void writebyte(unsigned char j)  //写一个字节
  98. {
  99.         unsigned char i,temp;
  100.            temp=j;
  101.            for (i=0;i<8;i++)
  102.    {
  103.            temp=temp<<1;
  104.            scl=0;
  105.            nop();
  106.            sda=CY;                //temp左移时,移出的值放入了CY中
  107.            nop();
  108.            scl=1;                //待sda线上的数据稳定后,将scl拉高
  109.            nop();
  110.    }
  111.    scl=0;
  112.    nop();
  113.    sda=1;
  114.    nop();
  115. }

  116. unsigned char readbyte()   //读一个字节
  117. {
  118.    unsigned char i,j,k=0;
  119.    scl=0; nop(); sda=1;
  120.    for (i=0;i<8;i++)
  121.    {  
  122.                 nop(); scl=1; nop();
  123.               if(sda==1)
  124.                 j=1;
  125.               else
  126.                 j=0;
  127.               k=(k<<1)|j;
  128.                   scl=0;
  129.         }
  130.            nop();
  131.         return(k);
  132. }

  133. void clock()         //I2C总线时钟
  134. {
  135.    unsigned char i=0;
  136.    scl=1;
  137.    nop();
  138.    while((sda==1)&&(i<255))
  139.              i++;
  140.    scl=0;
  141.    nop();
  142. }

  143. unsigned char read24c02(unsigned char address)
  144. {
  145.    unsigned char i;
  146.    start();
  147.    writebyte(0xa0);
  148.    clock();
  149.    writebyte(address);
  150.    clock();
  151.    start();
  152.    writebyte(0xa1);
  153.    clock();
  154.    i=readbyte();
  155.    stop();
  156.    delay(100);
  157.    return(i);
  158. }

  159. void write24c02(unsigned char address,unsigned char info)
  160. {
  161.    start();
  162.    writebyte(0xa0);
  163.    clock();
  164.    writebyte(address);
  165.    clock();
  166.    writebyte(info);
  167.    clock();
  168.    stop();
  169.    delay(5000); //这个延时一定要足够长,否则会出错。因为24c02在从sda上取得数据后,还需要一定时间的烧录过程。
  170. }

  171. /*******************************************
  172.                                         矩阵键盘
  173. ********************************************/
  174. void keyscan()
  175. {
  176.         P3=0xff;                //独立按键优先
  177.     temp=P3;
  178.     temp=temp&0xf0;
  179.     if(temp!=0xf0)
  180.     {
  181.       delay(10);
  182.       if(temp!=0xf0)
  183.       {        
  184.                 if(temp==0xe0) retry=1;        //重试
  185.                 if(temp==0xd0) close=1;        //关锁
  186.                 if(temp==0x70) reset=1;        //清内存密码为000000
  187.           }
  188.         }

  189.     P3=0xfe;
  190.     temp=P3;
  191.     temp=temp&0xf0;
  192.     if(temp!=0xf0)
  193.     {
  194.       delay(10);
  195.       if(temp!=0xf0)
  196.       {        
  197.         temp=P3;
  198.         switch(temp)
  199.         {
  200.           case 0xee:
  201.                key=0;
  202.                                                         if(wei<6)
  203.                                                         {
  204.                                                                 enter[wei]=key;
  205.                                                                 write_data('*');
  206.                                                         }
  207.                                                         wei++;
  208.                break;

  209.           case 0xde:
  210.                key=1;
  211.                                                         if(wei<6)
  212.                                                         {
  213.                                                                 enter[wei]=key;
  214.                                                                 write_data('*');
  215.                                                         }
  216.                                                         wei++;
  217.                break;

  218.           case 0xbe:
  219.                key=2;
  220.                                                         if(wei<6)
  221.                                                         {
  222.                                                                 enter[wei]=key;
  223.                                                                 write_data('*');
  224.                                                         }
  225.                                                         wei++;
  226.                break;

  227.           case 0x7e:
  228.                key=3;
  229.                                                                                    if(wei<6)
  230.                                                         {
  231.                                                                 enter[wei]=key;
  232.                                                                 write_data('*');
  233.                                                         }
  234.                                                         wei++;
  235.                break;
  236.          }
  237.          while(temp!=0xf0)
  238.         {
  239.            temp=P3;
  240.            temp=temp&0xf0;

  241.          }

  242.       }
  243.     }
  244.     P3=0xfd;
  245.     temp=P3;
  246.     temp=temp&0xf0;
  247.     if(temp!=0xf0)
  248.     {
  249.       delay(10);
  250.       if(temp!=0xf0)
  251.       {
  252.         temp=P3;
  253.         switch(temp)
  254.         {
  255.           case 0xed:
  256.                key=4;
  257.                                                         if(wei<6)
  258.                                                         {
  259.                                                                 enter[wei]=key;
  260.                                                                 write_data('*');
  261.                                                         }
  262.                                                         wei++;
  263.                break;

  264.           case 0xdd:
  265.                key=5;
  266.                                                         if(wei<6)
  267.                                                         {
  268.                                                                 enter[wei]=key;
  269.                                                                 write_data('*');
  270.                                                         }
  271.                                                         wei++;
  272.                break;

  273.           case 0xbd:
  274.                key=6;
  275.                                                         if(wei<6)
  276.                                                         {
  277.                                                                 enter[wei]=key;
  278.                                                                 write_data('*');
  279.                                                         }
  280.                                                         wei++;
  281.                break;

  282.           case 0x7d:
  283.                key=7;
  284.                                                         if(wei<6)
  285.                                                         {
  286.                                                                 enter[wei]=key;
  287.                                                                 write_data('*');
  288.                                                         }
  289.                                                         wei++;
  290.                break;
  291.          }
  292.          while(temp!=0xf0)
  293.          {
  294.            temp=P3;
  295.            temp=temp&0xf0;

  296.          }

  297.       }
  298.       }
  299.     P3=0xfb;
  300.     temp=P3;
  301.     temp=temp&0xf0;
  302.     if(temp!=0xf0)
  303.     {
  304.       delay(10);
  305.       if(temp!=0xf0)
  306.       {
  307.         temp=P3;
  308.         switch(temp)
  309.         {
  310.           case 0xeb:
  311.                key=8;
  312.                                                         if(wei<6)
  313.                                                         {
  314.                                                                 enter[wei]=key;
  315.                                                                 write_data('*');
  316.                                                         }
  317.                                                         wei++;
  318.                break;

  319.           case 0xdb:
  320.                key=9;
  321.                                                         if(wei<6)
  322.                                                         {
  323.                                                                 enter[wei]=key;
  324.                                                                 write_data('*');
  325.                                                         }
  326.                                                         wei++;
  327.                break;
  328.                            
  329.           case 0xbb:
  330.                change=1;                 //改密
  331.                            wei=0;
  332.                break;

  333.           case 0x7b:
  334.                              if(allow)                 //改后确认
  335.                sure=1;
  336.                break;
  337.          }
  338.         while(temp!=0xf0)
  339.          {
  340.            temp=P3;
  341.            temp=temp&0xf0;

  342.          }

  343.       }
  344.       }
  345.                
  346. /*          P3=0xf7;
  347.     temp=P3;
  348.     temp=temp&0xf0;
  349.     if(temp!=0xf0)
  350.     {
  351.       delay(10);
  352.       if(temp!=0xf0)
  353.       {
  354.         temp=P3;
  355.         switch(temp)
  356.         {
  357.           case 0xe7:
  358.                              retry=1;
  359.                break;

  360.           case 0xd7:
  361.                              close=1;
  362.                break;
  363.          }
  364.         while(temp!=0xf0)
  365.          {
  366.            temp=P3;
  367.            temp=temp&0xf0;
  368.            beep=0;
  369.          }
  370.          beep=1;
  371.       }
  372.       }        */
  373. }

  374. void compare()
  375. {
  376.         if(wei==6)
  377.         {
  378.                 if((enter[0]==password[0])&(enter[1]==password[1])&(enter[2]==password[2])&(enter[3]==password[3])&(enter[4]==password[4])&(enter[5]==password[5]))
  379.                         allow=1;
  380.         }
  381. }

  382. void main()
  383. {
  384.         uchar b,c,d;
  385.         init();
  386.         lcdinit();
  387.         
  388.         /****************************
  389. //        write24c02(110,0x01);
  390. //        write24c02(111,0x02);//24c02的第110到115地址单元作为密码存储区
  391. //        write24c02(112,0x03);
  392. //        write24c02(113,0x04);
  393. //        write24c02(114,0x05);
  394. //        write24c02(115,0x06);
  395.         
  396.         *****************************/
  397.         
  398.         /*password[0]=read24c02(110);  
  399.         password[1]=read24c02(111);
  400.         password[2]=read24c02(112);
  401.         password[3]=read24c02(113);
  402.         password[4]=read24c02(114);
  403.         password[5]=read24c02(115);*/
  404.         
  405.         while(1)
  406.         {
  407.                 keyscan();
  408.                 compare();
  409.                 if(allow)
  410.                 {
  411.                         P1=0x00;
  412.                         write_com(0xc0);
  413.                         write_data('O');
  414.                         write_data('P');
  415.                         write_data('E');
  416.                         write_data('N');
  417.                         delay(500);

  418.                         
  419.                 }
  420.                
  421.                 if(change)        
  422.                 {
  423.                         change=0;
  424.                         if(allow)
  425.                         {
  426.                                 allow=0;
  427.                                 write_com(0x01);
  428.                                 write_com(0xc0);
  429.                                 write_data('N');
  430.                                 write_data('E');
  431.                                 write_data('W');
  432.                                 write_com(0x80);
  433.                                 while(wei<6)
  434.                                 {
  435.                                         keyscan();
  436.                                 }
  437.                                 
  438.                         }
  439.                 }
  440.                 if(sure)
  441.                                 {
  442.                                         sure=0;allow=0; wei=0;
  443.                                         change=0;
  444.                                         for(b=0;b<6;b++)
  445.                                         {
  446.                                                 password[b]=enter[b];
  447.                                         }
  448.                                         write24c02(110,enter[0]);
  449.                                         write24c02(111,enter[1]);
  450.                                         write24c02(112,enter[2]);
  451.                                         write24c02(113,enter[3]);
  452.                                         write24c02(114,enter[4]);
  453.                                         write24c02(115,enter[5]);
  454.                                         write_com(0x01);
  455.                                 }
  456.                 if(close)
  457.                 {
  458.                         close=0;change=0;//所有变量均被清零。
  459.                         wei=0;        
  460.                         allow=0;
  461.                         P1=0xff;
  462.                         write_com(0x01);
  463.                 }
  464.         }
  465.         
  466. }
复制代码

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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