当前位置:网站首页>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 */
边栏推荐
- [C supplement] [string] display the schedule of a month by date
- The reviewboard has 500 errors when submitting a review. Solutions
- Htt [ripro network disk link detection plug-in] currently supports four common network disks
- SPIE Western optoelectronics exhibition returned offline and successfully held a science and engineering event
- 股票万1免5证券开户是合理安全的吗,怎么讲
- Penetration practice vulnhub range Nemesis
- In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)
- Yuancosmos game farmersworld farmers world - core content of the second conference in China!
- Report on research and investment prospects of China's silicon nitride ceramic substrate industry (2022 Edition)
- SCP -i private key usage
猜你喜欢
Common design parameters of solid rocket motor
Intel's open source deep learning tool library openvino will increase cooperation with local software and hardware parties and continue to open
The difference and relationship between iteratible objects, iterators and generators
Data warehouse (3) star model and dimension modeling of data warehouse modeling
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
What are the legal risks of NFT brought by stars such as curry and O'Neill?
Thinkphp6 - CMS multi wechat management system source code
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
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
How to write good code - Defensive Programming Guide
随机推荐
Smart factory digital management system software platform
Radhat builds intranet Yum source server
Maizeer: the two batches of products reported by the media have been taken off the shelves and sealed, and consumer appeals are accepted
Yolov5 practice: teach object detection by hand
The new server is packaged with the source code of H5 mall with an operation level value of several thousand
Session layer of csframework, server and client (1)
Kia recalls some K3 new energy with potential safety hazards
SPIE Western optoelectronics exhibition returned offline and successfully held a science and engineering event
DRF --- response rewrite
【Try to Hack】vulnhub DC4
JDBC: deep understanding of Preparedstatement and statement[easy to understand]
MFC obtains local IP (used more in network communication)
Intelligent operation and maintenance practice: banking business process and single transaction tracking
Cloud picture says | distributed transaction management DTM: the little helper behind "buy buy buy"
SCP -i private key usage
Equipment simulation and deduction training system software
Openlayers 自定义气泡框以及定位到气泡框
Penetration practice vulnhub range Tornado
Blackwich: the roadmap of decarbonization is the first step to realize the equitable energy transformation in Asia
Code example of libcurl download file