Blue Bridge Cup MCU | Special training case [advanced 02] long press and short press to control digital tube display

【1】Title requirements

Create a new project, write code in I/O mode , and implement the following functions on the CT107D MCU comprehensive training platform:

1. After the system is powered on, turn off the buzzer and relay, turn off all indicator lights, the rightmost two digits of the digital tube display the initial count value of 28, and the rest of the digital tubes are turned off.

2. Use timer 0 to achieve 10ms interval timing, which is used as the basic unit of long timing to record the duration of key presses.

3. Short-circuit pin 23 of J5 and set S4 as an independent button .

4. The S4 button is scanned circularly, the pressing time is less than 1 second , it is a short press , the number on the digital tube is incremented by 1, and when it exceeds the maximum value of 99, it returns to 00 and continues to count; the pressing time of the S4 button is more than 1 second , it is a long press , and the digital The count on the tube is cleared to 00. 

5. In the process of key scanning and processing, the normal display of the digital tube cannot be interfered with, so as to avoid the accidental triggering of keys and multiple processing of one key.

[Tips]: The realization principle of the long press and short press of the button can be seen in this blog: "[CC2530 Intensive Training 01] The Ordinary Delay Function Realizes the Long Press and Short Press of the button" .

[2] Core source code

/*==================蓝桥杯单片机特训==================
【进阶02】:长按与短按控制数码管显示
**平  台:CT107D单片机综合实训平台
**模  式:IO模式
**设  计:欧浩源(小蜜蜂老师,[email protected])
**时  间:2022-03-31
**更多详见:www.xmf393.com
====================================================*/
#include "reg52.h"

sbit S4 = P3^3;

//定义动态显示中单个数码管点亮时长
#define TSMG	500
//-------共阳数码管的段码编码表(无小数点)--------
//0 1 2 3 4 5 6 7 8 9 A b C D E F - .
unsigned char code SMG_NoDot[18]={0xc0,0xf9,
	  0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
    0x88,0x83,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};

unsigned char num = 28;					//定义计数变量
bit F_key = 0;    						//按键按下标志
unsigned int count_t = 0;				//10ms单位累计
void Display_Num();						//声明数码管显示函数		
/*====锁存器设置和数码管显示的代码参考前面的案例====*/
//===================定时器T0初始化===================
void Init_Timer0()
{
	//定时器计数初值设置
	TH0 = (0 - 10000) / 256;
	TL0 = (0 - 10000) % 256;
	TMOD = 0x01;                //模式1:16位非重装模式
	ET0 = 1;					//使能定时器0中断
	EA = 1;						//打开总中断
	TR0 = 1;					//启动定时器0
}
//===============定时器T0的中断服务函数================
void Service_Timer0() interrupt 1
{
	TH0 = (0 - 10000) / 256;
	TL0 = (0 - 10000) % 256;
	
	if(F_key == 1)				//在按键按下期间进行计数
	{
		count_t++;				//按键按下的持续时间
	}
}
//==================按键扫描处理函数===================
void Scan_Keys()
{
	if(S4 == 0)
	{
		DelaySMG(1000);			//去抖动处理
		if(S4 == 0)				//确认为按下信号
		{
			count_t = 0;		//时间计数变量清0
			F_key = 1;			//标志按键按下状态
			while(S4 == 0)		//等待按键松开
			{
				Display_Num();	//在按下期间保持数码管正常显示
			}
			F_key = 0;			//标志按键松开状态
			
			if(count_t > 50)	//按下时间大于1秒,长按
			{
				num = 0;		//数码管计数清除为00
			}
			else				//按下时间小于1秒,短按
			{
				num++;			//数码管计数加1
				if(num == 100)
				{
					num = 0;	//计数超出最大值99后恢复00
				}
			}
		}
	}
}

[Note]: For more information about the Blue Bridge Cup MCU preparation content, please refer to the "Blue Bridge Cup MCU Design and Development" Little Bee Special Training Manual, which can be downloaded from this site, and related more Blue Bridge Cup case complete source code and study preparations Competition notes, welcome to the official account of " Little Bee Notes ".

Guess you like

Origin blog.csdn.net/ohy3686/article/details/123898645