当前位置:网站首页>Blue Bridge Cup stm32g431 - three lines of code for keys (long press, short press, click, double click)
Blue Bridge Cup stm32g431 - three lines of code for keys (long press, short press, click, double click)
2022-06-30 02:03:00 【lzya.】
Three lines of code for the key ( Long press 、 Short press 、 single click 、 double-click )
Three lines of code for the key
key_value = KEY_Scan(); // Read the key value of the key
key_up = key_value & (key_old ^ key_value); // The rising edge of the key is detected Only effective at the moment when the key is lifted All other moments are null and void
key_down = ~key_value & (key_old ^ key_value); // The falling edge of the key is detected It is only effective at the moment when the key is pressed All other moments are null and void
key_old = key_value; // Record the key value after the last key press
Long press of the key 、 Short press
Long press the key 、 It's easy to follow a short train of thought , When the first key is pressed key_down The falling edge starts timing , Given a timing time , If in this plan
Detected within the time key_up Rising edge ( Means the key is released ), Short press . If... Is not detected within this timing time key_up Rising edge , be
For long press .
uint8_t KEY_Time_Count;
uint8_t key_value,key_up,key_down,uc_Key_Value;
static uint8_t key_old;
void KEY_Proc(void)
{
if(uwTick - uwTick_KEY_Set_Point<10) return; // Deceleration function
uwTick_KEY_Set_Point=uwTick;
key_value = KEY_Scan();
key_up = key_value & (key_old ^ key_value);
key_down = ~key_value & (key_old ^ key_value);
key_old = key_value;
if(key_down) // When a key is pressed
{
KEY_Time_Count = 0; Clear the timer Time from zero Here a basic timer is used for timing
}
if(KEY_Time_Count < 10) // If the timing time is less than 1s Short press
{
switch(key_up) // Judge whether the key is lifted Select the key value to execute the corresponding program of short press
{
case 1:
ucLed = 0xFF;
break;
}
}
else // Long press Timing time exceeded 1s
{
switch(key_value) // Judge whether the key is pressed Select the key value of the key to perform the corresponding function
{
case 1:
ucLed = 0x00;
break;
}
}
//*** Basic timer 6 The update callback function of is configured as 100ms Updated once
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM6)
{
KEY_Time_Count++; // Every time 100ms Count plus 1
HAL_TIM_Base_Start_IT(&htim6);
}
}
Key click 、 double-click
Click the key 、 Double click the train of thought and long press 、 The short press is similar . Start timing at the rising edge after the first key press , To press the key for the second time
During this period of time , Click... If the time exceeds the specified time . If the falling edge is detected within the specified time, it is double-click .
uint8_t key_value,key_up,key_down,uc_Key_Value;
static uint8_t key_old;
uint8_t key_down_num;
uint8_t KEY_Pressed_Num_Time_Count;
void KEY_Proc(void)
{
if(uwTick - uwTick_KEY_Set_Point<10) return; // Deceleration function
uwTick_KEY_Set_Point=uwTick; // Every time 10ms Scan once
key_value = KEY_Scan();
key_up = key_value & (key_old ^ key_value);
key_down = ~key_value & (key_old ^ key_value);
key_old = key_value;
if(key_up) // When the key is pressed and lifted for the first time
{
if(key_down_num == 0)
key_down_num = 1; // Record that the first key has been pressed
}
if(key_down_num == 1) // When the key is pressed and lifted for the first time Start timing
{
KEY_Pressed_Num_Time_Count += 10;
if(KEY_Pressed_Num_Time_Count >= 150) // When the key is pressed 1.5s The second press of the key is not detected after time - single click
{
switch(key_old)
{
case 2:
ucLed = 0x80;
break;
}
key_down_num = 0; // Zero clearing
KEY_Pressed_Num_Time_Count = 0; // Zero clearing
}
}
if(key_down) // When a key press is detected - double-click
{
if(key_down_num == 1) // When the key is pressed for the first time
{
switch(key_down)
{
case 2:
ucLed = 0x01;
break;
}
KEY_Pressed_Num_Time_Count = 0;
key_down_num = 0;
}
}
}
边栏推荐
- (1) Basic learning - figure out the difference between pin, pad, port, IO and net
- JS content confusion, return content encryption
- 工具与生活服务
- Is the processor the main factor in buying a mobile phone?
- JS reverse request parameter encryption:
- Unity2D--给动画添加关键帧并绑定事件
- C language score ranking
- [graph neural network] overview of graph classification learning [2]: graph classification based on graph neural network
- Summary of DOM
- Geotools wkt coordinate system conversion
猜你喜欢

Matlab 2012a 绘制带箭头的线段

C语言 说反话

Restore a 35k-55k Tencent Android Senior Engineer Interview

Mobaihe cm201-2-ch-hi3798mv300-300h-emmc and NAND_ Infrared Bluetooth voice_ Brush firmware package

Thinking carefully and fearfully: a software can be transmitted online to monitor whether employees want to "run away"
![[machine learning Q & A] cosine similarity, cosine distance, Euclidean distance and the meaning of distance in machine learning](/img/34/367c66b8d10e896848b102a7f9aa89.png)
[machine learning Q & A] cosine similarity, cosine distance, Euclidean distance and the meaning of distance in machine learning

Embedded test template

005_ button

Cookie encryption 13

C语言 数组元素循环右移问题
随机推荐
If you want to install a set of monitoring, what is the process? How much is it?
Upload, use of Avatar
Widget uses setimageviewbitmap method to set bug analysis
C语言 害死人不偿命的(3n+1)猜想
Conversion between opencv and image (valid for pro test)
JS reverse request parameter encryption:
Conjecture of prime pairs in C language
Sorting out the usage of transforms in pytoch
C语言 数组元素循环右移问题
Copy entire directory to output folder maintain folder structure- Copy entire directory to output folder maintaining the folder structure?
js Array. Five convenient applications of from()
If mybaits cannot query the data, it can query how to change it in the database
Fake divorce turns into real divorce. What about property
C language I want to pass
搞透AQS原理(流程图及同步队列图解)
001_ layout
CTF入门学习(Web方向)
(4) Blender source code analysis flash window display process
Record an oom exception in production
210. Schedule II - depth traversal