找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机开机密码锁程序

[复制链接]
跳转到指定楼层
楼主
ID:255156 发表于 2017-11-29 15:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于开发板设计的开机密码锁,功能如下:上电显示----,密码输入正确,停留0.5秒然后显示按键值,10秒没有操作或者密码输入错误都返回----,适合想把数码管和按键结合起来的学习,还有不同的窗口。

单片机源程序如下:
  1. #include<reg52.h>
  2. #define const_key_time 20
  3. #define const_no_key_push 4400
  4. #define const_05s 220
  5. void SystemInitial();
  6. void PeripheralInitial();
  7. void delay_short(unsigned char ucDelayShort);
  8. void delay_long(unsigned char ucDelayLong);
  9. void hc595_drive(unsigned char ucStatusTemp16_09,unsigned char ucStatusTemp08_01);
  10. void key_scan();
  11. void key_service();
  12. void display_drive();
  13. void display_service();
  14. void num_key_input(unsigned char ucWhichKey);
  15. sbit key_dr1=P0^0;
  16. sbit key_dr2=P0^1;
  17. sbit key_dr3=P0^2;
  18. sbit key_dr4=P0^3;
  19. sbit key_sr1=P0^4;
  20. sbit key_sr2=P0^5;
  21. sbit key_sr3=P0^6;
  22. sbit key_sr4=P0^7;
  23. sbit srclk_dr=P3^6;
  24. sbit rclk_dr=P3^5;
  25. sbit ser_dr=P3^4;
  26. unsigned char ucRowRecord=1;
  27. unsigned char ucKeyStep=1;
  28. unsigned char ucKeySec=0;
  29. unsigned char ucKeyLock=0;
  30. unsigned int uiKeyTimeCnt=0;
  31. unsigned char ucTemp=0;
  32. unsigned char ucDisplayDriveStep=1;
  33. unsigned char ucDig1;
  34. unsigned char ucDig2;
  35. unsigned char ucDig3;
  36. unsigned char ucDig4;
  37. unsigned char ucWd=1;
  38. unsigned char ucWd1Update=1;
  39. unsigned char ucWd2Update=0;
  40. unsigned char ucWd3Update=0;
  41. unsigned char ucPasswordInput[4];
  42. unsigned char ucPasswordCnt=0;
  43. unsigned char ucKeyNum=1;
  44. unsigned int uiNoKeyPushTimer=const_no_key_push;
  45. unsigned int uiPasswordTimer=const_05s;
  46. unsigned char ucTemp1=0;
  47. unsigned char ucTemp2=0;
  48. unsigned char ucTemp3=0;
  49. unsigned char ucTemp4=0;
  50. unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40,0x73};

  51. void main()
  52. {
  53.         SystemInitial();
  54.         delay_long(100);
  55.         PeripheralInitial();
  56.         while(1)
  57.         {
  58.                 key_service();
  59.                 display_service();
  60.         }
  61. }

  62. void display_service()
  63. {
  64.         switch(ucWd)
  65.         {
  66.                 case 1: if(ucWd1Update==1)
  67.                                 {
  68.                                         ucWd1Update=0;
  69.                                         ucDig4=ucPasswordInput[0];
  70.                                         ucDig3=ucPasswordInput[1];
  71.                                         ucDig2=ucPasswordInput[2];
  72.                                         ucDig1=ucPasswordInput[3];
  73.                                 }
  74.                                 break;
  75.                 case 2: if(ucWd2Update==1)
  76.                                 {
  77.                                         ucWd2Update=0;
  78.                                         ucDig4=10;
  79.                                         ucDig3=10;
  80.                                         ucDig2=10;
  81.                                         ucDig1=ucKeyNum;
  82.                                 }
  83.                                 break;
  84.                 case 3: if(ucWd3Update==1)
  85.                                 {
  86.                                         ucWd3Update=0;
  87.                                         ucDig4=ucPasswordInput[0];
  88.                                         ucDig3=ucPasswordInput[1];
  89.                                         ucDig2=ucPasswordInput[2];
  90.                                         ucDig1=ucPasswordInput[3];
  91.                                 }
  92.                                 break;
  93.         }
  94. }

  95. void key_scan()
  96. {
  97.         switch(ucKeyStep)
  98.         {
  99.                 case 1: if(ucRowRecord==1)
  100.                                 {
  101.                                         key_dr1=0;
  102.                                         key_dr2=1;
  103.                                         key_dr3=1;
  104.                                         key_dr4=1;
  105.                                 }
  106.                                 else if(ucRowRecord==2)
  107.                                 {
  108.                                         key_dr1=1;
  109.                                         key_dr2=0;
  110.                                         key_dr3=1;
  111.                                         key_dr4=1;
  112.                                 }
  113.                                 else if(ucRowRecord==3)
  114.                                 {
  115.                                         key_dr1=1;
  116.                                         key_dr2=1;
  117.                                         key_dr3=0;
  118.                                         key_dr4=1;
  119.                                 }
  120.                                 else
  121.                                 {
  122.                                         key_dr1=1;
  123.                                         key_dr2=1;
  124.                                         key_dr3=1;
  125.                                         key_dr4=0;
  126.                                 }
  127.                                 uiKeyTimeCnt=0;
  128.                                 ucKeyStep++;
  129.                                 break;
  130.                 case 2: uiKeyTimeCnt++;
  131.                                 if(uiKeyTimeCnt>1)
  132.                                 {
  133.                                         uiKeyTimeCnt=0;
  134.                                         ucKeyStep++;
  135.                                 }
  136.                                 break;
  137.                 case 3: if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==1)
  138.                                 {
  139.                                         ucKeyStep=1;
  140.                                         ucKeyLock=0;
  141.                                         uiKeyTimeCnt=0;
  142.                                         ucRowRecord++;
  143.                                         if(ucRowRecord>4)
  144.                                                 ucRowRecord=1;
  145.                                 }
  146.                                 else if(ucKeyLock==0)
  147.                                 {                                       
  148.                                         if(key_sr1==0&&key_sr2==1&&key_sr3==1&&key_sr4==1)
  149.                                         {
  150.                                                 uiKeyTimeCnt++;
  151.                                                 if(uiKeyTimeCnt>const_key_time)
  152.                                                 {
  153.                                                         uiKeyTimeCnt=0;
  154.                                                         ucKeyLock=1;       
  155.                                                         if(ucRowRecord==1)
  156.                                                                 ucKeySec=1;
  157.                                                         else if(ucRowRecord==2)
  158.                                                                 ucKeySec=2;
  159.                                                         else if(ucRowRecord==3)
  160.                                                                 ucKeySec=3;
  161.                                                         else
  162.                                                                 ucKeySec=4;
  163.                                                 }
  164.                                         }
  165.                                         if(key_sr1==1&&key_sr2==0&&key_sr3==1&&key_sr4==1)
  166.                                         {
  167.                                                 uiKeyTimeCnt++;
  168.                                                 if(uiKeyTimeCnt>const_key_time)
  169.                                                 {
  170.                                                         uiKeyTimeCnt=0;
  171.                                                         ucKeyLock=1;       
  172.                                                         if(ucRowRecord==1)
  173.                                                                 ucKeySec=5;
  174.                                                         else if(ucRowRecord==2)
  175.                                                                 ucKeySec=6;
  176.                                                         else if(ucRowRecord==3)
  177.                                                                 ucKeySec=7;
  178.                                                         else
  179.                                                                 ucKeySec=8;
  180.                                                 }
  181.                                         }
  182.                                         if(key_sr1==1&&key_sr2==1&&key_sr3==0&&key_sr4==1)
  183.                                         {
  184.                                                 uiKeyTimeCnt++;
  185.                                                 if(uiKeyTimeCnt>const_key_time)
  186.                                                 {
  187.                                                         uiKeyTimeCnt=0;
  188.                                                         ucKeyLock=1;       
  189.                                                         if(ucRowRecord==1)
  190.                                                                 ucKeySec=9;
  191.                                                         else if(ucRowRecord==2)
  192.                                                                 ucKeySec=10;
  193.                                                         else if(ucRowRecord==3)
  194.                                                                 ucKeySec=11;
  195.                                                         else
  196.                                                                 ucKeySec=12;
  197.                                                 }
  198.                                         }
  199.                                         if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==0)
  200.                                         {
  201.                                                 uiKeyTimeCnt++;
  202.                                                 if(uiKeyTimeCnt>const_key_time)
  203.                                                 {
  204.                                                         uiKeyTimeCnt=0;
  205.                                                         ucKeyLock=1;       
  206.                                                         if(ucRowRecord==1)
  207.                                                                 ucKeySec=13;
  208.                                                         else if(ucRowRecord==2)
  209.                                                                 ucKeySec=14;
  210.                                                         else if(ucRowRecord==3)
  211.                                                                 ucKeySec=15;
  212.                                                         else
  213.                                                                 ucKeySec=16;
  214.                                                 }
  215.                                         }
  216.                                 }
  217.                                 break;
  218.         }
  219. }

  220. void key_service()
  221. {
  222.         switch(ucKeySec)
  223.         {
  224.                 case 1: num_key_input(1);
  225.                                 ucKeySec=0;
  226.                                 break;
  227.                 case 2: num_key_input(2);
  228.                                 ucKeySec=0;
  229.                                 break;
  230.                 case 3: num_key_input(3);
  231.                                 ucKeySec=0;
  232.                                 break;
  233.                 case 4: num_key_input(4);
  234.                                 ucKeySec=0;
  235.                                 break;
  236.                 case 5: num_key_input(5);
  237.                                 ucKeySec=0;
  238.                                 break;
  239.                 case 6: num_key_input(6);
  240.                                 ucKeySec=0;
  241.                                 break;
  242.                 case 7: num_key_input(7);
  243.                                 ucKeySec=0;
  244.                                 break;
  245.                 case 8: num_key_input(8);
  246.                                 ucKeySec=0;
  247.                                 break;
  248.                 case 9: num_key_input(9);
  249.                                 ucKeySec=0;
  250.                                 break;
  251.                 case 10: num_key_input(0);
  252.                                  ucKeySec=0;
  253.                                  break;
  254.                 case 11: ucKeySec=0;
  255.                                  break;
  256.                 case 12: ucKeySec=0;
  257.                                  break;
  258.                 case 13: ucKeySec=0;
  259.                                  break;                                          
  260.                 case 14: ucKeySec=0;
  261.                                  break;
  262.                 case 15: ucKeySec=0;
  263.                                  break;
  264.                 case 16: ucKeySec=0;
  265.                                  break;
  266.         }
  267. }

  268. void num_key_input(unsigned char ucWhichKey)
  269. {
  270.         switch(ucWd)
  271.         {
  272.                 case 1: ucPasswordInput[ucPasswordCnt]=ucWhichKey;
  273.                                 ucPasswordCnt++;
  274.                                 if(ucPasswordCnt>=4)
  275.                                 {
  276.                                         ucPasswordCnt=0;
  277.                                         ucWd=3;
  278.                                         ucWd3Update=1;
  279.                                         uiPasswordTimer=const_05s;
  280.                                 }
  281.                                 ucWd1Update=1;
  282.                                 uiNoKeyPushTimer=const_no_key_push;
  283.                                 break;
  284.                 case 2: ucKeyNum=ucWhichKey;
  285.                                 ucWd2Update=1;
  286.                                 uiNoKeyPushTimer=const_no_key_push;
  287.                                 break;
  288.         }
  289. }

  290. void display_drive()
  291. {
  292.         switch(ucDisplayDriveStep)
  293.         {
  294.                 case 1: ucTemp=table[ucDig1];
  295.                                 hc595_drive(ucTemp,0xfe);
  296.                                 break;

  297.                 case 2: ucTemp=table[ucDig2];
  298.                                 hc595_drive(ucTemp,0xfd);
  299.                                 break;
  300.             case 3: ucTemp=table[ucDig3];
  301.                                 hc595_drive(ucTemp,0xfb);
  302.                                 break;
  303.                 case 4: ucTemp=table[ucDig4];
  304.                                 hc595_drive(ucTemp,0xf7);
  305.                                 break;
  306.         }
  307.         ucDisplayDriveStep++;
  308.         if(ucDisplayDriveStep>4)
  309.                 ucDisplayDriveStep=1;
  310. }

  311. void hc595_drive(unsigned char ucStatusTemp16_09,unsigned char ucStatusTemp08_01)
  312. {
  313.         unsigned char ucTempData=0;
  314.         unsigned char i;
  315.         srclk_dr=0;
  316.         rclk_dr=0;
  317.        
  318.         ucTempData=ucStatusTemp16_09;
  319.         for(i=0;i<8;i++)
  320.         {
  321.                 if(ucTempData>=0x80)
  322.                         ser_dr=1;
  323.                 else ser_dr=0;
  324.                 srclk_dr=0;
  325.                 delay_short(1);
  326.                 srclk_dr=1;
  327.                 ucTempData=ucTempData<<1;
  328.         }
  329.        
  330.         ucTempData=ucStatusTemp08_01;
  331.         for(i=0;i<8;i++)
  332.         {
  333.                 if(ucTempData>=0x80)
  334.                         ser_dr=1;
  335.                 else ser_dr=0;
  336.                 srclk_dr=0;
  337.                 delay_short(1);
  338.                 srclk_dr=1;
  339.                 delay_short(1);
  340.                 ucTempData=ucTempData<<1;
  341.         }
  342.        
  343.         rclk_dr=0;
  344.         delay_short(1);
  345.         rclk_dr=1;
  346.         delay_short(1);
  347. }

  348. void T0_time() interrupt 1
  349. {
  350.         unsigned int i;
  351.         if(ucWd==3)
  352.         {
  353.                 if(uiPasswordTimer>0)
  354.                         uiPasswordTimer--;
  355.                 if(uiPasswordTimer==0)
  356.                 {
  357.                         if(ucPasswordInput[0]==1&&ucPasswordInput[1]==3&&ucPasswordInput[2]==1&&ucPasswordInput[3]==4)
  358.                         {
  359.                                 ucWd=2;
  360.                                 ucWd2Update=1;
  361.                         }
  362.                         else
  363.                         {
  364.                                 for(i=0;i<4;i++)
  365.                                         ucPasswordInput[i]=11;
  366.                                 ucWd=1;
  367.                                 ucWd1Update=1;
  368.                         }
  369.                 }
  370.         }
  371.         if(ucWd==2)
  372.         {
  373. ……………………

  374. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
24.开机密码锁.rar (39.22 KB, 下载次数: 20)


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

使用道具 举报

沙发
ID:365788 发表于 2018-7-7 13:09 | 只看该作者
楼主 ,你确定你的代码没有问题?怎么我自己烧到板子里去之后并没有你说的功能呢?矩阵按键也不显示,LCD也不显示输入的数据???
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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