当前位置:网站首页>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 use JMeter function and mockjs function in metersphere interface test
- RadHat搭建内网YUM源服务器
- Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
- Length of learning and changing
- Why should you consider using prism
- Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
- From comedians to NBA Zhan Huang, check the encrypted advertisements during this super bowl
- Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day
- Check log4j problems using stain analysis
- Countdownlatch blocking wait for multithreading concurrency
猜你喜欢

Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open

Yolov5 practice: teach object detection by hand

Fix the black screen caused by iPhone system failure

Replace UUID, nanoid is faster and safer!

ISO 27001 Information Security Management System Certification

Setting up a time server requires the client to automatically synchronize the time of the server at 9 a.m. every day

New patent applications and transfers

June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries

An example of data analysis of an old swatch and an old hard disk disassembly and assembly combined with the sensor of an electromagnetic press

Yuancosmos game farmersworld farmers world - core content of the second conference in China!
随机推荐
Replace UUID, nanoid is faster and safer!
Is online stock account opening safe? Is it reliable?
SQL injection vulnerability (MySQL and MSSQL features)
Kernel stray cat stray dog pet adoption platform H5 source code
China metallocene polyethylene (MPE) Industry Research Report (2022 Edition)
Smart factory digital management system software platform
ACL 2022 | decomposed meta learning small sample named entity recognition
Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
SLO is increasingly used to achieve observability | Devops
China PBAT resin Market Forecast and Strategic Research Report (2022 Edition)
RadHat搭建内网YUM源服务器
Gold, silver and four job hopping, interview questions are prepared, and Ali becomes the champion
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say
Code example of libcurl download file
China biodegradable plastics market forecast and investment strategy report (2022 Edition)
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
开发那些事儿:EasyCVR集群设备管理页面功能展示优化
两数之和c语言实现[通俗易懂]
MySQL -- explain performance optimization
PIP version problems: PIP problems still occur when installing akshare and using Tsinghua source and Douban source