简介
一种在stm32 hal库中实现非阻塞任务处理扫描的方式。
stm32f1xx_it.c
c
/* USER CODE BEGIN 0 */
uint8_t Time_1ms = 0;
uint8_t Time_10ms = 0;
/* USER CODE END 0 */
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
Time_1ms++;
Time_10ms++;
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
main.c
c
/*
* 变量定义
*/
#define TIMER_FLAG_SET 1U
#define TIMER_FLAG_CLR 0U
#define TASK_TIMER_1MS 1U
#define TASK_TIMER_10MS 10U
//#define TASK_TIMER_30MS 30U
/* 扫描函数
1ms
10ms
*/
extern uint8_t Time_1ms;
extern uint8_t Time_10ms;
uint8_t Get1msFlag(void)
{
uint8_t tempReturn = TIMER_FLAG_CLR;
if(Time_1ms >= TASK_TIMER_1MS)
{
Time_1ms = TIMER_FLAG_CLR;
tempReturn = TIMER_FLAG_SET;
}
else{}
return tempReturn;
}
uint8_t Get10msFlag(void)
{
uint8_t tempReturn = TIMER_FLAG_CLR;
if(Time_10ms >= TASK_TIMER_10MS)
{
Time_10ms = TIMER_FLAG_CLR;
tempReturn = TIMER_FLAG_SET;
}
else{}
return tempReturn;
}
int main(void)
{
HAL_Init();
SystemClock_Config();
while(1)
{
if(Get1msFlag() == TIMER_FLAG_SET)
{
//task1();
}
if(Get10msFlag() == TIMER_FLAG_SET)
{
//task2();
}
}
}