当前位置:   article > 正文

基于C51单片机设计的密码锁(附源代码)_单片机密码锁代码

单片机密码锁代码

这位正在读文章的朋友你好,这篇文章是本人的第二篇,希望较第一篇相比,能更严谨一点,切实地给你带来帮助。(如有不对的地方,欢迎您的指正)

本次项目用到的外设有C51芯片,SG90舵机,lcd1602液晶显示屏。

实验共用到了lcd1602(lcd1602.c)、舵机(Motor.c)、矩阵键盘(Maxtrixkey.c)文件的编写。如下图所示

先分别详细地介绍一下各模块的编程:

第一部分:矩阵键盘编程

期望结果:按键从左上方到右下方分别代表着从数字1到数字16.(如图所示),其中数字11我们根据它比较接近9,将其在main函数中设计成确认键。

 代码如下:

MaxtrixKey.c

  1. #include <REGX51.H>
  2. #include "Delay.h"
  3. unsigned char MaxtriKey()
  4. {
  5. unsigned char KeyNumber=0;
  6. P3=0xFF;//先把P1全部高电平
  7. P3_4=0;//把第一列置低
  8. if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=1;}
  9. if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=5;}
  10. if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=9;}
  11. if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=13;}
  12. P3=0xFF;//先把P1全部高电平
  13. P3_5=0;//把第二列置低
  14. if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=2;}
  15. if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=6;}
  16. if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=10;}
  17. if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=14;}
  18. P3=0xFF;//先把P1全部高电平
  19. P3_6=0;//把第三列置低
  20. if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=3;}
  21. if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=7;}
  22. if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=11;}
  23. if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=15;}
  24. P3=0xFF;//先把P1全部高电平
  25. P3_7=0;//把第四列置低
  26. if(P3_0==0){Delay(20);while(P3_0==0);Delay(20);KeyNumber=4;}
  27. if(P3_1==0){Delay(20);while(P3_1==0);Delay(20);KeyNumber=8;}
  28. if(P3_2==0){Delay(20);while(P3_2==0);Delay(20);KeyNumber=12;}
  29. if(P3_3==0){Delay(20);while(P3_3==0);Delay(20);KeyNumber=16;}
  30. return KeyNumber;
  31. }

MaxtrixKey.h

  1. #ifndef __MAXTRIXKEY_H__
  2. #define __MAXTRIXKEY_H__
  3. unsigned char MaxtriKey();
  4. #endif

.h文件就是简单定义一下.c中的函数,矩阵键盘C文件里只有这一个。

.c文件中矩阵键盘设计原理不加以赘述,我设计的接的是P3口,各位读者可以自由选择。

接下来是lcd1602的C文件,这个C文件的代码我是从网上找的,知道各函数的用途其实就足够了(对于我这样的业余选手  emm...),我在网上找的这个C文件里的函数挺多的,但最主要的用的是符号显示和数字显示函数。

lcd1602.c(直接用,也无伤大雅)

  1. #include <REGX51.H>
  2. //引脚配置:
  3. sbit LCD_RS=P2^6;
  4. sbit LCD_RW=P2^5;
  5. sbit LCD_EN=P2^7;
  6. #define LCD_DataPort P0
  7. //函数定义:
  8. /**
  9. * @brief LCD1602延时函数,12MHz调用可延时1ms
  10. * @param 无
  11. * @retval 无
  12. */
  13. void LCD_Delay()
  14. {
  15. unsigned char i, j;
  16. i = 2;
  17. j = 239;
  18. do
  19. {
  20. while (--j);
  21. } while (--i);
  22. }
  23. /**
  24. * @brief LCD1602写命令
  25. * @param Command 要写入的命令
  26. * @retval 无
  27. */
  28. void LCD_WriteCommand(unsigned char Command)
  29. {
  30. LCD_RS=0;
  31. LCD_RW=0;
  32. LCD_DataPort=Command;
  33. LCD_EN=1;
  34. LCD_Delay();
  35. LCD_EN=0;
  36. LCD_Delay();
  37. }
  38. /**
  39. * @brief LCD1602写数据
  40. * @param Data 要写入的数据
  41. * @retval 无
  42. */
  43. void LCD_WriteData(unsigned char Data)
  44. {
  45. LCD_RS=1;
  46. LCD_RW=0;
  47. LCD_DataPort=Data;
  48. LCD_EN=1;
  49. LCD_Delay();
  50. LCD_EN=0;
  51. LCD_Delay();
  52. }
  53. /**
  54. * @brief LCD1602设置光标位置
  55. * @param Line 行位置,范围:1~2
  56. * @param Column 列位置,范围:1~16
  57. * @retval 无
  58. */
  59. void LCD_SetCursor(unsigned char Line,unsigned char Column)
  60. {
  61. if(Line==1)
  62. {
  63. LCD_WriteCommand(0x80|(Column-1));
  64. }
  65. else if(Line==2)
  66. {
  67. LCD_WriteCommand(0x80|(Column-1+0x40));
  68. }
  69. }
  70. /**
  71. * @brief LCD1602初始化函数
  72. * @param 无
  73. * @retval 无
  74. */
  75. void LCD_Init()
  76. {
  77. LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
  78. LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
  79. LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
  80. LCD_WriteCommand(0x01);//光标复位,清屏
  81. }
  82. /**
  83. * @brief 在LCD1602指定位置上显示一个字符
  84. * @param Line 行位置,范围:1~2
  85. * @param Column 列位置,范围:1~16
  86. * @param Char 要显示的字符
  87. * @retval 无
  88. */
  89. void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
  90. {
  91. LCD_SetCursor(Line,Column);
  92. LCD_WriteData(Char);
  93. }
  94. /**
  95. * @brief 在LCD1602指定位置开始显示所给字符串
  96. * @param Line 起始行位置,范围:1~2
  97. * @param Column 起始列位置,范围:1~16
  98. * @param String 要显示的字符串
  99. * @retval 无
  100. */
  101. void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
  102. {
  103. unsigned char i;
  104. LCD_SetCursor(Line,Column);
  105. for(i=0;String[i]!='\0';i++)
  106. {
  107. LCD_WriteData(String[i]);
  108. }
  109. }
  110. /**
  111. * @brief 返回值=X的Y次方
  112. */
  113. int LCD_Pow(int X,int Y)
  114. {
  115. unsigned char i;
  116. int Result=1;
  117. for(i=0;i<Y;i++)
  118. {
  119. Result*=X;
  120. }
  121. return Result;
  122. }
  123. /**
  124. * @brief 在LCD1602指定位置开始显示所给数字
  125. * @param Line 起始行位置,范围:1~2
  126. * @param Column 起始列位置,范围:1~16
  127. * @param Number 要显示的数字,范围:0~65535
  128. * @param Length 要显示数字的长度,范围:1~5
  129. * @retval 无
  130. */
  131. void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
  132. {
  133. unsigned char i;
  134. LCD_SetCursor(Line,Column);
  135. for(i=Length;i>0;i--)
  136. {
  137. LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
  138. }
  139. }
  140. /**
  141. * @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
  142. * @param Line 起始行位置,范围:1~2
  143. * @param Column 起始列位置,范围:1~16
  144. * @param Number 要显示的数字,范围:-32768~32767
  145. * @param Length 要显示数字的长度,范围:1~5
  146. * @retval 无
  147. */
  148. void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
  149. {
  150. unsigned char i;
  151. unsigned int Number1;
  152. LCD_SetCursor(Line,Column);
  153. if(Number>=0)
  154. {
  155. LCD_WriteData('+');
  156. Number1=Number;
  157. }
  158. else
  159. {
  160. LCD_WriteData('-');
  161. Number1=-Number;
  162. }
  163. for(i=Length;i>0;i--)
  164. {
  165. LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
  166. }
  167. }
  168. /**
  169. * @brief 在LCD1602指定位置开始以十六进制显示所给数字
  170. * @param Line 起始行位置,范围:1~2
  171. * @param Column 起始列位置,范围:1~16
  172. * @param Number 要显示的数字,范围:0~0xFFFF
  173. * @param Length 要显示数字的长度,范围:1~4
  174. * @retval 无
  175. */
  176. void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
  177. {
  178. unsigned char i,SingleNumber;
  179. LCD_SetCursor(Line,Column);
  180. for(i=Length;i>0;i--)
  181. {
  182. SingleNumber=Number/LCD_Pow(16,i-1)%16;
  183. if(SingleNumber<10)
  184. {
  185. LCD_WriteData(SingleNumber+'0');
  186. }
  187. else
  188. {
  189. LCD_WriteData(SingleNumber-10+'A');
  190. }
  191. }
  192. }
  193. /**
  194. * @brief 在LCD1602指定位置开始以二进制显示所给数字
  195. * @param Line 起始行位置,范围:1~2
  196. * @param Column 起始列位置,范围:1~16
  197. * @param Number 要显示的数字,范围:0~1111 1111 1111 1111
  198. * @param Length 要显示数字的长度,范围:1~16
  199. * @retval 无
  200. */
  201. void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
  202. {
  203. unsigned char i;
  204. LCD_SetCursor(Line,Column);
  205. for(i=Length;i>0;i--)
  206. {
  207. LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
  208. }
  209. }

lcd1602.h

  1. #ifndef __LCD1602_H__
  2. #define __LCD1602_H__
  3. //用户调用函数:
  4. void LCD_Init();
  5. void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);
  6. void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);
  7. void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
  8. void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
  9. void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
  10. void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
  11. #endif

接下来是SG90舵机部分,舵机工作原理如果不了解,建议找书看看,我这里解释的话,可能没这么严谨,所以我这里直接给出代码了。

Motor.c

  1. #include <REGX51.H>
  2. #include "Delay.h"
  3. #define MotorData P1
  4. unsigned char phasecw[4] ={0x08,0x04,0x02,0x01};
  5. unsigned char phaseccw[4]={0x01,0x02,0x04,0x08};
  6. void MotorCW(void)
  7. {
  8. unsigned int i;
  9. for(i=0;i<4;i++)
  10. {
  11. MotorData=phasecw[i];
  12. Delay(15);
  13. }
  14. }
  15. void MotorCCW(void)
  16. {
  17. unsigned int i;
  18. for(i=0;i<4;i++)
  19. {
  20. MotorData=phaseccw[i];
  21. Delay(15);
  22. }
  23. }
  24. void MotorStop(void)
  25. {
  26. MotorData=0x00;
  27. }

MotorCW是逆时针转动90度,MotorCCW是顺时针转动90度,分别代表着开门与关门。

Motor.h

  1. #ifndef __MOTOR_H__
  2. #define __MOTOR_H__
  3. void MotorCW(void);
  4. void MotorCCW(void);
  5. void MotorStop(void);
  6. #endif

main.c(这里我设计的密码是2345,你随意* *)

  1. #include <REGX51.H>
  2. #include "lcd1602.h"
  3. #include "MaxtrixKey.h"
  4. #include "Delay.h"
  5. #include "Motor.h"
  6. unsigned char KeyNum;
  7. unsigned int Password;
  8. unsigned int Count=0;
  9. unsigned int k,m;
  10. void main()
  11. {
  12. LCD_Init();
  13. LCD_ShowString(1,1,"Password:");
  14. while(1)
  15. {
  16. KeyNum=MaxtriKey();
  17. if(KeyNum)
  18. {
  19. if(KeyNum<=10)//如果s1-s10按键按下 输入密码
  20. {
  21. if(Count<4)
  22. {
  23. Password*=10;
  24. Password+=KeyNum%10;//获取一位密码
  25. Count++;
  26. }
  27. LCD_ShowNum(2,1,Password,4);
  28. }
  29. if(KeyNum==11)//当确认键被按下
  30. {
  31. if(Password==2345)
  32. {
  33. LCD_Init();
  34. LCD_ShowString(1,1,"Welcome home!");
  35. Password=0;
  36. Count=0;
  37. Delay(1000);
  38. for(k=0;k<125;k++)
  39. {
  40. MotorCW();
  41. }
  42. LCD_Init();
  43. LCD_ShowString(1,1,"Will Be closed");
  44. LCD_ShowString(2,1,"5S later....");
  45. Delay(5000);
  46. for(m=0;m<125;m++)
  47. {
  48. MotorCCW();
  49. }
  50. LCD_Init();
  51. LCD_ShowString(1,1,"Password:");
  52. LCD_ShowNum(2,1,Password,4);
  53. }
  54. else
  55. {
  56. LCD_ShowString(1,14,"ERR");
  57. Delay(3000);
  58. LCD_Init();
  59. LCD_ShowString(1,1,"Try again?");
  60. Delay(3000);
  61. Password=0;
  62. Count=0;
  63. Delay(1000);
  64. LCD_Init();
  65. LCD_ShowString(1,1,"Enter Password:");
  66. LCD_ShowNum(2,1,Password,4);
  67. }
  68. }
  69. }
  70. }
  71. }

实验现象:

已上传到我 主页--->视频

 

 

 

 

 

 我红色圈住的这个东西,是SG90的驱动的芯片,一般情况下,这么大的学习板都是带的,我这个也带的,就在红圈的右边一点,当时买这个驱动芯片是想着把它固定在寝室门上做一个蓝牙控制寝室开关灯的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/36407?site
推荐阅读
相关标签
  

闽ICP备14008679号