当前位置:网站首页>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-07 05:49:00 【Hua Weiyun】
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 .
边栏推荐
- 拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
- I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
- 2pc of distributed transaction solution
- 原生小程序 之 input切换 text与password类型
- Simple case of SSM framework
- 【Shell】清理nohup.out文件
- ForkJoin最全详解(从原理设计到使用图解)
- 关于服装ERP,你知道多少?
- 软件测试面试技巧
- Flink SQL realizes reading and writing redis and dynamically generates hset key
猜你喜欢
![[PM products] what is cognitive load? How to adjust cognitive load reasonably?](/img/75/2277e0c413be561ec963b44679eb75.jpg)
[PM products] what is cognitive load? How to adjust cognitive load reasonably?

Determine whether the file is a DICOM file

Differences and introduction of cluster, distributed and microservice

Cve-2021-3156 vulnerability recurrence notes

Message queuing: how to ensure that messages are not lost

架构设计的五个核心要素

Distributed global ID generation scheme

集群、分布式、微服务的区别和介绍

得物客服一站式工作台卡顿优化之路
![SQLSTATE[HY000][1130] Host ‘host. docker. internal‘ is not allowed to connect to this MySQL server](/img/05/1e4bdddce1e07f7edd2aeaa59139ab.jpg)
SQLSTATE[HY000][1130] Host ‘host. docker. internal‘ is not allowed to connect to this MySQL server
随机推荐
Get the way to optimize the one-stop worktable of customer service
Mapbox Chinese map address
High voltage leakage relay bld-20
产业金融3.0:“疏通血管”的金融科技
上海字节面试问题及薪资福利
Codeforces Round #416 (Div. 2) D. Vladik and Favorite Game
5. Data access - entityframework integration
make makefile cmake qmake都是什么,有什么区别?
Determine whether the file is a DICOM file
拼多多商品详情接口、拼多多商品基本信息、拼多多商品属性接口
Dj-zbs2 leakage relay
Web architecture design process
C#可空类型
STM32按键状态机2——状态简化与增加长按功能
《ClickHouse原理解析与应用实践》读书笔记(6)
zabbix_ Get test database failed
Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i
数据中心为什么需要一套基础设施可视化管理系统
Leakage relay jd1-100
5阶多项式轨迹
