混合式調(diào)度器C51源代碼
/*==============================================================
1ms時標(biāo) 混合式調(diào)度器(一個搶占式任務(wù),多個合作式任務(wù))
作者:shadow.hu
===============================================================*/
#includereg52.h>
#define uchar unsigned char
#define ushort unsigned short
#define SCH_MAX_TASKS 9
#define ERROR_SCH_TOO_MANY_TASKS 9
#define ERROR_SCH_CANOT_DELETE_TASK 0
#define RETURN_ERROR 0
#define RETURN_NORMAL 1
#define INTERRPT_Timer_2_Overflow 5
#define SCH_REPORT_ERRORS
#ifdef SCH_REPORT_ERRORS
#define Error_Port P1
#endif
typedef data struct
{
void (code *pTask)(void);
ushort Delay;
ushort Period;
ushort RunMe;
uchar Co_op;//如果任務(wù)是合作式的,設(shè)置為1,如果任務(wù)是搶占式的,設(shè)置為0
}sTask;
sTask SCH_tasks_G[SCH_MAX_TASKS];
void SCH_Init_T2(void);
uchar SCH_Add_Task(void (code * pFunction)(),const ushort Delay, ushort PERIOD);
// 函數(shù)名指針 延時的時標(biāo)數(shù) 執(zhí)行任務(wù)的時間間隔
// 為0則立即執(zhí)行 如果為0,表示單次任務(wù)
void SCH_Dispatch_Tasks(void);
void SCH_Start(void);
bit SCH_Delete_Task(const ushort TASK_INDEX);
void SCH_Go_To_Sleep(void);
void SCH_Report_Status(void);//報告系統(tǒng)狀況
void LED_Flash_Init(void);
void LED_Flash_Update_A(void);
void LED_Flash_Update_B(void);
void LED_Flash_Update_C(void);
void LED_Flash_Update_D(void);
void LED_Flash_Update_E(void);
void LED_Flash_Update_F(void);
void LED_Flash_Update_G(void);
void LED_Flash_Update_H(void);
uchar Error_code_G = 0;//
static ushort Error_tick_count_G;//記住自從上一次紀(jì)錄錯誤以來的時間
static uchar Last_error_code_G;//上次的錯誤代碼(在1分鐘之后復(fù)位)
uchar LED_State_G_A = 0;
uchar LED_State_G_B = 0;
uchar LED_State_G_C = 0;
uchar LED_State_G_D = 0;
uchar LED_State_G_E = 0;
uchar LED_State_G_F = 0;
uchar LED_State_G_G = 0;
uchar LED_State_G_H = 0;
sbit LED_pin_A = P1^0;
sbit LED_pin_B = P1^1;
sbit LED_pin_C = P1^2;
sbit LED_pin_D = P1^3;
sbit LED_pin_E = P1^4;
sbit LED_pin_F = P1^5;
sbit LED_pin_G = P1^6;
sbit LED_pin_H = P1^7;
//Error_code_G = ERROR_SCH_TOO_MANY_TASKS;
//Error_code_G = ERROR_SCH_WAITING_FOR_SLAVE_TO_ACK;
//Error_code_G = ERROR_SCH_WAITING_FOR_START_COMAND_FROM_MASTER;
//Error_code_G = ERROR_SCH_ONE_OR_MORE_SLAVES_DID_NOT_START;
//Error_code_G = ERROR_SCH_LOST_SLAVE;
//Error_code_G = ERROR_SCH_CAN_BUS_ERROR;
//Error_code_G = ERROR_I2C_WRITE_BYTE_AT24C64;
void main(void)
{
SCH_Init_T2();
LED_Flash_Init();
SCH_Add_Task(LED_Flash_Update_A,0,1000);//添加一個任務(wù)
SCH_Add_Task(LED_Flash_Update_B,0,2000);//添加一個任務(wù)
SCH_Add_Task(LED_Flash_Update_C,0,3000);//添加一個任務(wù)
SCH_Add_Task(LED_Flash_Update_D,0,4000);//添加一個任務(wù)
SCH_Add_Task(LED_Flash_Update_E,0,5000);//添加一個任務(wù)
SCH_Add_Task(LED_Flash_Update_F,0,6000);//添加一個任務(wù)
SCH_Add_Task(LED_Flash_Update_G,0,7000);//添加一個任務(wù)
SCH_Add_Task(LED_Flash_Update_H,0,8000);//添加一個任務(wù)
SCH_Start();//開全局中斷
while(1)
{
SCH_Dispatch_Tasks();
}
}
/*------------------------------------------------------------
這是調(diào)度器的中斷服務(wù)程序,初始化函數(shù)中的定時器設(shè)置決定了它
的調(diào)度頻率,這個版本的調(diào)度器由定時器2觸發(fā)中斷,定時器自動重裝。
-------------------------------------------------------------*/
void SCH_Update(void) interrupt INTERRPT_Timer_2_Overflow
{
//刷新任務(wù)隊(duì)列
uchar Index;
TF2 = 0;//必須手工清除
//注意:計算單位為時標(biāo)(不是毫秒)
for(Index = 0;Index SCH_MAX_TASKS;Index++)
{ //檢測這里是否有任務(wù)
if(SCH_tasks_G[Index].pTask)
{
if(SCH_tasks_G[Index].Delay == 0)
{
//任務(wù)需要運(yùn)行,間隔的時間已經(jīng)到了
if(SCH_tasks_G[Index].Co_op)
{
//如果是合作式任務(wù),RunMe標(biāo)志加1
SCH_tasks_G[Index].RunMe += 1;//要執(zhí)行任務(wù)的標(biāo)志加1
}
else//如果它是搶占式任務(wù),立即運(yùn)行它
{
(*SCH_tasks_G[Index].pTask)();//運(yùn)行任務(wù)
SCH_tasks_G[Index].RunMe -= 1;
//周期性的任務(wù)將自動再次運(yùn)行,單次任務(wù)就刪除
if(SCH_tasks_G[Index].Period == 0)
{
SCH_tasks_G[Index].pTask = 0;
}
}
if(SCH_tasks_G[Index].Period)//時標(biāo)間隔不等于0
{
//調(diào)度周期性的任務(wù)再次運(yùn)行,每隔這個固定的時標(biāo)長度執(zhí)行一次任務(wù)
SCH_tasks_G[Index].Delay = SCH_tasks_G[Index].Period;
}
}
else //任務(wù)有延遲執(zhí)行要求,還沒到達(dá)延遲的時間
{
//還沒有準(zhǔn)備好運(yùn)行,延遲減1
SCH_tasks_G[Index].Delay -= 1;
}
}
}
}
void SCH_Init_T2(void)
{
uchar i;
for(i=0;iSCH_MAX_TASKS;i++)
{
SCH_Delete_Task(i);
}
Error_code_G = 0;
T2CON = 0x04;
TMOD = 0x00;
TH2 = 0xfc;
RCAP2H = 0xfc;
TL2 = 0x18;
RCAP2L = 0x18;
ET2 = 1;
TR2 = 1;
}
/*----------------------------------------------------------------------------
任務(wù)函數(shù)每隔一定時間間隔或在用戶定義的延遲之后運(yùn)行
pFunction -- 將被調(diào)用的函數(shù)名稱。注意:被調(diào)函數(shù)必須是“void void”型
DELAY -- 在任務(wù)第一次被執(zhí)行之前的間隔
PERIOD -- 如果它為0,則只調(diào)用該函數(shù)一次,由DELAY確定其調(diào)用的時間
如果非0,那么它就是被重復(fù)調(diào)用的時間間隔
Co_op -- 如果是合作式任務(wù)則設(shè)置為1,如果是搶占式任務(wù)則設(shè)置為0.
注意:如果以后要刪除任務(wù),將需要返回值
例子:
Task_ID = SCH_Add_Task(Do_X,1000,0,0);
使函數(shù)Do_X()在1000個調(diào)度器時標(biāo)之后運(yùn)行一次(搶占式任務(wù))
Task_ID = SCH_Add_Task(Do_X,0,1000,1);
使函數(shù)Do_X()每隔1000個調(diào)度器時標(biāo)運(yùn)行一次(合作式任務(wù))
Task_ID = SCH_Add_Task(Do_X,300,1000,0);
使函數(shù)Do_X()每隔1000個調(diào)度器時標(biāo)運(yùn)行一次,任務(wù)首先在T=300個時標(biāo)時被執(zhí)行
然后是1300個時標(biāo).........(搶占式任務(wù))
-----------------------------------------------------------------------------*/uchar SCH_Add_Task(void (code * pFunction)(),const ushort DELAY, ushort PERIOD,bit Co_op)
{
uchar Index = 0;
//首先在隊(duì)列中找到一個空隙(如果有的話,否則就不添加新任務(wù))
while((SCH_tasks_G[Index].pTask != 0)(Index SCH_MAX_TASKS))
{
Index++;//當(dāng)一個新任務(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;//直接返回,不添加這個新任務(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ù)的位置(以便以后刪除)
}
void SCH_Dispatch_Tasks(void)
{
uchar Index;
//調(diào)度(運(yùn)行)下一個任務(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)志
}
//如果這是個“單次”任務(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();//報告系統(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è)置全局錯誤變量
Error_code_G = ERROR_SCH_CANOT_DELETE_TASK;
Return_code = RETURN_ERROR;//返回錯誤代碼
}
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{
LED_State_G_B = 1;
LED_pin_B = 1;
}
}
void LED_Flash_Update_C(void)
{
if(LED_State_G_C == 1)
{
LED_State_G_C = 0;
LED_pin_C = 0;
}
else
{
LED_State_G_C = 1;
LED_pin_C = 1;
}
}
void LED_Flash_Update_D(void)
{
if(LED_State_G_D == 1)
{
LED_State_G_D = 0;
LED_pin_D = 0;
}
else
{
LED_State_G_D = 1;
LED_pin_D = 1;
}
}
void LED_Flash_Update_E(void)
{
if(LED_State_G_E == 1)
{
LED_State_G_E = 0;
LED_pin_E = 0;
}
else
{
LED_State_G_E = 1;
LED_pin_E = 1;
}
}
void LED_Flash_Update_F(void)
{
if(LED_State_G_F == 1)
{
LED_State_G_F = 0;
LED_pin_F = 0;
}
else
{
LED_State_G_F = 1;
LED_pin_F = 1;
}
}
void LED_Flash_Update_G(void)
{
if(LED_State_G_G == 1)
{
LED_State_G_G = 0;
LED_pin_G = 0;
}
else
{
LED_State_G_G = 1;
LED_pin_G = 1;
}
}
void LED_Flash_Update_H(void)
{
if(LED_State_G_H == 1)
{
LED_State_G_H = 0;
LED_pin_H = 0;
}
else
{
LED_State_G_H = 1;
LED_pin_H = 1;
}
}
void LED_Flash_Init(void)
{
LED_State_G_A= 0;//初始化LED狀態(tài)
LED_State_G_B= 0;//初始化LED狀態(tài)
LED_State_G_C= 0;//初始化LED狀態(tài)
}
評論