Key function: single click, double click, long press
If a button is pressed for more than 1.5 seconds, it is considered a long press event.
If the time interval between two consecutive presses does not exceed 350ms, it is considered a double-click event.
Except for the above two items, all key presses belong to single-click events.
Without further ado, let's get straight to the code. If you have any questions, please feel free to discuss in the comments section.
/* This function runs once every 1ms so the time unit is 1ms */
#define KEY_LP_TIME 1500 /* Long press time (unit: ms) */
#define KEY_DOUBLE_TIME 350 /* If the interval between two presses does not exceed this value, it will be a double-click event*/
uint8_t KEY1_LP_FLG; /* Button long press flag */
uint8_t KEY1_PR_FLG; /* Button pressed flag */
uint8_t Key1LtDelay; /* Debounce time after key is lifted */
uint8_t Key1PrDelay; /* Debounce time after pressing the button */
uint8_t Key1PrCnt; /* Count the number of times a button is pressed to detect double clicks */
uint16_t Key1PrTimeCnt; /* Count the duration of pressing twice in a row to detect double click */
uint16_t Key1PrTime; /* Count the duration of button press to detect long press */
void Key1Scan(void){ //1ms scan once
if(KEY1 == 0){
Key1LtDelay = 0;
if(!KEY1_PR_FLG){
Key1PrDelay++;
if(Key1PrDelay>20){
Key1PrDelay = 0;
KEY1_PR_FLG = 1;
/********** User Code **********/
Key1PrCnt++; /*Count the number of times pressed*/
if(Key1PrCnt>2){ Key1PrCnt = 0; }
KEY1_LP_FLG = 0;
Key1PrTime = 0;
/**************** End *************/
}
}
}
else{
Key1PrDelay = 0;
if(KEY1_PR_FLG){
Key1LtDelay++;
if(Key1LtDelay>20){
Key1LtDelay = 0;
KEY1_PR_FLG = 0;
/********** User Code **********/
KEY1_LP_FLG = 0; /* Lift the button and touch the long press mark */
/* When a button is pressed but exceeds the double-click detection time but does not reach the long-press time, it is considered a single-click event*/
if(Key1PrTime >= KEY_DOUBLE_TIME){
//Click event
}
/**************** End *************/
}
}
}
//Long press detection
if(KEY1_PR_FLG){
if(!KEY1_LP_FLG){
Key1PrTime++;
if(Key1PrTime >= KEY_LP_TIME){
Key1PrTime = 0;
KEY1_LP_FLG = 1; /* Long press for 1.5S to set the long press flag*/
/********** User Code **********/
//Long press event
/************ End *************/
}
}
}
//Double click detection
if(Key1PrCnt==1){
if(Key1PrTimeCnt < KEY_DOUBLE_TIME){ Key1PrTimeCnt++; }
else{
if(!KEY1_PR_FLG){
//Click event
}
Key1PrTimeCnt = 0;
Key1PrCnt = 0;
}
}
else if(Key1PrCnt==2){
if(Key1PrTimeCnt < KEY_DOUBLE_TIME){
//Double click event
}
Key1PrCnt = 0;
Key1PrTimeCnt = 0;
}
}
Previous article:STM8l configuration before entering low power consumption
Next article:Eliminate the influence of abnormal reset of the microcontroller caused by external interference through software program
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Another technical solution for power-type plug-in hybrid: A brief discussion on Volvo T8 plug-in hybrid technology
- Online estimation of generator parameters based on multi-sensor data fusion
- I want to ask when will the e-coins for this Fujitsu event be distributed?
- The virtual machine suddenly becomes very stuck
- The active energy register of ADE7878A chip does not count
- [Evaluation of SGP40] 3. UART control test
- Serial port controller working principle block diagram
- EEWORLD University Hall----Live Replay: Fujitsu Empowers Automotive Electronics Technology Transformation
- Embedded: These three elements of C language should be used well
- 【Contactless facial recognition access control system】+ 2-System composition and functions
- Fault Detection of Small Signal Analog Amplifier Circuit