当前位置:网站首页>Record 3 - the state machine realizes key control and measures the number of external pulses
Record 3 - the state machine realizes key control and measures the number of external pulses
2022-07-01 18:02:00 【weixin_ sixty-five million four hundred and eighty-nine thousan】
Catalog
State machine introduction
To shake
Direct delay will occupy the execution time of the processor , Reduce the utilization efficiency of the processor . In engineering applications, state machine and timing interrupt are commonly used to eliminate chattering
Design idea of state machine
Divide a process into several States and switch directly , There is a certain connection between these States
Mission
With stm32f411ceu6 For example 
Engineering configuration
TIM10 To configure :

TIM2 To configure :


Code implementation
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
typedef enum
{
KEY_CHECK=0,// Key detection status
KEY_COMFIRM,// Press the key to confirm the status
KEY_RELEASE// Key release status
}KEY_STATE;// Define three key states
/* USER CODE END PTD */
/* USER CODE BEGIN PV */
KEY_STATE KeyState=KEY_CHECK;
uint8_t KeyFlag=0;// Key valid value flag :1—— It works ,0—— Invalid
uint8_t Result=0;
/* USER CODE END PV */
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start_IT(&htim10);// Turn on timer 10 interrupt
HAL_TIM_Base_Start(&htim2);// Turn on timer 2
printf ("TIMER COUNTER:\n");
/* USER CODE END 2 */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(KeyFlag==1)// Press the judgment key
{
KeyFlag = 0;
HAL_GPIO_TogglePin (GPIOC ,GPIO_PIN_13);
HAL_GPIO_WritePin (GPIOA ,GPIO_PIN_6 ,GPIO_PIN_SET );
HAL_Delay (1);
HAL_GPIO_WritePin (GPIOA ,GPIO_PIN_6 ,GPIO_PIN_RESET );
HAL_Delay (1);
Result = __HAL_TIM_GetCounter(&htim2);
// Get the current counter value
printf ("Count=%d.\n",Result);
}
}
/* USER CODE END 3 */
State machine implementation
/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)// Callback function
{
if(htim->Instance == TIM10 )
{
switch(KeyState)
{
case KEY_CHECK :
{
if(HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_0 )==GPIO_PIN_RESET)
{
KeyState = KEY_COMFIRM;
}
break ;
}
case KEY_COMFIRM :
{
if(HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_0 )==GPIO_PIN_RESET)
{
KeyState = KEY_RELEASE ;
KeyFlag = 1;
}
else
{
KeyState = KEY_CHECK ;
}
break ;
}
case KEY_RELEASE :
{
if(HAL_GPIO_ReadPin (GPIOA,GPIO_PIN_0 )==GPIO_PIN_SET)
{
KeyState = KEY_CHECK ;
}
break ;
}
default :break ;
}
}
}
int fputc(int ch,FILE *f)
{
HAL_UART_Transmit (&huart2 ,(uint8_t *)&ch,1,HAL_MAX_DELAY);
return ch;
}//printf Function redefinition
/* USER CODE END 4 */
边栏推荐
- How to retrieve the password for opening Excel files
- Maizeer: the two batches of products reported by the media have been taken off the shelves and sealed, and consumer appeals are accepted
- Detailed explanation of ArrayList expansion
- Redis主从实现10秒检查与恢复
- [2. Basics of Delphi grammar] 4 Object Pascal operators and expressions
- Enter wechat applet
- 反射型XSS漏洞
- Sword finger offer II 105 Maximum area of the island
- Kernel stray cat stray dog pet adoption platform H5 source code
- Slider verification code identification gadget display
猜你喜欢

Detailed explanation of string's trim() and substring()

Yuancosmos game farmersworld farmers world - core content of the second conference in China!

ISO 27001 Information Security Management System Certification

(十七)DAC转换实验

Apache iceberg source code analysis: schema evolution
![[PHP foundation] realize the connection between PHP and SQL database](/img/eb/c8953eddfe3b19b0adb5529957d275.jpg)
[PHP foundation] realize the connection between PHP and SQL database
Roewe rx5's "a little more" product strategy

Penetration practice vulnhub range Nemesis

(十六)ADC转换实验

Why should you consider using prism
随机推荐
SLO is increasingly used to achieve observability | Devops
[C supplement] [string] display the schedule of a month by date
Review Net 20th anniversary development and 51aspx growth
[PHP foundation] realize the connection between PHP and SQL database
(十六)ADC转换实验
Detailed explanation of string's trim() and substring()
SCP -i private key usage
EasyCVR设备录像出现无法播放现象的问题修复
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
Research Report on development prediction and investment direction of nylon filament sewing thread in China (2022 Edition)
New patent applications and transfers
Extract the compressed package file and retrieve the password
Reflective XSS vulnerability
手机开户股票开户安全吗?那么开户需要带些什么?
Is it safe to open a stock account by mobile phone? What do you need to bring with you to open an account?
Sword finger offer II 105 Maximum area of the island
Rotation order and universal lock of unity panel
(17) DAC conversion experiment
The reviewboard has 500 errors when submitting a review. Solutions
Report on research and investment prospects of China's silicon nitride ceramic substrate industry (2022 Edition)