基于时间触发的裸机任务调度一

 /**/
1
#define TASK_NUM (3) 2 3 typedef struct _TASK_COMPONETS_ 4 { 5 uint8_t Run; //运行状态 6 uint16_t Timer; //任务启动延时,设置此值防止任务重叠 7 uint16_t lvtTime; //任务运行周期间隔 8 void (*TaskHook)(void); //任务函数指针 9 10 }TASK_COMPONENTS; 11 12 13 typedef enum _TASK_LIST_ 14 { 15 TASK_1, 16 TASK_2, 17 TASK_3, 18 TASK_MAX 19 }TASK_LIST; 20 21 TASK_COMPONENTS TaskComps[TASK_NUM] = 22 { 23 {0, 1, 35, Task1}, 24 {0, 3, 40, Task2}, 25 {0, 5, 45, Task3}, 26 }; 27 28 void TaskRemaks(void) 29 { 30 uint8_t i; 31 for(i = 0; i < TASK_MAX; i++) 32 { 33 if(TaskComps[i].Timer) 34 { 35 TaskComps[i].Timer--; 36 if(TaskComps[i].Timer == 0) 37 { 38 TaskComps[i].Timer = TaskComps[i].lvtTime; 39 TaskComps[i].Run = 1; 40 } 41 } 42 } 43 44 } 45 46 void TaskProcess(void) 47 { 48 uint8_t i; 49 for(i = 0; i < TASK_MAX; i++) 50 { 51 if(TaskComps[i].Run) 52 { 53 TaskComps[i].TaskHook(); 54 TaskComps[i].Run = 0; 55 } 56 } 57 } 58 59 void Task1 (void) 60 { 61 printf("[%ldms]T1 \r\n",HAL_GetTick()); 62 } 63 64 void Task2 (void) 65 { 66 printf("[%ldms]T2 \r\n",HAL_GetTick()); 67 } 68 69 void Task3 (void) 70 { 71 printf("[%ldms]T3 \r\n",HAL_GetTick()); 72 }

1ms定时器中断:

 1 void MX_TIM1_Init(void)
 2 {
 3   TIM_ClockConfigTypeDef sClockSourceConfig;
 4   TIM_MasterConfigTypeDef sMasterConfig;
 5 
 6   htim1.Instance = TIM1;
 7   htim1.Init.Prescaler = 71;
 8   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 9   htim1.Init.Period = 999;
10   htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
11   htim1.Init.RepetitionCounter = 0;
12   htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
13   if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
14   {
15     _Error_Handler(__FILE__, __LINE__);
16   }
17 
18   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
19   if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
20   {
21     _Error_Handler(__FILE__, __LINE__);
22   }
23 
24   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
25   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
26   if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
27   {
28     _Error_Handler(__FILE__, __LINE__);
29   }
30 
31 }
32 
33 
34 void TIM1_UP_IRQHandler(void)
35 {
36   /* USER CODE BEGIN TIM1_UP_IRQn 0 */
37 
38   /* USER CODE END TIM1_UP_IRQn 0 */
39   HAL_TIM_IRQHandler(&htim1);
40   /* USER CODE BEGIN TIM1_UP_IRQn 1 */
41 
42   /* USER CODE END TIM1_UP_IRQn 1 */
43 }
44 
45 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
46 {
47     if(htim->Instance == TIM1)
48     {
49         TaskRemaks();
50     }
51 }
 1 int main(void)
 2 {
 3   /* USER CODE BEGIN 1 */
 4 
 5   /* USER CODE END 1 */
 6 
 7   /* MCU Configuration----------------------------------------------------------*/
 8 
 9   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
10   HAL_Init();
11 
12   /* USER CODE BEGIN Init */
13 
14   /* USER CODE END Init */
15 
16   /* Configure the system clock */
17   SystemClock_Config();
18 
19   /* USER CODE BEGIN SysInit */
20 
21   /* USER CODE END SysInit *
22 
23   /* Initialize all configured peripherals */
24   MX_GPIO_Init();
25   MX_USART1_UART_Init();
26   MX_TIM1_Init();
27   /* USER CODE BEGIN 2 */
28   HAL_TIM_Base_Start_IT(&htim1);
29 
30   /* Infinite loop */
31   /* USER CODE BEGIN WHILE */
32   while (1)
33   {
34 
35   /* USER CODE END WHILE */
36 
37   /* USER CODE BEGIN 3 */
38      TaskProcess();
39   }
40   /* USER CODE END 3 */
41 
42 }
43   /* USER CODE END 2 */

测试如下图:TASK1 间隔 35ms执行一次, TASK 2间隔40ms,TASK 3间隔45。

一开始启动TASK1,   2ms后启动TASK2,   4ms后启动TASK3,防止任务重叠。

 

posted @ 2021-04-12 14:01  M&D  阅读(537)  评论(0编辑  收藏  举报