当前位置:网站首页>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 */
边栏推荐
- In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)
- Fix the black screen caused by iPhone system failure
- (16) ADC conversion experiment
- Roewe rx5's "a little more" product strategy
- Easycvr accesses the equipment through the national standard gb28181 protocol. What is the reason for the automatic streaming of the equipment?
- Nearly 60% of the employees strongly support Ctrip's "3+2" working mode, and work at home for two days a week
- [PHP foundation] realize the connection between PHP and SQL database
- PIP version problems: PIP problems still occur when installing akshare and using Tsinghua source and Douban source
- To improve the efficiency of office collaboration, trackup may be the best choice
- Source code of new campus errand / campus task platform on mutual station
猜你喜欢
Kia recalls some K3 new energy with potential safety hazards
DNS
How to use JMeter function and mockjs function in metersphere interface test
Penetration practice vulnhub range Tornado
What are the legal risks of NFT brought by stars such as curry and O'Neill?
Kernel stray cat stray dog pet adoption platform H5 source code
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
Roewe rx5's "a little more" product strategy
Review Net 20th anniversary development and 51aspx growth
(16) ADC conversion experiment
随机推荐
PHP实现敏感词过滤系统「建议收藏」
Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)
Fix the black screen caused by iPhone system failure
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
Length of learning and changing
Extract the compressed package file and retrieve the password
EasyCVR通过国标GB28181协议接入设备,出现设备自动拉流是什么原因?
ISO 27001 Information Security Management System Certification
How to retrieve the password for opening Excel files
Can hero sports go public against the wind?
Roewe rx5's "a little more" product strategy
Development cost of smart factory management system software platform
Unity3d extended toolbar
Small exercise -- subnet division and summary
pyqt5中,在控件上画柱状图
[splishsplash] about how to receive / display user parameters, MVC mode and genparam on GUI and JSON
Thinkphp6 - CMS multi wechat management system source code
MES production equipment manufacturing execution system software
Source code of new campus errand / campus task platform on mutual station
In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)