第八节学习了定时器的调度和模块化编程,通过学习规范了代码,方便了代码管理。使用调度器合理配置每个任务,能避免程序堵塞
任务调度器核心代码如下
- #include "task.h"
-
- static TASK_COMPONENTS Task_Comps[]=
- {
- //状态 计数 周期 函数
-
- {0, 300, 300, LED0_Blink}, /* task 1 Period: 300ms */
- {0, 600, 600, LED1_Blink}, /* task 1 Period: 600ms */
- {0, 900, 900, LED2_Blink}, /* task 1 Period: 600ms */
- {0, 10, 10, KEY_Task}, /* task 1 Period: 600ms */
- };
-
- u8 Tasks_Max = sizeof(Task_Comps)/sizeof(Task_Comps[0]);
-
- //========================================================================
- // 函数: Task_Handler_Callback
- // 描述: 任务标记回调函数.
- // 参数: None.
- // 返回: None.
- // 版本: V1.0, 2012-10-22
- //========================================================================
- void Task_Marks_Handler_Callback(void)
- {
- u8 i;
- for(i=0; i<Tasks_Max; i++)
- {
- if(Task_Comps[i].TIMCount) /* If the time is not 0 */
- {
- Task_Comps[i].TIMCount--; /* Time counter decrement */
- if(Task_Comps[i].TIMCount == 0) /* If time arrives */
- {
- /*Resume the timer value and try again */
- Task_Comps[i].TIMCount = Task_Comps[i].TRITime;
- Task_Comps[i].Run = 1; /* The task can be run */
- }
- }
- }
- }
-
- //========================================================================
- // 函数: Task_Pro_Handler_Callback
- // 描述: 任务处理回调函数.
- // 参数: None.
- // 返回: None.
- // 版本: V1.0, 2012-10-22
- //========================================================================
- void Task_Pro_Handler_Callback(void)
- {
- u8 i;
- for(i=0; i<Tasks_Max; i++)
- {
- if(Task_Comps[i].Run) /* If task can be run */
- {
- Task_Comps[i].Run = 0; /* Flag clear 0 */
- Task_Comps[i].TaskHook(); /* Run task */
- }
- }
- }
复制代码
|