• 51单片机:实现CSGO中C4下包功能(附功能实现视频和代码详解)


    目录

    一、功能实现

    二、功能简介

    1.矩阵键盘输入密码

    2.S11确认输入密码

    3.启用蜂鸣器

    三、模块化代码

    1.Buzzer.h

    2.Buzzer.c

    3.MatrixKey.h

    4.MatrixKey.c

    5.LCD1602.h

    6.LCD1602.c

    7.Delay.h

    8.Delay.c

    四、主函数

    五、Keil5界面


     

    一、功能实现

    51单片机实现CSGO中C4下包

     

    二、功能简介

    1.矩阵键盘输入密码

    按键密码
    S11
    S22
    S33
    S44
    S55
    S66
    S77
    S88
    S99
    S100

    2.S11确认输入密码

    1. if(KeyNum)//等价于if(KeyNum!=0)----非零即真
    2. {
    3. if(KeyNum<=10)//如果S1~S10按键按下,输入密码
    4. {
    5. if(InputCount<7)
    6. {
    7. InputCount++;
    8. LCD_ShowNum(2,InputCount,KeyNum,1);
    9. }
    10. }
    11. if(KeyNum==11) //如果S11按键按下
    12. {
    13. LCD_ShowNum(2,1,Password,7);
    14. LCD_ShowString(2,1,"*******");
    15. }
    16. }

    3.启用蜂鸣器

    1. void Buzzer_Time(unsigned int ms)//蜂鸣器时长
    2. {
    3. unsigned int i;
    4. for(i=0;i2;i++)
    5. {
    6. Buzzer=!Buzzer;
    7. Buzzer_Delay700us();
    8. //每700微秒翻转一次,一个周期是1.4毫秒,频率是698Hz
    9. }
    10. }

    三、模块化代码

    1.Buzzer.h

    1. #ifndef __BUZZER_H__
    2. #define __BUZZER_H__
    3. void Buzzer_Time(unsigned int ms);
    4. #endif

    2.Buzzer.c

    1. #include
    2. //蜂鸣器端口
    3. sbit Buzzer=P2^5;
    4. //sbit位控制宏,可以将单片机的某个引脚或寄存器的特定位映射为一个C语言的变量。
    5. //格式如下
    6. //sbit <位变量名>=<引脚或者寄存器地址>.<位号>;
    7. //解释:位变量名 是给这个位定义的变量名
    8. //引脚或者寄存地地址 是所控制的引脚或者寄存地地址
    9. //位号 表示所要控制的位在引脚或寄存器中的位置
    10. /**
    11. * @brief 蜂鸣器私有延时函数,延时500us
    12. * @param 无
    13. * @retval 无
    14. */
    15. void Buzzer_Delay700us(void)
    16. {
    17. unsigned char data i, j;
    18. i = 2;
    19. j = 61;
    20. do
    21. {
    22. while (--j);
    23. } while (--i);
    24. }
    25. /**
    26. * @brief 蜂鸣器发声
    27. * @param ms(发声时长)
    28. * @retval 无
    29. */
    30. void Buzzer_Time(unsigned int ms)//蜂鸣器时长
    31. {
    32. unsigned int i;
    33. for(i=0;i2;i++)
    34. {
    35. Buzzer=!Buzzer;
    36. Buzzer_Delay700us();
    37. //每700微秒翻转一次,一个周期是1.4毫秒,频率是698Hz
    38. }
    39. }

    3.MatrixKey.h

    1. #ifndef __MATRIXKEY_H__
    2. #define __MATRIXKEY_H__
    3. unsigned char MatrixKey();
    4. #endif

    4.MatrixKey.c

    1. #include
    2. #include "Delay.h"
    3. /**
    4. * @brief 矩阵键盘读取按键键码
    5. * @param 无
    6. * @retval KeyNumber 按下按键的键码值
    7. 如果按键按下不放,程序会停留在此函数,松手的一瞬间,返回按键的键码,没有按键按下的时候放回0
    8. */
    9. int MatrixKey()
    10. {
    11. //矩阵扫描,按一行或者按一列,依次输入扫描,要扫描的给0,其余给1
    12. int KeyNumber=0;
    13. P1=0xFF; //默认都是1(高电平)
    14. P1_3=0;
    15. //检测按键按下,就是判断是否变成0
    16. if(P1_7==0)//1行1列的按钮=1
    17. {
    18. Delay(20);
    19. while(P1_7==0)
    20. {
    21. Delay(20);
    22. }
    23. KeyNumber=1;
    24. }
    25. if(P1_6==0)//2行1列的按钮=5
    26. {
    27. Delay(20);
    28. while(P1_6==0)
    29. {
    30. Delay(20);
    31. }
    32. KeyNumber=5;
    33. }
    34. if(P1_5==0)//3行1列的按钮=9
    35. {
    36. Delay(20);
    37. while(P1_5==0)
    38. {
    39. Delay(20);
    40. }
    41. KeyNumber=9;
    42. }
    43. if(P1_4==0)//4行1列的按钮=13
    44. {
    45. Delay(20);
    46. while(P1_4==0)
    47. {
    48. Delay(20);
    49. }
    50. KeyNumber=13;
    51. }
    52. P1=0xFF; //默认都是1(高电平)
    53. P1_2=0;
    54. //检测按键按下,就是判断是否变成0
    55. if(P1_7==0)//1行2列的按钮=2
    56. {
    57. Delay(20);
    58. while(P1_7==0)
    59. {
    60. Delay(20);
    61. }
    62. KeyNumber=2;
    63. }
    64. if(P1_6==0)//2行2列的按钮=6
    65. {
    66. Delay(20);
    67. while(P1_6==0)
    68. {
    69. Delay(20);
    70. }
    71. KeyNumber=6;
    72. }
    73. if(P1_5==0)//3行2列的按钮=10
    74. {
    75. Delay(20);
    76. while(P1_5==0)
    77. {
    78. Delay(20);
    79. }
    80. KeyNumber=10;
    81. }
    82. if(P1_4==0)//4行2列的按钮=14
    83. {
    84. Delay(20);
    85. while(P1_4==0)
    86. {
    87. Delay(20);
    88. }
    89. KeyNumber=14;
    90. }
    91. P1=0xFF; //默认都是1(高电平)
    92. P1_1=0;
    93. //检测按键按下,就是判断是否变成0
    94. if(P1_7==0)//1行3列的按钮=3
    95. {
    96. Delay(20);
    97. while(P1_7==0)
    98. {
    99. Delay(20);
    100. }
    101. KeyNumber=3;
    102. }
    103. if(P1_6==0)//2行3列的按钮=7
    104. {
    105. Delay(20);
    106. while(P1_6==0)
    107. {
    108. Delay(20);
    109. }
    110. KeyNumber=7;
    111. }
    112. if(P1_5==0)//3行3列的按钮=11
    113. {
    114. Delay(20);
    115. while(P1_5==0)
    116. {
    117. Delay(20);
    118. }
    119. KeyNumber=11;
    120. }
    121. if(P1_4==0)//4行3列的按钮=15
    122. {
    123. Delay(20);
    124. while(P1_4==0)
    125. {
    126. Delay(20);
    127. }
    128. KeyNumber=15;
    129. }
    130. P1=0xFF; //默认都是1(高电平)
    131. P1_0=0;
    132. //检测按键按下,就是判断是否变成0
    133. if(P1_7==0)//1行4列的按钮=4
    134. {
    135. Delay(20);
    136. while(P1_7==0)
    137. {
    138. Delay(20);
    139. }
    140. KeyNumber=4;
    141. }
    142. if(P1_6==0)//2行4列的按钮=8
    143. {
    144. Delay(20);
    145. while(P1_6==0)
    146. {
    147. Delay(20);
    148. }
    149. KeyNumber=8;
    150. }
    151. if(P1_5==0)//3行4列的按钮=12
    152. {
    153. Delay(20);
    154. while(P1_5==0)
    155. {
    156. Delay(20);
    157. }
    158. KeyNumber=12;
    159. }
    160. if(P1_4==0)//4行4列的按钮=16
    161. {
    162. Delay(20);
    163. while(P1_4==0)
    164. {
    165. Delay(20);
    166. }
    167. KeyNumber=16;
    168. }
    169. return KeyNumber;
    170. }

    5.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

    6.LCD1602.c

    1. #include
    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 c;
    116. char i;
    117. int Result=1;
    118. for(i=0;i
    119. {
    120. Result*=X;
    121. }
    122. return Result;
    123. }
    124. /**
    125. * @brief 在LCD1602指定位置开始显示所给数字
    126. * @param Line 起始行位置,范围:1~2
    127. * @param Column 起始列位置,范围:1~16
    128. * @param Number 要显示的数字,范围:0~65535
    129. * @param Length 要显示数字的长度,范围:1~5
    130. * @retval 无
    131. */
    132. void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
    133. {
    134. unsigned char i;
    135. LCD_SetCursor(Line,Column);
    136. for(i=Length;i>0;i--)
    137. {
    138. LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
    139. }
    140. }
    141. /**
    142. * @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
    143. * @param Line 起始行位置,范围:1~2
    144. * @param Column 起始列位置,范围:1~16
    145. * @param Number 要显示的数字,范围:-32768~32767
    146. * @param Length 要显示数字的长度,范围:1~5
    147. * @retval 无
    148. */
    149. void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
    150. {
    151. unsigned char i;
    152. unsigned int Number1;
    153. LCD_SetCursor(Line,Column);
    154. if(Number>=0)
    155. {
    156. LCD_WriteData('+');
    157. Number1=Number;
    158. }
    159. else
    160. {
    161. LCD_WriteData('-');
    162. Number1=-Number;
    163. }
    164. for(i=Length;i>0;i--)
    165. {
    166. LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
    167. }
    168. }
    169. /**
    170. * @brief 在LCD1602指定位置开始以十六进制显示所给数字
    171. * @param Line 起始行位置,范围:1~2
    172. * @param Column 起始列位置,范围:1~16
    173. * @param Number 要显示的数字,范围:0~0xFFFF
    174. * @param Length 要显示数字的长度,范围:1~4
    175. * @retval 无
    176. */
    177. void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
    178. {
    179. unsigned char i,SingleNumber;
    180. LCD_SetCursor(Line,Column);
    181. for(i=Length;i>0;i--)
    182. {
    183. SingleNumber=Number/LCD_Pow(16,i-1)%16;
    184. if(SingleNumber<10)
    185. {
    186. LCD_WriteData(SingleNumber+'0');
    187. }
    188. else
    189. {
    190. LCD_WriteData(SingleNumber-10+'A');
    191. }
    192. }
    193. }
    194. /**
    195. * @brief 在LCD1602指定位置开始以二进制显示所给数字
    196. * @param Line 起始行位置,范围:1~2
    197. * @param Column 起始列位置,范围:1~16
    198. * @param Number 要显示的数字,范围:0~1111 1111 1111 1111
    199. * @param Length 要显示数字的长度,范围:1~16
    200. * @retval 无
    201. */
    202. void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
    203. {
    204. unsigned char i;
    205. LCD_SetCursor(Line,Column);
    206. for(i=Length;i>0;i--)
    207. {
    208. LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
    209. }
    210. }

    7.Delay.h

    1. #ifndef __DELAY_H__
    2. #define __DELAY_H__
    3. void Delay(unsigned int xms);
    4. #endif

    8.Delay.c

    1. void Delay(unsigned int xms)
    2. {
    3. unsigned char data i, j;
    4. while(xms--)
    5. {
    6. i = 2;
    7. j = 199;
    8. do
    9. {
    10. while (--j);
    11. } while (--i);
    12. }
    13. }

    四、主函数

    1. #include
    2. #include "Delay.h"
    3. #include "LCD1602.h"
    4. #include "MatrixKey.h"
    5. #include "Buzzer.h"
    6. unsigned long KeyNum;
    7. unsigned long Password;
    8. int InputCount=0,ErrorCount=0,i=0;
    9. int state=0;
    10. //判断状态0假1真,当密码输入正确的时候状态变更为1,未输入时和密码输入错误是仍然为0
    11. int secret=7355608;
    12. //secret为全局变量,储存密码,默认密码是7355608
    13. void main()
    14. {
    15. LCD_Init();
    16. LCD_ShowString(1,1,"C4");
    17. Delay(1000);
    18. LCD_ShowString(1,1,"DESINGER:MLS");
    19. //字符串内可以加空格,以取代刷新屏幕的作用
    20. Delay(1000);
    21. LCD_ShowString(1,1,"Password: ");
    22. LCD_ShowString(2,1,"*******");
    23. while(1)
    24. {
    25. KeyNum=MatrixKey();
    26. if(KeyNum)//等价于if(KeyNum!=0)----非零即真
    27. {
    28. if(KeyNum<=10)//如果S1~S10按键按下,输入密码
    29. {
    30. if(InputCount<7)
    31. {
    32. InputCount++;
    33. LCD_ShowNum(2,InputCount,KeyNum,1);
    34. }
    35. }
    36. if(KeyNum==11) //如果S11按键按下
    37. {
    38. LCD_ShowNum(2,1,Password,7);
    39. LCD_ShowString(2,1,"*******");
    40. Buzzer_Time(100);
    41. Delay(800);
    42. Buzzer_Time(100);
    43. Delay(800);
    44. Buzzer_Time(100);
    45. Delay(800);
    46. Buzzer_Time(100);
    47. Delay(800);
    48. Buzzer_Time(100);
    49. Delay(750);
    50. Buzzer_Time(100);
    51. Delay(750);
    52. Buzzer_Time(100);
    53. Delay(750);
    54. Buzzer_Time(100);
    55. Delay(750);
    56. Buzzer_Time(100);
    57. Delay(700);
    58. Buzzer_Time(100);
    59. Delay(700);
    60. Buzzer_Time(100);
    61. Delay(700);
    62. Buzzer_Time(100);
    63. Delay(700);
    64. Buzzer_Time(100);
    65. Delay(650);
    66. Buzzer_Time(100);
    67. Delay(650);
    68. Buzzer_Time(100);
    69. Delay(600);
    70. Buzzer_Time(100);
    71. Delay(600);
    72. Buzzer_Time(100);
    73. Delay(600);
    74. Buzzer_Time(100);
    75. Delay(550);
    76. Buzzer_Time(100);
    77. Delay(550);
    78. Buzzer_Time(100);
    79. Delay(550);
    80. Buzzer_Time(100);
    81. Delay(500);
    82. Buzzer_Time(100);
    83. Delay(500);
    84. Buzzer_Time(100);
    85. Delay(500);
    86. Buzzer_Time(100);
    87. Delay(450);
    88. Buzzer_Time(100);
    89. Delay(450);
    90. Buzzer_Time(100);
    91. Delay(400);
    92. Buzzer_Time(100);
    93. Delay(400);
    94. Buzzer_Time(100);
    95. Delay(400);
    96. Buzzer_Time(100);
    97. Delay(400);
    98. Buzzer_Time(100);
    99. Delay(350);
    100. Buzzer_Time(100);
    101. Delay(350);
    102. Buzzer_Time(100);
    103. Delay(350);
    104. Buzzer_Time(100);
    105. Delay(300);
    106. Buzzer_Time(100);
    107. Delay(300);
    108. Buzzer_Time(100);
    109. Delay(300);
    110. Buzzer_Time(100);
    111. Delay(250);
    112. Buzzer_Time(100);
    113. Delay(250);
    114. Buzzer_Time(100);
    115. Delay(200);
    116. Buzzer_Time(100);
    117. Delay(200);
    118. Buzzer_Time(100);
    119. Delay(200);
    120. Buzzer_Time(100);
    121. Delay(150);
    122. Buzzer_Time(100);
    123. Delay(150);
    124. Buzzer_Time(100);
    125. Delay(150);
    126. Buzzer_Time(100);
    127. Delay(100);
    128. Buzzer_Time(100);
    129. Delay(100);
    130. Buzzer_Time(100);
    131. Delay(100);
    132. Buzzer_Time(100);
    133. Delay(100);
    134. Buzzer_Time(100);
    135. Delay(100);
    136. Buzzer_Time(100);
    137. Delay(100);
    138. Buzzer_Time(100);
    139. Delay(100);
    140. Buzzer_Time(100);
    141. Delay(100);
    142. Buzzer_Time(100);
    143. Delay(100);
    144. Buzzer_Time(100);
    145. Delay(100);
    146. Buzzer_Time(100);
    147. Delay(10);
    148. Buzzer_Time(100);
    149. Delay(10);
    150. Buzzer_Time(100);
    151. Delay(10);
    152. Buzzer_Time(100);
    153. Delay(10);
    154. Buzzer_Time(100);
    155. Delay(10);
    156. Buzzer_Time(100);
    157. Delay(10);
    158. Buzzer_Time(100);
    159. Delay(10);
    160. Buzzer_Time(100);
    161. Delay(10);
    162. Buzzer_Time(100);
    163. Delay(1);
    164. Buzzer_Time(100);
    165. Delay(1);
    166. Buzzer_Time(100);
    167. Delay(1);
    168. Buzzer_Time(100);
    169. Delay(1);
    170. Buzzer_Time(100);
    171. Delay(1000);
    172. Buzzer_Time(1000);
    173. }
    174. }
    175. }
    176. }

    五、Keil5界面

    7816ff99d6c74bb6b7105c6caaa8f3a0.png

                                                          (创作不易,多多支持)                             【免费】51单片机:实现CSGO中C4下包功能资源-CSDN文库

                                                                    谢谢大家!

                                                            小白一枚,请多指教!                                                               

                                                             FROM 明月清风mls

     

     

     

     

  • 相关阅读:
    Python爬虫之Scrapy框架(案例练习)
    Mycat中间件分配数据库
    SwiftUI使用MatchedGeometryEffect快速同步不同继承层级中视图的位置和尺寸
    什么蓝牙耳机适合运动、运动用的蓝牙耳机推荐
    ASP.NET Core 在 IIS 下的两种部署模式
    [一篇读懂]C语言十讲:单链表的新建、查找
    计算机毕业设计Java订餐系统(源码+系统+mysql数据库+lw文档)
    技术分享 | 接口自动化测试之JSON Schema模式该如何使用?
    2022.11.24
    FastBert学习笔记
  • 原文地址:https://blog.csdn.net/weixin_67341796/article/details/140383475