STM32F1 Development Guide Note 19-Advanced usage of keys-click, double click, long press, slide

The buttons are divided into mechanical buttons and (capacitive / resistive) touch buttons.
The mechanical keys need anti-shake operation, and the high sensitivity of the touch keys can eliminate the anti-shake process.

Button click is a routine operation, but how to double-click and slide?

Double-click judgment:
After the button is pressed and released, determine whether the button is pressed again within a specified period of time. If it is pressed again, it is a double-click operation. Note that the specified period of time is shorter (several hundreds of milliseconds ).

Long press judgment:
Press the button and hold it for a specified period of time, you can judge it is a long press operation, pay attention to this specified period of time is longer (a few seconds).

Slide operation: slide
from A key to B key, how to judge whether it is sliding instead of A key and B key click separately: when A key is pressed and A key and B key are held at the same time during quick release, then release Press the A key and hold down the B key. There is a time slot holding the AB key at the same time. This operation can be judged as sliding.

code show as below:

#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "touch_key.h"
#include "usart.h"

#define KEYA_SPEED1	100	  //长按的时间长度(单位10mS),总共1S
#define KEYA_SPEED2	10	  //双击的时间长度(单位20mS),总共200ms

int main (void){//主程序
	u16 k=1000;	//用于滑动加减计数
	u8 a=0,b,c=0;
	u8 s=0; //刚刚结束滑动标志
	RCC_Configuration(); //系统时钟初始化 
	USART1_Init(115200); //串口初始化,参数中写波特率
	LED_Init();//LED初始化
	TOUCH_KEY_Init();//按键初始化

	while(1)
	{
//A
		if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2))	//检测按键是否按下
		{ 
			delay_ms(20); //延时去抖动
			if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2))	//判断长短键
			{
				while((!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2))&&c<KEYA_SPEED1) //循环判断长按,到时跳转
				{ 
					c++;
					delay_ms(10); //长按判断的计时,1S
				}
				if(c>=KEYA_SPEED1)	 //长键处理
				{
					//长按后执行的程序放到此处
					GPIO_WriteBit(GPIOB,GPIO_Pin_5,(BitAction)(0));//LED控制
					printf("A键长按 \r\n");
					while(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)); //等待按键松开
				}
				else	//单击处理
				{ 
					if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_3))	//判断B键按下
					{
						k++; //用于显示的计数值
						printf("A键右滑 %d \r\n",k); 
						a=1;s=1; //a是单双击判断标志,s是刚刚结束滑动标志
					}
					if(a==0)
					{
						for(b=0;b<KEYA_SPEED2;b++)	//检测双击
						{
							delay_ms(20);
							if(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2))
							{
								a=1;
								//双击后执行的程序放到此处
								GPIO_WriteBit(GPIOE,GPIO_Pin_5,(BitAction)(0));//LED控制
								printf("A键双击 \r\n");
								while(!GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2));	//等待按键松开
							}
						}
						if(a==0)	//判断单击
						{ 
							if(s==1)	//判断是不是刚执行完滑动操作
							{ 
								s=0; //如果是则本次不执行单击处理(因为是滑动的放开操作)
							}
							else	//如果不是,则正常执行单击处理
							{	 
								//单击后执行的程序放到此处
								//GPIO_WriteBit(LEDPORT,LED1|LED2,(BitAction)(0));//LED控制
								GPIO_WriteBit(GPIOB,GPIO_Pin_5,(BitAction)(1));
								GPIO_WriteBit(GPIOE,GPIO_Pin_5,(BitAction)(1));
								printf("A键单击 \r\n");
							}
						}
					}
				}
				a=0;c=0; //参数清0
			}
		}
	}
}
	

Code analysis:

First determine whether to long press (low level is maintained for 1 second), then determine whether to slide to the right (the B button is pressed simultaneously), and then determine whether to double-click (press the button again within 200 milliseconds), if it is not a click.

Experimental phenomena:

Insert picture description here

Disclaimer: This article refers to Du Yang's STM32 teaching.

Published 35 original articles · Like1 · Visits1713

Guess you like

Origin blog.csdn.net/qq_38958704/article/details/105616760