Single chip microcomputer three key modes single click double click long press

Publisher:睿智之光Latest update time:2019-03-28 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/**************************************************************************

Function: key scan

Entry parameters: Double-click waiting time

Return value: button status 0: no action 1: single click 2: double click

key=0 means pressing

**************************************************************************/

u8 click_N_Double (u8 time)

{

static u8 flag_key,count_key,double_key;

static u16 count_single,Forever_count;

if(KEY2==0) Forever_count++; //Long press flag is not set to 1

else Forever_count=0;

if(0==KEY2&&0==flag_key) flag_key=1;

if(0==count_key)

{

if(flag_key==1)//Click once to enter once

{

double_key++;

count_key=1;

}

if(double_key==2) //Judge double click

{

double_key=0;

count_single=0;

return 2; //Double click to execute command

}

}

if(1==KEY2) flag_key=0,count_key=0;

if(1==double_key)

{

count_single++;

if(count_single>time&&Forever_count

{

//The interval is too long to be considered a single click

double_key=0;

count_single=0;

return 1; //Click to execute the command

}

if(Forever_count>time)

{

//Long press

double_key=0;

count_single=0;

}

}

return 0;

}



/**************************************************************************

Function: key scan (the operation here is mainly for single click)

Entry parameters: None

Return value: button status 0: no action 1: click

**************************************************************************/

u8 click(void)

{

static u8 flag_key=1;//button press and release flag

if(flag_key&&KEY==0)

{

flag_key=0;

return 1; // Button pressed

}

else if(1==KEY) flag_key=1;

return 0; //No button pressed

}




/**************************************************************************

Function: long press detection

Entry parameters: None

Return value: button status 0: no action 1: long press 2s

**************************************************************************/

u8 Long_Press(void)

{

static u16 Long_Press_count,Long_Press;

if(Long_Press==0&&KEY==0) Long_Press_count++; //Long press flag is not set to 1

else Long_Press_count=0;

if(Long_Press_count>200)

{

Long_Press=1; //This is to control the long press time

Long_Press_count=0;

return 1;

}

if(Long_Press==1) //Long press mark position 1

{

Long_Press=0;

}

return 0;

}

Keywords:MCU Reference address:Single chip microcomputer three key modes single click double click long press

Previous article:Detailed explanation of the magic of STM32 buttons
Next article:STM32 Getting Started Development--LED Module to Implement Marquee

Recommended ReadingLatest update time:2024-11-16 16:03

Design of single chip digital thermometer
1. Basic knowledge of temperature sensor AD590 The current generated by AD590 is proportional to the absolute temperature. It can accept an operating voltage of 4V-30V and a detection temperature range of -55℃-+150℃. It has very good linear output performance. For every 1℃ increase in temperature, its current increa
[Microcontroller]
Design of single chip digital thermometer
STC15 MCU uses HC-SR04 ultrasonic ranging module
【HC-SR04】 HC-HR04 ultrasonic distance measurement can provide non-contact distance sensing function of 2cm-400cm, and the ranging accuracy can reach 3mm. Basic working principle 1. Use IO port TRIG to trigger the ranging and give a high level signal of at least 10us. 2. The module automatically sends 8 40KHz squar
[Microcontroller]
STC15 MCU uses HC-SR04 ultrasonic ranging module
51 single chip microcomputer digital tube and matrix keyboard
Use a 4*4 matrix keyboard and a digital tube The characters 0-F are encoded on the 4*4 matrix keyboard, and a digital tube displays the characters entered on each keyboard. //4*4 keyboard detection program, after pressing the key, the corresponding code is displayed on the digital tube   #include reg51.h   typedef uns
[Microcontroller]
Design of electronic password lock based on STC89C52 single chip microcomputer
introduction In the past, personal data storage and departmental file archives mostly used mechanical locks, which required carrying multiple keys, making them extremely inconvenient to use and had poor security if the keys were lost. With the continuous development of science and technology, peo
[Microcontroller]
Design of electronic password lock based on STC89C52 single chip microcomputer
A digital video mixer based on single chip microcomputer control
In recent years, digital television technology has developed rapidly. Digital video mixers can mix two or more digital video signals into one or more digital video signals according to certain algorithms. As the basic processing unit of digital video signals, the mixer plays a vital role in the editing, production,
[Microcontroller]
51 single chip microcomputer frequency meter Proteus simulation design (with program circuit)
The Proteus simulation circuit diagram is as follows: (There will be a clear picture after downloading the simulation file) There are circuit diagrams and programs. The circuit is from isis and the program is from uv3. Download the simulation project file: http://www.51hei.com/f/dgnplj.rar    Below is the progra
[Microcontroller]
51 single chip microcomputer frequency meter Proteus simulation design (with program circuit)
Playing with MCU Part 5 - Serial port multi-data transmission and reception
#include reg51.h #define uchar unsigned char    //byte #define uint unsigned int    //word sbit led1=P0^0;  sbit led2=P0^1;  #define INBUF_LEN 8 //data length  unsigned char inbuf ; //data storage array unsigned char count3; //counter //Send a character to the serial port  void send_char_com( unsigned
[Microcontroller]
Playing with MCU Part 5 - Serial port multi-data transmission and reception
AVR MCU ADC Experiment
#include iom128v.h #include macros.h volatile unsigned char i=0x00; /*-------------------------------------------------------------------- Program name: AD conversion initialization program Program function: Notes: Tips: Input: Return: --------------------------------------------------------------------*
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号