从单片机基础到程序框架(全集 2019pdf版).pdf - 第619页

sbit KEY_I NPUT1=P2^ 2; sbit KEY_I NPUT2=P2^ 1; sbit P1_0= P1^0; sbit P1_1= P1^1; sbit P1_2= P1^2; sbit P1_3= P1^3; sbit P3_4= P3^4; //数码管转换 表 code unsig ned char Cu 8DigTable[]= { 0x3f, //0 序号 0 0x06, //1 序号 1 0x5b, //2…

100%1 / 836
上一节讲“累加式”的秒表,这一节讲“递减式的倒计时。通过此小项目,加深理解在显示框架中常
用的更新变量(整屏更新和局部更新)以及应用程序与按键是如何关联的框架。同时,在拆分“个十百千
万...”的时候,有一处地方必须注意,“先整除后求余”必须用一行代码一气呵成不能拆分成两行代码“先
整除;后求余”,否则会有隐患会有 bug。除非,把四个临时变都改成 unsigned long 类型。比如:
以下这样是对的:
static unsigned char Su8Temp_1;
Su8Temp_1=vGu32CountdownTimerCnt/10%10; //一气呵成,没毛病。
以下这样是有隐患有 bug 的(除非把 Su8Temp_1 改成 unsigned long 类型
static unsigned char Su8Temp_1;
Su8Temp_1=vGu32CountdownTimerCnt/10;
Su8Temp_1=Su8Temp_1%10; //拆分成两行代码后,有隐患有 bug。数据溢出的原因引起。
本节倒计时程序的功能K1 按键是复位按键,每按一次,倒计时停止并且重新恢复初始值 10.00 秒。K2
按键是启动按键,当秒表处于复位后停止的状态时按一次则开始启动倒计时,当倒计时变 0.00 秒的时候,
蜂鸣器发出一次“滴”的提示声。此时,如果想启动新一轮的倒计时,只需按一次 K1 复位键,然后再按 K2
启动按键,就可以启动新一轮 10.00 秒的倒计时。代码如下:
#include "REG52.H"
#define KEY_FILTER_TIME 25
#define SCAN_TIME 1
#define VOICE_TIME 50 //蜂鸣器一次“滴”的声音长度 50ms
void T0_time();
void SystemInitial(void) ;
void Delay(unsigned long u32DelayTime) ;
void PeripheralInitial(void) ;
void KeyScan(void);
void KeyTask(void);
void VoiceScan(void); //蜂鸣器的驱动函数
void DisplayScan(void); //底层显示的驱动函数
void DisplayTask(void); //上层显示的任务函数
void RunTask(void); //倒计时的应用程序
void BeepOpen(void);
void BeepClose(void);
sbit KEY_INPUT1=P2^2;
sbit KEY_INPUT2=P2^1;
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
sbit P3_4=P3^4;
//数码管转换
code unsigned char Cu8DigTable[]=
{
0x3f, //0 序号 0
0x06, //1 序号 1
0x5b, //2 序号 2
0x4f, //3 序号 3
0x66, //4 序号 4
0x6d, //5 序号 5
0x7d, //6 序号 6
0x07, //7 序号 7
0x7f, //8 序号 8
0x6f, //9 序号 9
0x00, //不显 序号 10
};
//数码管底层驱动扫描的软件定时器
volatile unsigned char vGu8ScanTimerFlag=0;
volatile unsigned int vGu16ScanTimerCnt=0;
//倒计时的软件定时器,注意,这里是 unsigned long 类型,范围 0 4294967295 毫秒
volatile unsigned char vGu8CountdownTimerFlag=0;
volatile unsigned long vGu32CountdownTimerCnt=10000; //开机默认显示初始值 10.000
//数码管上层 10ms 就定时刷新一次显示的软件定时器。用于及时更新显示秒表当前的实时数
volatile unsigned char vGu8UpdateTimerFlag=0;
volatile unsigned int vGu16UpdateTimerCnt=0;
//蜂鸣器的软件定时器
volatile unsigned char vGu8BeepTimerFlag=0;
volatile unsigned int vGu16BeepTimerCnt=0;
unsigned char Gu8RunStart=0; //应用程序的总启动
unsigned char Gu8RunStep=0; //应用程序的总运行步骤。建议跟 vGu8RunStart 成双成对出
unsigned char Gu8RunStatus=0; //当前倒计时的状态。0 代表停止,1 代表正在工作中
unsigned char Gu8WdUpdate=1; //开机默认整屏更新一次显示。此变量在显示框架中是非常重要的变
volatile unsigned char vGu8Display_Righ_4=1; //显示“1”,跟 vGu32CountdownTimerCnt 高位一致
volatile unsigned char vGu8Display_Righ_3=0;
volatile unsigned char vGu8Display_Righ_2=0;
volatile unsigned char vGu8Display_Righ_1=0;
volatile unsigned char vGu8Display_Righ_Dot_4=0;
volatile unsigned char vGu8Display_Righ_Dot_3=1; //开机默认保留显示 2 个小数点
volatile unsigned char vGu8Display_Righ_Dot_2=0;
volatile unsigned char vGu8Display_Righ_Dot_1=0;
volatile unsigned char vGu8KeySec=0;
void main()
{
SystemInitial();
Delay(10000);
PeripheralInitial();
while(1)
{
KeyTask(); //按键的任务函数
DisplayTask(); //数码管显示的上层任务函数
RunTask(); //倒计时的应用程序
}
}
void KeyTask(void) //按键的任务函数
{
if(0==vGu8KeySec)
{
return;
}
switch(vGu8KeySec)
{
case 1: //复位按键
Gu8RunStatus=0; //倒计时返回停止的状态
Gu8RunStart=0; //倒计时的运行步骤的停止
Gu8RunStep=0; //总运行步骤归零。建议跟 vGu8RunStart 成双成对出