找回密码
 注册

QQ登录

只需一步,快速开始

搜索

51单片机电子密码锁程序 3x3矩阵按键Proteus仿真

[复制链接]
eng 发表于 2021-6-25 02:47:34 | 显示全部楼层 |阅读模式
3*3矩阵按键 继电器控制密码正确与错误
两个LED灯显示报警和密码正确
LCD1602显示屏显示密码
仿真原理图如下(proteus仿真工程文件和完整单片机c源码可到本帖附件中下载)
1.gif
部分源码:完整源码见附件
  1. include<reg51.h>
  2. #define ulong unsigned long
  3. #define uint unsigned int
  4. #define uchar unsigned char
  5. uchar temp,num,flag,r;
  6. uchar code table1[]="welcome!       ";
  7. uchar code table2[]="enter password";
  8. uchar code table3[]="012345";
  9. uchar code table4[]="code is wrong!";
  10. uchar code table5[]="you are right!";
  11. uchar code table6[]="the second time!";
  12. uchar code table7[]="the last time!";
  13. uchar code table8[]="Input new code:";
  14. uchar code table9[]="calling.....";
  15. uchar code table10[]="password";
  16. uchar code table11[]="    ";
  17. uchar code table12[]="Modify right!!!";
  18. uchar code table13[]="Input again:";
  19. uchar code table14[]="Modify error!!!";
  20. uchar init_pswd[4]={2,3,0,5};
  21. uchar pswd[4]={0};
  22. sbit lcdrs=P2^0; //LCD数据/指令(H/L)选择端
  23. sbit lcdrw=P2^1;
  24. sbit lcden=P2^2; //LCD使能
  25. sbit P1_6=P1^6;
  26. sbit P2_6=P2^6;//报警指示灯
  27. sbit P2_7=P2^7;
  28. sbit P3_7=P3^7; //蜂鸣器
  29. /****************子函数声明*/
  30. void delay(uint z);
  31. void init();//初始化子函数
  32. void write_com(uchar com);//写指令子函数
  33. void write_data(uchar dat1);//写数据子函数
  34. uint keyscan();
  35. void mima();//密码处理子函数
  36. void mima1();//修改密码函数
  37. void Alarm(uchar t);//报警函数
  38. /***************液晶驱动*/
  39. void init()
  40. {
  41. lcden=0;
  42. write_com(0x38);
  43. write_com(0x0e);
  44. write_com(0x06);
  45. write_com(0x01);
  46. write_com(0x80);
  47. }
  48. void write_com(uchar com)
  49. {
  50. lcdrs=0;
  51. lcdrw=0;
  52. P0=com;
  53. delay(5);
  54. lcden=1;
  55. delay(5);
  56. lcden=1;
  57. delay(5);
  58. lcden=0;
  59. }
  60. void write_data(uchar dat1)
  61. {
  62. lcdrs=1;
  63. lcdrw=0;
  64. P0=dat1;
  65. delay(5);
  66. lcden=1;
  67. delay(5);
  68. lcden=0;
  69. }
  70. void delay(uint z) //延时子函数 z*1ms
  71. {
  72. uint x,y;
  73. for(x=z;x>0;x--)
  74. for(y=110;y>0;y--);
  75. }
  76. uint keyscan() //键盘扫描程序
  77. {
  78. P1=0xfe; //先选定第一行
  79. temp=P1;
  80. temp=temp&0xf8; //列线全为1,行线为0
  81. while(temp!=0xf8)
  82. {
  83. delay(15); //延时去抖
  84. temp=P1;
  85. temp=temp&0xf8;
  86. while(temp!=0xf8)
  87. {
  88. temp=P1;
  89. switch(temp) //判断哪列按下
  90. {
  91. case 0xf6:num=0;flag=1;break;//第一列按下
  92. case 0xee:num=1;flag=1;break;//第二列按下
  93. case 0xde:num=2;flag=1;break;//第三列按下
  94. }
  95. while(temp!=0xf8)
  96. {
  97. temp=P1;
  98. temp=temp&0xf8;
  99. }
  100. }
  101. }
  102. P1=0xfd; //选定第二行,列线为1
  103. temp=P1;
  104. temp=temp&0xf8;
  105. while(temp!=0xf8)
  106. {
  107. delay(15);
  108. temp=P1;
  109. temp=temp&0xf8;
  110. while(temp!=0xf8)
  111. {
  112. temp=P1;
  113. switch(temp) //判断哪列按下
  114. {
  115. case 0xf5:num=3;flag=1;//第一列按下
  116. break;
  117. case 0xed:num=4;flag=1;//第二列按下
  118. break;
  119. case 0xdd:num=5;flag=1;//第三列按下
  120. break;
  121. }
  122. while(temp!=0xf8)
  123. {
  124. temp=P1;
  125. temp=temp&0xf8;

  126. ……………………

  127. …………限于本文篇幅 余下代码请下载附件…………
复制代码
完整单片机源程序和proteus仿真文件.zip (101.18 KB, 售价: 2 E币)

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

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-24 06:55

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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