Single button, single click + double click + long press

Publisher:shengjuLatest update time:2019-11-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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;

}

}


Reference address:Single button, single click + double click + long press

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

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号