当前位置:网站首页>[embedded explanation] key scanning based on finite state machine and stm32
[embedded explanation] key scanning based on finite state machine and stm32
2022-07-28 12:55:00 【Mdc_ stdio】
Based on finite state machine and STM32 Realize key scanning
The finite state machine is actually a conceptual machine , A block diagram showing a finite number of States and behaviors such as transitions and actions between these states ( On procedure ).
Take the diagram drawn by my program as an example :

Mark with high level , stay S1 Detect whether the input level is high , If it is high, run to S2, Otherwise, keep at S1;
stay S2 Judge whether the input level is high again , If it is low, it indicates that the change just made is interference , go back to S1, If it is high, it means that the key is pressed , Run to S3;
stay S3 You can judge whether the operation of the key is long press or short press , The specific method is to judge whether the level is still high , If it is high, keep the state machine at S3, Define a variable at the same time ( It is recommended to use a pointer , See code for details ), Make it always add . If it is low, run to S4;
stay S4 Determine whether the variable is greater than a certain value , Greater than, long press , Less than, short press , And perform relevant operations , After execution, return the state machine to S1
This kind of writing can use the form of time slice , every other 10ms Run the state machine once , The purpose of eliminating chattering can be realized , It can also accurately control the long press time .
Using state machines + Time slice , A substantial increase CPU Utilization of resources , And much more efficient .
Time slice, for example , For example, I have a cycle 30ms,10ms Need to perform a key scan ,20ms Do it once ADC, Then, using time slices, the program is :
notes :Time Is every... In the timer interrupt 1ms Self adding once .
if( Time %10 == 0 ){
KeyScan();
}
if( Time %20 == 0 ){
ADC();
}
if( Time > 30 ){
Time = 1;
}
Paste the complete code I wrote :
Key.h:
#ifndef _BSP_KEY_H
#define _BSP_KEY_H
#include "stm32f10x.h"
#define KEY0_GPIO_PORT GPIOE
#define KEY0_GPIO_PIN GPIO_Pin_0
#define KEY0_GPIO_CLOCK RCC_APB2Periph_GPIOE
#define KEY1_GPIO_PORT GPIOE
#define KEY1_GPIO_PIN GPIO_Pin_1
#define KEY1_GPIO_CLOCK RCC_APB2Periph_GPIOE
#define KEY2_GPIO_PORT GPIOE
#define KEY2_GPIO_PIN GPIO_Pin_2
#define KEY2_GPIO_CLOCK RCC_APB2Periph_GPIOE
#define ON 1
#define OFF 0
#define LONGTIME 8000
//--------- Define the State Sx Enumerated type of
typedef enum {
FsmState_1 = 1,
FsmState_2 = 2,
FsmState_3 = 3,
FsmState_4 = 4,
FsmState_5 = 5,
}FsmState_x;
//--------- Define the structure of the key
typedef struct FsmTable_s{
uint8_t event; /* Triggering event */
uint8_t CurState; /* current state */
void (*EventFunction)(void); /* Action function */
uint16_t Time; /* Time count */
uint16_t GpioPin; /* Key pin */
GPIO_TypeDef * GpioPort; /* Pin GPIO */
}Key;
void key_GPIO_Init(void);
void Key_1_EventFunction(void);
void Key_1_LongEventFunction(void);
void Key_2_EventFunction(void);
void Key_2_LongEventFunction(void);
void Key_Fsm(Key *key);
#endif
/********************************************** * * State machine related * **********************************************/
/********************************************** * * Key 1 Short time event function * *********************************************/
void Key_1_EventFunction()
{
}
/********************************************** * * Key 1 Long time event function * *********************************************/
void Key_1_LongEventFunction()
{
}
/********************************************** * * Key 2 Short time event function * *********************************************/
void Key_2_EventFunction()
{
}
/********************************************** * * Key 2 Long time event function * *********************************************/
void Key_2_LongEventFunction()
{
}
/********************************************** * * Key scanning based on state machine * Input :Key Pointer variable of type * Output : nothing * *********************************************/
void Key_Fsm(Key *key)
{
void (*EventFunction)(void);
switch( key->CurState )
{
case FsmState_1:{
//-------- state 1
if( GPIO_ReadInputDataBit(key->GpioPort,key->GpioPin) == ON ){
key->CurState = FsmState_2;
}
}break;
case FsmState_2:{
//-------- state 2
if( GPIO_ReadInputDataBit(key->GpioPort,key->GpioPin) == ON ){
key->CurState = FsmState_3;
}
else{
//-------- Interference return status 1
key->CurState = FsmState_1;
}
}break;
case FsmState_3:{
if( GPIO_ReadInputDataBit(key->GpioPort,key->GpioPin) == ON ){
key->Time++;//-------- Variable self addition
key->CurState = FsmState_3;//-------- Keep in a state 3
}
else{
key->CurState = FsmState_4;
}
}break;
case FsmState_4:{
EventFunction = key->EventFunction;//-------- Short press function
if( (key->Time > LONGTIME) && (key->GpioPin == KEY0_GPIO_PIN) ){
key->Time = 0;
EventFunction = Key_1_LongEventFunction;//--------Key1 Long press function of
}
if( (key->Time > LONGTIME) && (key->GpioPin == KEY1_GPIO_PIN) ){
key->Time = 0;
EventFunction = Key_2_LongEventFunction;//--------Key2 Long press function of
}
key->CurState = FsmState_1;//-------- Return to status 1
EventFunction();//-------- Execute the corresponding function
}break;
}
}
#include "bsp_key.h"
extern uint16_t TimeSlice;
int main()
{
Key *key_1,*key_2;
Key key1,key2;
key_1 = &key1;
key_2 = &key2;
TIM_TimeBase_Init();
key_GPIO_Init();
key_1->GpioPin = KEY0_GPIO_PIN;
key_1->GpioPort = KEY0_GPIO_PORT;
key_1->CurState = FsmState_2;
key_1->EventFunction = Key_1_EventFunction;
key_2->GpioPin = KEY1_GPIO_PIN;
key_2->GpioPort = KEY1_GPIO_PORT;
key_2->CurState = FsmState_1;
key_2->EventFunction = Key_2_EventFunction;
while(1)
{
//-------------- Key scan ,10ms Do it once --------------//
if( TimeSlice %10==0 ){
Key_Fsm(key_1);
Key_Fsm(key_2);
}
}
}
Single chip microcomputer - Embedded design topic selection and project sharing :
https://blog.csdn.net/m0_71572576/article/details/125409052
Last
边栏推荐
- LeetCode84 柱状图中最大的矩形
- C# 结构使用
- 快速读入
- Installation and reinstallation of win11 system graphic version tutorial
- Unity 安装 Device Simulator
- GMT installation and use
- [nuxt 3] (XII) project directory structure 3
- 机器学习实战-神经网络-21
- leetcode 376. Wiggle Subsequence
- [pictures and texts] detailed tutorial of one click reinstallation of win11 system
猜你喜欢

What SaaS architecture design does a software architect need to know?

LeetCode 42.接雨水

MMA8452Q几种模式的初始化实例

一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码

Machine learning practice - neural network-21

New Oriental's single quarter revenue was 524million US dollars, a year-on-year decrease of 56.8%, and 925 learning centers were reduced

Ccf201912-2 recycling station site selection

Markdown concise grammar manual

DART 三维辐射传输模型申请及下载

Unity installs the device simulator
随机推荐
New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held
Problem solving during copilot trial
Ten prohibitions for men and women in love
Block reversal (summer vacation daily question 7)
Design a thread pool
Interface control telerik UI for WPF - how to use radspreadsheet to record or comment
[half understood] zero value copy
Ccf201912-2 recycling station site selection
与元素类型 “item” 相关联的 “name” 属性值不能包含'<” 字符解决办法
Redis implements distributed locks
STM32 loopback structure receives and processes serial port data
Deployment之滚动更新策略。
西门子对接Leuze BPS_304i 笔记
Custom paging tag 02 of JSP custom tag
STM32F103 several special pins are used as ordinary io. Precautions and data loss of backup register 1,2
Leetcode94. Middle order traversal of binary trees
区块反转(暑假每日一题 7)
Communication example between upper computer and Mitsubishi fn2x
The input string contains an array of numbers and non characters, such as a123x456. Take the consecutive numbers as an integer, store them in an array in turn, such as 123 in a[0], 456 in a[1], and ou
05 pyechars basic chart (example code + effect diagram)