目录
51单片机实现CSGO中C4下包
| 按键 | 密码 |
| S1 | 1 |
| S2 | 2 |
| S3 | 3 |
| S4 | 4 |
| S5 | 5 |
| S6 | 6 |
| S7 | 7 |
| S8 | 8 |
| S9 | 9 |
| S10 | 0 |
- if(KeyNum)//等价于if(KeyNum!=0)----非零即真
- {
- if(KeyNum<=10)//如果S1~S10按键按下,输入密码
- {
- if(InputCount<7)
- {
- InputCount++;
- LCD_ShowNum(2,InputCount,KeyNum,1);
-
- }
-
- }
-
- if(KeyNum==11) //如果S11按键按下
- {
- LCD_ShowNum(2,1,Password,7);
- LCD_ShowString(2,1,"*******");
- }
- }
- void Buzzer_Time(unsigned int ms)//蜂鸣器时长
- {
- unsigned int i;
- for(i=0;i
2;i++) - {
- Buzzer=!Buzzer;
- Buzzer_Delay700us();
- //每700微秒翻转一次,一个周期是1.4毫秒,频率是698Hz
-
- }
- }
- #ifndef __BUZZER_H__
- #define __BUZZER_H__
-
- void Buzzer_Time(unsigned int ms);
-
- #endif
- #include
-
- //蜂鸣器端口
- sbit Buzzer=P2^5;
- //sbit位控制宏,可以将单片机的某个引脚或寄存器的特定位映射为一个C语言的变量。
- //格式如下
- //sbit <位变量名>=<引脚或者寄存器地址>.<位号>;
- //解释:位变量名 是给这个位定义的变量名
- //引脚或者寄存地地址 是所控制的引脚或者寄存地地址
- //位号 表示所要控制的位在引脚或寄存器中的位置
-
-
-
- /**
- * @brief 蜂鸣器私有延时函数,延时500us
- * @param 无
- * @retval 无
- */
-
-
- void Buzzer_Delay700us(void)
- {
- unsigned char data i, j;
-
- i = 2;
- j = 61;
- do
- {
- while (--j);
- } while (--i);
-
-
- }
-
-
-
- /**
- * @brief 蜂鸣器发声
- * @param ms(发声时长)
- * @retval 无
- */
-
-
- void Buzzer_Time(unsigned int ms)//蜂鸣器时长
- {
- unsigned int i;
- for(i=0;i
2;i++) - {
- Buzzer=!Buzzer;
- Buzzer_Delay700us();
- //每700微秒翻转一次,一个周期是1.4毫秒,频率是698Hz
-
- }
- }
- #ifndef __MATRIXKEY_H__
- #define __MATRIXKEY_H__
-
- unsigned char MatrixKey();
-
- #endif
- #include
- #include "Delay.h"
- /**
- * @brief 矩阵键盘读取按键键码
- * @param 无
- * @retval KeyNumber 按下按键的键码值
- 如果按键按下不放,程序会停留在此函数,松手的一瞬间,返回按键的键码,没有按键按下的时候放回0
- */
- int MatrixKey()
- {
- //矩阵扫描,按一行或者按一列,依次输入扫描,要扫描的给0,其余给1
- int KeyNumber=0;
-
- P1=0xFF; //默认都是1(高电平)
- P1_3=0;
- //检测按键按下,就是判断是否变成0
- if(P1_7==0)//1行1列的按钮=1
- {
- Delay(20);
- while(P1_7==0)
- {
- Delay(20);
- }
- KeyNumber=1;
- }
-
-
- if(P1_6==0)//2行1列的按钮=5
- {
- Delay(20);
- while(P1_6==0)
- {
- Delay(20);
- }
- KeyNumber=5;
- }
-
-
- if(P1_5==0)//3行1列的按钮=9
- {
- Delay(20);
- while(P1_5==0)
- {
- Delay(20);
- }
- KeyNumber=9;
- }
-
-
- if(P1_4==0)//4行1列的按钮=13
- {
- Delay(20);
- while(P1_4==0)
- {
- Delay(20);
- }
- KeyNumber=13;
- }
-
-
- P1=0xFF; //默认都是1(高电平)
- P1_2=0;
- //检测按键按下,就是判断是否变成0
- if(P1_7==0)//1行2列的按钮=2
- {
- Delay(20);
- while(P1_7==0)
- {
- Delay(20);
- }
- KeyNumber=2;
- }
-
-
- if(P1_6==0)//2行2列的按钮=6
- {
- Delay(20);
- while(P1_6==0)
- {
- Delay(20);
- }
- KeyNumber=6;
- }
-
-
- if(P1_5==0)//3行2列的按钮=10
- {
- Delay(20);
- while(P1_5==0)
- {
- Delay(20);
- }
- KeyNumber=10;
- }
-
- if(P1_4==0)//4行2列的按钮=14
- {
- Delay(20);
- while(P1_4==0)
- {
- Delay(20);
- }
- KeyNumber=14;
- }
-
-
- P1=0xFF; //默认都是1(高电平)
- P1_1=0;
- //检测按键按下,就是判断是否变成0
- if(P1_7==0)//1行3列的按钮=3
- {
- Delay(20);
- while(P1_7==0)
- {
- Delay(20);
- }
- KeyNumber=3;
- }
-
-
- if(P1_6==0)//2行3列的按钮=7
- {
- Delay(20);
- while(P1_6==0)
- {
- Delay(20);
- }
- KeyNumber=7;
- }
-
- if(P1_5==0)//3行3列的按钮=11
- {
- Delay(20);
- while(P1_5==0)
- {
- Delay(20);
- }
- KeyNumber=11;
- }
-
- if(P1_4==0)//4行3列的按钮=15
- {
- Delay(20);
- while(P1_4==0)
- {
- Delay(20);
- }
- KeyNumber=15;
- }
-
-
- P1=0xFF; //默认都是1(高电平)
- P1_0=0;
- //检测按键按下,就是判断是否变成0
- if(P1_7==0)//1行4列的按钮=4
- {
- Delay(20);
- while(P1_7==0)
- {
- Delay(20);
- }
- KeyNumber=4;
- }
-
-
- if(P1_6==0)//2行4列的按钮=8
- {
- Delay(20);
- while(P1_6==0)
- {
- Delay(20);
- }
- KeyNumber=8;
- }
-
-
- if(P1_5==0)//3行4列的按钮=12
- {
- Delay(20);
- while(P1_5==0)
- {
- Delay(20);
- }
- KeyNumber=12;
- }
-
-
- if(P1_4==0)//4行4列的按钮=16
- {
- Delay(20);
- while(P1_4==0)
- {
- Delay(20);
- }
- KeyNumber=16;
- }
-
- return KeyNumber;
- }
- #ifndef __LCD1602_H__
- #define __LCD1602_H__
-
- //用户调用函数:
- void LCD_Init();
- void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);
- void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);
- void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
- void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
- void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
- void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
-
- #endif
- #include
-
- //引脚配置:
- sbit LCD_RS=P2^6;
- sbit LCD_RW=P2^5;
- sbit LCD_EN=P2^7;
- #define LCD_DataPort P0
-
- //函数定义:
- /**
- * @brief LCD1602延时函数,12MHz调用可延时1ms
- * @param 无
- * @retval 无
- */
- void LCD_Delay()
- {
- unsigned char i, j;
-
- i = 2;
- j = 239;
- do
- {
- while (--j);
- } while (--i);
- }
-
- /**
- * @brief LCD1602写命令
- * @param Command 要写入的命令
- * @retval 无
- */
- void LCD_WriteCommand(unsigned char Command)
- {
- LCD_RS=0;
- LCD_RW=0;
- LCD_DataPort=Command;
- LCD_EN=1;
- LCD_Delay();
- LCD_EN=0;
- LCD_Delay();
- }
-
- /**
- * @brief LCD1602写数据
- * @param Data 要写入的数据
- * @retval 无
- */
- void LCD_WriteData(unsigned char Data)
- {
- LCD_RS=1;
- LCD_RW=0;
- LCD_DataPort=Data;
- LCD_EN=1;
- LCD_Delay();
- LCD_EN=0;
- LCD_Delay();
- }
-
- /**
- * @brief LCD1602设置光标位置
- * @param Line 行位置,范围:1~2
- * @param Column 列位置,范围:1~16
- * @retval 无
- */
- void LCD_SetCursor(unsigned char Line,unsigned char Column)
- {
- if(Line==1)
- {
- LCD_WriteCommand(0x80|(Column-1));
- }
- else if(Line==2)
- {
- LCD_WriteCommand(0x80|(Column-1+0x40));
- }
- }
-
- /**
- * @brief LCD1602初始化函数
- * @param 无
- * @retval 无
- */
- void LCD_Init()
- {
- LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
- LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
- LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
- LCD_WriteCommand(0x01);//光标复位,清屏
- }
-
- /**
- * @brief 在LCD1602指定位置上显示一个字符
- * @param Line 行位置,范围:1~2
- * @param Column 列位置,范围:1~16
- * @param Char 要显示的字符
- * @retval 无
- */
- void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
- {
- LCD_SetCursor(Line,Column);
- LCD_WriteData(Char);
- }
-
- /**
- * @brief 在LCD1602指定位置开始显示所给字符串
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param String 要显示的字符串
- * @retval 无
- */
- void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
- {
- unsigned char i;
- LCD_SetCursor(Line,Column);
- for(i=0;String[i]!='\0';i++)
- {
- LCD_WriteData(String[i]);
- }
- }
-
- /**
- * @brief 返回值=X的Y次方
- */
- int LCD_Pow(int X,int Y)
- {
- unsigned c;
- char i;
- int Result=1;
- for(i=0;i
- {
- Result*=X;
- }
- return Result;
- }
-
- /**
- * @brief 在LCD1602指定位置开始显示所给数字
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:0~65535
- * @param Length 要显示数字的长度,范围:1~5
- * @retval 无
- */
- void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
- {
- unsigned char i;
- LCD_SetCursor(Line,Column);
- for(i=Length;i>0;i--)
- {
- LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
- }
- }
-
- /**
- * @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:-32768~32767
- * @param Length 要显示数字的长度,范围:1~5
- * @retval 无
- */
- void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
- {
- unsigned char i;
- unsigned int Number1;
- LCD_SetCursor(Line,Column);
- if(Number>=0)
- {
- LCD_WriteData('+');
- Number1=Number;
- }
- else
- {
- LCD_WriteData('-');
- Number1=-Number;
- }
- for(i=Length;i>0;i--)
- {
- LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
- }
- }
-
- /**
- * @brief 在LCD1602指定位置开始以十六进制显示所给数字
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:0~0xFFFF
- * @param Length 要显示数字的长度,范围:1~4
- * @retval 无
- */
- void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
- {
- unsigned char i,SingleNumber;
- LCD_SetCursor(Line,Column);
- for(i=Length;i>0;i--)
- {
- SingleNumber=Number/LCD_Pow(16,i-1)%16;
- if(SingleNumber<10)
- {
- LCD_WriteData(SingleNumber+'0');
- }
- else
- {
- LCD_WriteData(SingleNumber-10+'A');
- }
- }
- }
-
- /**
- * @brief 在LCD1602指定位置开始以二进制显示所给数字
- * @param Line 起始行位置,范围:1~2
- * @param Column 起始列位置,范围:1~16
- * @param Number 要显示的数字,范围:0~1111 1111 1111 1111
- * @param Length 要显示数字的长度,范围:1~16
- * @retval 无
- */
- void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
- {
- unsigned char i;
- LCD_SetCursor(Line,Column);
- for(i=Length;i>0;i--)
- {
- LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
- }
- }
7.Delay.h
- #ifndef __DELAY_H__
- #define __DELAY_H__
-
- void Delay(unsigned int xms);
-
- #endif
8.Delay.c
- void Delay(unsigned int xms)
- {
- unsigned char data i, j;
- while(xms--)
- {
- i = 2;
- j = 199;
- do
- {
- while (--j);
- } while (--i);
- }
- }
四、主函数
- #include
- #include "Delay.h"
- #include "LCD1602.h"
- #include "MatrixKey.h"
- #include "Buzzer.h"
-
-
-
-
- unsigned long KeyNum;
- unsigned long Password;
- int InputCount=0,ErrorCount=0,i=0;
- int state=0;
- //判断状态0假1真,当密码输入正确的时候状态变更为1,未输入时和密码输入错误是仍然为0
- int secret=7355608;
- //secret为全局变量,储存密码,默认密码是7355608
-
-
-
-
- void main()
- {
- LCD_Init();
- LCD_ShowString(1,1,"C4");
-
- Delay(1000);
- LCD_ShowString(1,1,"DESINGER:MLS");
- //字符串内可以加空格,以取代刷新屏幕的作用
- Delay(1000);
- LCD_ShowString(1,1,"Password: ");
- LCD_ShowString(2,1,"*******");
- while(1)
- {
- KeyNum=MatrixKey();
- if(KeyNum)//等价于if(KeyNum!=0)----非零即真
- {
- if(KeyNum<=10)//如果S1~S10按键按下,输入密码
- {
- if(InputCount<7)
- {
- InputCount++;
- LCD_ShowNum(2,InputCount,KeyNum,1);
-
- }
-
- }
-
- if(KeyNum==11) //如果S11按键按下
- {
- LCD_ShowNum(2,1,Password,7);
- LCD_ShowString(2,1,"*******");
- Buzzer_Time(100);
- Delay(800);
- Buzzer_Time(100);
- Delay(800);
- Buzzer_Time(100);
- Delay(800);
- Buzzer_Time(100);
- Delay(800);
- Buzzer_Time(100);
- Delay(750);
- Buzzer_Time(100);
- Delay(750);
- Buzzer_Time(100);
- Delay(750);
- Buzzer_Time(100);
- Delay(750);
- Buzzer_Time(100);
- Delay(700);
- Buzzer_Time(100);
- Delay(700);
- Buzzer_Time(100);
- Delay(700);
- Buzzer_Time(100);
- Delay(700);
- Buzzer_Time(100);
- Delay(650);
- Buzzer_Time(100);
- Delay(650);
- Buzzer_Time(100);
- Delay(600);
- Buzzer_Time(100);
- Delay(600);
- Buzzer_Time(100);
- Delay(600);
- Buzzer_Time(100);
- Delay(550);
- Buzzer_Time(100);
- Delay(550);
- Buzzer_Time(100);
- Delay(550);
- Buzzer_Time(100);
- Delay(500);
- Buzzer_Time(100);
- Delay(500);
- Buzzer_Time(100);
- Delay(500);
- Buzzer_Time(100);
- Delay(450);
- Buzzer_Time(100);
- Delay(450);
- Buzzer_Time(100);
- Delay(400);
- Buzzer_Time(100);
- Delay(400);
- Buzzer_Time(100);
- Delay(400);
- Buzzer_Time(100);
- Delay(400);
- Buzzer_Time(100);
- Delay(350);
- Buzzer_Time(100);
- Delay(350);
- Buzzer_Time(100);
- Delay(350);
- Buzzer_Time(100);
- Delay(300);
- Buzzer_Time(100);
- Delay(300);
- Buzzer_Time(100);
- Delay(300);
- Buzzer_Time(100);
- Delay(250);
- Buzzer_Time(100);
- Delay(250);
- Buzzer_Time(100);
- Delay(200);
- Buzzer_Time(100);
- Delay(200);
- Buzzer_Time(100);
- Delay(200);
- Buzzer_Time(100);
- Delay(150);
- Buzzer_Time(100);
- Delay(150);
- Buzzer_Time(100);
- Delay(150);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(100);
- Buzzer_Time(100);
- Delay(10);
- Buzzer_Time(100);
- Delay(10);
- Buzzer_Time(100);
- Delay(10);
- Buzzer_Time(100);
- Delay(10);
- Buzzer_Time(100);
- Delay(10);
- Buzzer_Time(100);
- Delay(10);
- Buzzer_Time(100);
- Delay(10);
- Buzzer_Time(100);
- Delay(10);
- Buzzer_Time(100);
- Delay(1);
- Buzzer_Time(100);
- Delay(1);
- Buzzer_Time(100);
- Delay(1);
- Buzzer_Time(100);
- Delay(1);
- Buzzer_Time(100);
- Delay(1000);
- Buzzer_Time(1000);
-
- }
- }
- }
- }
五、Keil5界面

(创作不易,多多支持) 【免费】51单片机:实现CSGO中C4下包功能资源-CSDN文库
谢谢大家!
小白一枚,请多指教!
FROM 明月清风mls