混合式調(diào)度器C51源代碼
uchar SCH_Add_Task(void (code * pFunction)(),const ushort DELAY, ushort PERIOD,bit Co_op)
{
uchar Index = 0;
//首先在隊(duì)列中找到一個(gè)空隙(如果有的話,否則就不添加新任務(wù))
while((SCH_tasks_G[Index].pTask != 0)(Index SCH_MAX_TASKS))
{
Index++;//當(dāng)一個(gè)新任務(wù)被添加,且沒有超過任務(wù)上限
}
//是否達(dá)到任務(wù)隊(duì)列的結(jié)尾?
if(Index == SCH_MAX_TASKS)//任務(wù)數(shù)量達(dá)到上限
{
Error_code_G = ERROR_SCH_TOO_MANY_TASKS;
return SCH_MAX_TASKS;//直接返回,不添加這個(gè)新任務(wù)
}
//如果能運(yùn)行到這里,說明任務(wù)隊(duì)列中有空隙,添加任務(wù)。
SCH_tasks_G[Index].pTask = pFunction;
SCH_tasks_G[Index].Delay = DELAY;
SCH_tasks_G[Index].Period = PERIOD;
SCH_tasks_G[Index].Co_op = Co_op;
SCH_tasks_G[Index].RunMe = 0;
return Index;//返回任務(wù)的位置(以便以后刪除)
}本文引用地址:http://m.butianyuan.cn/article/151635.htm
void SCH_Dispatch_Tasks(void)
{
uchar Index;
//調(diào)度(運(yùn)行)下一個(gè)任務(wù)(如果有任務(wù)就緒)
for(Index = 0;Index SCH_MAX_TASKS;Index++)
{
//只調(diào)度合作式任務(wù)
if((SCH_tasks_G[Index].RunMe > 0)(SCH_tasks_G[Index].Co_op))
{
(*SCH_tasks_G[Index].pTask)();//執(zhí)行任務(wù)
SCH_tasks_G[Index].RunMe -= 1;//清除任務(wù)需要執(zhí)行的標(biāo)志
}
//如果這是個(gè)“單次”任務(wù),將它從隊(duì)列中刪除
if(SCH_tasks_G[Index].Period == 0)
{
SCH_tasks_G[Index].pTask = 0;// 比通過調(diào)用來刪除任務(wù)更快SCH_Delete_Task(Index);
}
}
SCH_Report_Status();//報(bào)告系統(tǒng)狀況
SCH_Go_To_Sleep();
}
void SCH_Start(void)
{
EA = 1;
}
bit SCH_Delete_Task(const ushort TASK_INDEX)
{
bit Return_code;
if(SCH_tasks_G[TASK_INDEX].pTask == 0)
{
//這里沒有任務(wù)。。。設(shè)置全局錯(cuò)誤變量
Error_code_G = ERROR_SCH_CANOT_DELETE_TASK;
Return_code = RETURN_ERROR;//返回錯(cuò)誤代碼
}
else
{
Return_code = RETURN_NORMAL;
}
//刪除任務(wù)
SCH_tasks_G[TASK_INDEX].pTask = 0x0000;
SCH_tasks_G[TASK_INDEX].Delay = 0;
SCH_tasks_G[TASK_INDEX].Period = 0;
SCH_tasks_G[TASK_INDEX].RunMe = 0;
return Return_code;
}
void SCH_Go_To_Sleep()
{
PCON |= 0x01;//進(jìn)入休眠模式
}
void SCH_Report_Status(void)
{
/* #ifdef SCH_REPORT_ERRORS
if(Error_code_G != Last_error_code_G)
{
Error_Port = 255 - Error_code_G;
Last_error_code_G = Error_code_G;
if(Error_code_G != 0)
{
Error_tick_count_G = 60000;
}
else
{
Error_tick_count_G = 0;
}
}
else
{
if(Error_tick_count_G != 0)
{
if(--Error_count_G == 0)
{
Error_code_G = 0;
}
}
}
#endif */
}
void LED_Flash_Update_A(void)
{
if(LED_State_G_A == 1)
{
LED_State_G_A = 0;
LED_pin_A = 0;
}
else
{
LED_State_G_A = 1;
LED_pin_A = 1;
}
}
void LED_Flash_Update_B(void)
{
if(LED_State_G_B == 1)
{
LED_State_G_B = 0;
LED_pin_B = 0;
}
else
評(píng)論