当前位置:网站首页>STM32 key state machine 2 - state simplification and long press function addition
STM32 key state machine 2 - state simplification and long press function addition
2022-07-06 17:58:00 【Code farmer loves learning】
Last article , With the key anti shake function , The basic principle and application method of state machine are introduced .
The state diagram of the previous article is as follows :
Because only press and release are detected , And it has the function of key shaking elimination , Therefore, the above 4 Status , Press jitter and release jitter are two independent States , And these two jittery States , It can also run continuously in multiple cycles , The cycle period of this state machine is set to 10ms, When a certain level is continuously detected in the jitter state 5 Next time , That is, it is considered that the chattering elimination is completed , Go to the next steady state .
For the same function , State diagrams are not static , For key debounce , The two jitter states can also be represented by sharing a jitter state .
1 De chattering state simplification
1.1 State diagram
Press jitter and release jitter share a jitter state to represent , At the same time, the cycle period of the state machine needs to be set to 50ms, such , The jitter state only needs to pass once , Whether it is really key jitter can be determined by the level . The simplified state diagram is as follows :
In order to be able to , Distinguish whether the previous state is released or pressed , Then we can judge whether this time it is the jitter or the real action of the key , You need to add a status to record the previous status
KEY_STATUS g_keyStatus = KS_RELEASE; // The end of the current cycle ( State machine ) state
KEY_STATUS g_nowKeyStatus = KS_RELEASE; // current state ( After each cycle with g_keyStatus bring into correspondence with )
KEY_STATUS g_lastKeyStatus = KS_RELEASE; // Last state ( Used to record the previous state to distinguish the source of the state )
Be careful : Here g_lastKeyStatus Used to record the previous status , There is also this variable in the last article , But the effect is different , The function of this variable in the previous article is the same as that here g_nowKeyStatus The same effect .
1.2 Code
Compare with the simplified state diagram , Write the corresponding state machine logic code :
void key_status_check()
{
switch(g_keyStatus)
{
// Key release ( The initial state )
case KS_RELEASE:
{
// Low level detected , First, eliminate the chattering
if (KEY0 == 0)
{
g_keyStatus = KS_SHAKE;
}
}
break;
// shake
case KS_SHAKE:
{
if (KEY0 == 1)
{
g_keyStatus = KS_RELEASE;
if (KS_PRESS == g_lastKeyStatus)
{
printf("=====> key release\r\n");
}
}
else
{
g_keyStatus = KS_PRESS;
if (KS_RELEASE == g_lastKeyStatus)
{
printf("=====> key press\r\n");
}
}
}
break;
// Press steadily and briefly
case KS_PRESS:
{
// High level detected , First, eliminate the chattering
if (KEY0 == 1)
{
g_keyStatus = KS_SHAKE;
}
}
break;
default:break;
}
if (g_keyStatus != g_nowKeyStatus)
{
g_lastKeyStatus = g_nowKeyStatus;
g_nowKeyStatus = g_keyStatus;
printf("new key status:%d(%s)\r\n", g_keyStatus, key_status_name[g_keyStatus]);
}
}
Be careful g_lastKeyStatus Role of variables .
1.3 test
2 Add long press function
On the basis of detecting press and release , Add long press function , You need to add a long press state in the state diagram . then , Modify the code according to the state diagram .
Again , According to whether it is necessary to distinguish the two jitter States and the difference of the cycle cycle cycle of the state machine , There are two kinds of state diagrams .
2.1 Not simplified state diagram
Let's first look at the cycle 10ms, Distinguish between press jitter and release jitter. Add the state diagram after long press function :
After clarifying the logic of the state diagram , According to the state diagram , Modify the corresponding code , No more code here , The complete code can be checked in my code warehouse
2.2 Simplified state diagram
Next, let's take a look at the state machine diagram of the specific long press function to simplify the dithering state :
The comparison shows that , Simplified state diagram , The status can be one less , But the shaking state , There will be more inputs and outputs , Because at present, every state has passed through this state .
If the requirements for jitter detection are not high , You can also keep only the logic of press jitter , Loosen the shaking branch and remove , Jump directly to the released state , State logic can be simplified again .
2.3 Code
According to the state diagram , Write the corresponding state machine logic code , as follows :
void key_status_check()
{
switch(g_keyStatus)
{
// Key release ( The initial state )
case KS_RELEASE:
{
// Low level detected , First, eliminate the chattering
if (KEY0 == 0)
{
g_keyStatus = KS_SHAKE;
}
}
break;
// shake
case KS_SHAKE:
{
if (KEY0 == 1)
{
g_keyStatus = KS_RELEASE;
if (KS_SHORT_PRESS == g_lastKeyStatus || KS_LONG_PRESS == g_lastKeyStatus)
{
printf("=====> key release\r\n");
}
}
else
{
if (KS_RELEASE == g_lastKeyStatus)
{
g_PressTimeCnt = 0;
g_keyStatus = KS_SHORT_PRESS;
printf("=====> key short press\r\n");
}
else if (KS_SHORT_PRESS == g_lastKeyStatus)
{
g_keyStatus = KS_SHORT_PRESS;
}
else
{
}
}
}
break;
// Press steadily and briefly
case KS_SHORT_PRESS:
{
// High level detected , First, eliminate the chattering
if (KEY0 == 1)
{
g_keyStatus = KS_SHAKE;
}
g_PressTimeCnt++;
if (g_PressTimeCnt == 20) //1000ms
{
g_keyStatus = KS_LONG_PRESS;
printf("=====> key long press\r\n");
}
}
break;
// Steady long press
case KS_LONG_PRESS:
{
// High level detected , First, eliminate the chattering
if (KEY0 == 1)
{
g_keyStatus = KS_SHAKE;
}
g_PressTimeCnt++;
if (g_PressTimeCnt % 20 == 0) // every other 1000ms Print once
{
printf("=====> key long press:%d\r\n", g_PressTimeCnt/20);
}
}
break;
default:break;
}
if (g_keyStatus != g_nowKeyStatus)
{
g_lastKeyStatus = g_nowKeyStatus;
g_nowKeyStatus = g_keyStatus;
printf("new key status:%d(%s)\r\n", g_keyStatus, key_status_name[g_keyStatus]);
}
}
Be careful , In the jitter state , When the detection is high ( Release the button ), Whether the previous state is short press or long press , The next state is the released state .
2.4 test
3 summary
This article continues to introduce the use of state machines , On the basis of the first part , By simplifying the button to shake the logic , And add the function of pressing and holding keys , Further introduce the modification of state diagram and the implementation of state machine code , And passed the actual test , Demonstrate the running effect of the state machine .
边栏推荐
- Interview assault 63: how to remove duplication in MySQL?
- Pytest learning ----- pytest confitest of interface automation test Py file details
- Interview shock 62: what are the precautions for group by?
- 微信小程序获取手机号
- 遠程代碼執行滲透測試——B模塊測試
- Grafana 9 is officially released, which is easier to use and more cool!
- adb常用命令
- Unity particle special effects series - treasure chest of shining stars
- C# NanoFramework 点灯和按键 之 ESP32
- VR panoramic wedding helps couples record romantic and beautiful scenes
猜你喜欢
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Distinguish between basic disk and dynamic disk RAID disk redundant array
Spark calculation operator and some small details in liunx
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
Establishment of graphical monitoring grafana
F200——搭载基于模型设计的国产开源飞控系统无人机
Grafana 9 is officially released, which is easier to use and more cool!
视频融合云平台EasyCVR增加多级分组,可灵活管理接入设备
[translation] principle analysis of X Window Manager (I)
随机推荐
Principle and usage of extern
Flet教程之 13 ListView最常用的滚动控件 基础入门(教程含源码)
Summary of study notes for 2022 soft exam information security engineer preparation
Getting started with pytest ----- test case rules
遠程代碼執行滲透測試——B模塊測試
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
SAP UI5 框架的 manifest.json
kivy教程之在 Kivy 中支持中文以构建跨平台应用程序(教程含源码)
Kernel link script parsing
The art of Engineering
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
Quick start of Hongmeng system
The art of Engineering (3): do not rely on each other between functions of code robustness
1700C - Helping the Nature
Reppoints: advanced order of deformable convolution
Unity particle special effects series - treasure chest of shining stars
Solution qui ne peut pas être retournée après la mise à jour du navigateur Web flutter
趣-关于undefined的问题
Why should Li Shufu personally take charge of building mobile phones?
Compile and build, from the bottom to the top