广告
返回顶部
首页 > 资讯 > 移动开发 >C51模拟CSGO的C4炸弹小程序
  • 843
分享到

C51模拟CSGO的C4炸弹小程序

小程序 2023-10-27 22:10:22 843人浏览 八月长安
摘要

本文章使用的道具有LCD1602显示屏、矩阵键盘、这两个,可以买普中的C51练习板这些东西都会带有。 规则: 开始用矩阵键盘输入密码7355608按S16启动炸弹 炸弹倒计时15秒后爆炸 按住独立按键拆弹,拆弹时间为3.5秒 S14为重置游

本文章使用的道具有LCD1602显示屏、矩阵键盘、这两个,可以买普中的C51练习板这些东西都会带有。

规则:

开始用矩阵键盘输入密码7355608按S16启动炸弹

炸弹倒计时15秒后爆炸

按住独立按键拆弹,拆弹时间为3.5秒

S14为重置游戏(炸弹启动后无法重置),S15为清空密码重新输入,S16为确定,S1-S9为1-9,S10为0

如图所示

  

一、 LCD1602的代码编程

这个现实屏幕的代码编程参考了B站up主江协科技的部分代码,编写思路也差不多,详情见链接

http://【51单片机入门教程-2020版 程序全程纯手打 从零开始入门】 https://www.bilibili.com/video/BV1Mb411e7re/https://blog.csdn.net/qq2606393744/article/details/?p=32&share_source=copy_web&vd_source=a87f3237d624c356d11c0b6df1a6ff91

代码部分

#include sbit LCD_RS=P2^6;sbit LCD_RW=P2^5;sbit LCD_E=P2^7;#define LCD_DataPort P0//将LCD1602的D0口直接换成P0void LCD_Delay()//@12.000MHz,延时一毫秒,1602执行LCD—E用{unsigned char i, j;i = 2;j = 239;do{while (--j);} while (--i);}//写入指令void LCD_WriteCommand(unsigned char Command){        LCD_RS=0;       //根据LCD1602写操作时序图,给低电平,之后同理LCD_RW=0;LCD_DataPort=Command;LCD_E=1;LCD_Delay();         //延时由于指令执行时间问题,速度太快可能读不过来LCD_E=0;LCD_Delay();}//写入数据void LCD_WriteData(unsigned char Data){LCD_RS=1;       LCD_RW=0;LCD_DataPort=Data;LCD_E=1;LCD_Delay();         LCD_E=0;LCD_Delay();}//1602初始化void LCD_Init(void){LCD_WriteCommand(0x38);   //八位数据接口,两行显示,5*7点阵    LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动LCD_WriteCommand(0x01);//清屏}//设置光标位置void LCD_setCursor(unsigned char Line,unsigned char Column){if(Line==1){LCD_WriteCommand(0x80|(Column-1));//由计算得}else{LCD_WriteCommand(0x80|(Column-1)+0x40);}}//1602显示字符void LCD_showChar(unsigned char Line, unsigned char Column, unsigned char Char) //显示字符(指定行,列,字符){    LCD_setCursor(Line, Column);    LCD_WriteData(Char);}//在LCD1602指定位置开始显示所给字符串void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char *String){unsigned char i;LCD_setCursor(Line,Column);for(i=0;String[i]!='\0';i++){LCD_WriteData(String[i]);}}//返回值=X的Y次方int LCD_Pow(int X,int Y){unsigned char i;int Result=1;for(i=0;i0;i--){LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');}}//在LCD1602显示字符串,随着循环length次显示第i个字符void LCD_ShowCharlist(unsigned char Line,unsigned char Column,char *String,unsigned char length){unsigned char i;LCD_setCursor(Line,Column);for(i=0;i

允许用户调用函数

.h文件配置,使用方法基本上大同小异我只给出两段演示

#ifndef __LCD1602_H__#define __LCD1602_H__//用户调用函数void LCD_Init(void);void LCD_showChar(unsigned char Line,unsigned char Column,unsigned char Char);void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char *String);void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);void LCD_ShowCharlist(unsigned char Line,unsigned char Column,char *String,unsigned char length);#endif

 二、 矩阵键盘代码编程

同样参考了江科自协的51入门教程,链接如下

http://【51单片机入门教程-2020版 程序全程纯手打 从零开始入门】 https://www.bilibili.com/video/BV1Mb411e7re/https://blog.csdn.net/qq2606393744/article/details/?p=15&share_source=copy_web&vd_source=a87f3237d624c356d11c0b6df1a6ff91

代码

#include #include "Delay.h"unsigned char MatrixKey(){  unsigned char KeyNumber=0;  P1=0xFF;  P1_3=0;  if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=1;}  if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=5;}  if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=9;}  if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=13;}P1=0xFF;  P1_2=0;  if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=2;}  if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=6;}  if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=10;}  if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=14;}P1=0xFF;  P1_1=0;  if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=3;}  if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=7;}  if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=11;}  if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=15;}P1=0xFF;  P1_0=0;  if(P1_7==0){Delay(20);while(P1_7==0);Delay(20);KeyNumber=4;}  if(P1_6==0){Delay(20);while(P1_6==0);Delay(20);KeyNumber=8;}  if(P1_5==0){Delay(20);while(P1_5==0);Delay(20);KeyNumber=12;}  if(P1_4==0){Delay(20);while(P1_4==0);Delay(20);KeyNumber=16;}  return KeyNumber;}

允许用户调用函数

.h文件配置,使用方法基本上大同小异我只给出两段演示

#ifndef __MATRIXKEY_H__#define __MATRIXKEY_H__unsigned char MatrixKey();#endif

 

  、 延时代码编程

多次用到,你也可以用在主函数里,利用stc-isp生成。需要.h文件调用。

#include voidDelay(unsigned int xms)//@12.000MHz{unsigned char i, j;while(xms--){i = 2;j = 239;do{while (--j);} while (--i);  }}void Delayus()//@12.000MHz{unsigned char i;_nop_();i = 47;while (--i);}

四、 定时器代码编程

需要.h文件调用。

#include void Timer0Init(void){ TMOD&=0xF0; //把TMOD的低四位清零,高四位保持不变 TMOD|=0x01; //把TMOD的最低位置1,高四位保持不变 TL0=0x18;   //设置定时器初值 TH0=0xFC; //设置定时器初值 TF0=0;      //清除TF0标志 TR0=1;      // 定时器0开始计时 ET0=1;//打开定时器 EA=1;//打开总中断 PT0=0;   }//定时器中断函数模板//void Timer0_Routine() interrupt 1//{// static unsigned int T0Count;// TL0=0x18;// TH0=0xFC; // T0Count++;// if(T0Count>=1000)// {//   T0Count=0;        // }//}

五、 独立按键编程

需要.h文件调用。

#include "Key.h"unsigned char key_scan(){if(KEY1==0){//while(KEY1==0);return KEY1_PRESS;}else if(KEY2==0){//while(KEY2==0);return KEY2_PRESS;}else if(KEY3==0){//while(KEY3==0);return KEY3_PRESS;}else if(KEY4==0){//while(KEY4==0);return KEY4_PRESS;}else{return KEY_UNPRESS;}}

主函数代码

思路就是利用指针LCD显示多位密码,简易密码加上判断条件。这个代码的缺点是无法加速报警,加速报警之后会和拆弹抢时间。

#include #include "LCD1602.h"  //LCD1602#include "MatrixKey.h"//矩阵键盘#include "Delay.h"//延时#include "Timer0.h"//定时器#include "Key.h"//独立按键#define len_pd 7//密码长度sbit Buzzer=P2^5;//定义蜂鸣器为P2.5unsigned char KeyNum,Sec=15,flag,cai=0;//定义字节,keyNum为矩阵按键、Sce为爆炸倒计时秒数、flag为判断字符、cai为拆弹密码计数unsigned int j,i;//定义整数j、iint Count = 0;//定义输入密码计数unsigned char PassWord[len_pd]={'7','3','5','5','6','0','8'};//密码unsigned char Temp_Password[len_pd];//中间变量void Buzzer_Time(unsigned int ms){//蜂鸣器配置unsigned int i;for (i=0;i=1000)//若定时器小于等于1000毫秒,计数清零,Sec自减 { T0Count=0; Sec--; if(Sec){//报警,让蜂鸣器随秒数响for(i=0;i<100;i++){Buzzer=~Buzzer;Delay(1); } } }}

整个编程图

 

来源地址:https://blog.csdn.net/qq2606393744/article/details/131637647

--结束END--

本文标题: C51模拟CSGO的C4炸弹小程序

本文链接: https://www.lsjlt.com/news/504252.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作