極小資源單片機(jī)調(diào)度器
- /****小小調(diào)度器開始**********************************************/
- #define MAXTASKS 2
- static unsigned char timers[MAXTASKS];
- unsigned char currdt;
- #define _SS static unsigned char _lc; switch(_lc){default:
- #define _EE ;}; _lc=0; return 255;
- #define WaitX(tickets)do {_lc=__LINE__+((__LINE__%256)==0); return tickets ;} while(0); case __LINE__+((__LINE__%256)==0):
- #define RunTask(TaskName,TaskID) do { if (timers[TaskID]==0) timers[TaskID]=TaskName(); }while(0);
- #define CallSub(SubTaskName) do { _lc=__LINE__+((__LINE__%256)==0); return 0; case __LINE__+((__LINE__%256)==0):currdt=SubTaskName(); if(currdt!=255) return currdt;} while(0);
- #define UpdateTimers() unsigned char i; for(i=MAXTASKS;i>0 ;i--){if((timers[i-1]!=0)&&(timers[i-1]!=255)) timers[i-1]--;}
- #define SEM unsigned int
- //初始化信號(hào)量
- #define InitSem(sem) sem=0;
- //等待信號(hào)量
- #define WaitSem(sem) do{ sem=1; WaitX(0); if (sem>0) return 1;} while(0);
- //等待信號(hào)量或定時(shí)器溢出, 定時(shí)器tickets 最大為0xFFFE
- #define WaitSemX(sem,tickets)do { sem=tickets+1; WaitX(0); if(sem>1){ sem--;return 1;} } while(0);
- //發(fā)送信號(hào)量
- #define SendSem(sem)do {sem=0;} while(0);
- /*****小小調(diào)度器結(jié)束*******************************************************/
- sbit LED1 = P2^1;
- sbit LED2 = P2^2;
- void InitT0()
- {
- TMOD = 0x21;
- IE |= 0x82;// 12t
- TL0=0Xff;
- TH0=0XDB;//22M---b7;
- TR0 = 1;
- }
- void INTT0(void) interrupt 1 using 1
- {
- UpdateTimers();
- TL0=0Xff; //10ms 重裝
- TH0=0XDB;//b7;
- }
- voidtask1(){
- _SS
- while(1){
- WaitX(50);
- LED1=!LED1;
- }
- _EE
- }
- voidtask2(){
- _SS
- while(1){
- WaitX(100);
- LED2=!LED2;
- }
- _EE
- }
- void main()
- {
- InitT0();
- while(1){
- RunTask(task1,0);
- RunTask(task2,1);
- }
- }
版本二、
在IAR6.7下編譯通過。(選用:Misra C 2004)
/****小小調(diào)度器開始**********************************************/
#define MAXTASKS 3U
volatile static unsigned char timers[MAXTASKS];
#define _SS static unsigned char _lc=0U; switch(_lc){case 0U:
#define _EE break; default:break;}_lc=0U;return 255U;
#define WaitX(tickets) _lc=(unsigned char)(__LINE__)%255U+1U;if(_lc){return (tickets);}break; case ((unsigned char)(__LINE__)%255U)+1U:
#define RunTask(TaskName,TaskID)do {if (timers[(TaskID)]==0U){ timers[(TaskID)]=(TaskName)(); } } while(0)
#define RunTaskA(TaskName,TaskID) { if (timers[(TaskID)]==0U) {timers[(TaskID)]=(TaskName)(); continue;} } /*前面的任務(wù)優(yōu)先保證執(zhí)行*/
#define CallSub(SubTaskName) do {unsigned char currdt; _lc=(unsigned char)(__LINE__)%255U+1U; if(_lc) {return 0U;} break; case (unsigned char)(__LINE__)%255U+1U:currdt=(SubTaskName)(); if(currdt!=255U) {return currdt;}} while(0);
#define InitTasks() do {unsigned char i; for(i=MAXTASKS;i>0U ;i--) {timers[i-1U]=0U;} } while(0)
#define UpdateTimers() do { unsigned char i;for(i=MAXTASKS;i>0U ;i--){ if(timers[i-1U]!=0U){ if(timers[i-1U]!=255U) { timers[i-1U]--;} } }} while(0)
#define SEM unsigned int
/*初始化信號(hào)量*/
#define InitSem(sem) (sem)=0;
/*等待信號(hào)量*/
#define WaitSem(sem) do{ (sem)=1; WaitX(0); if ((sem)>0) return 1;} while(0);
/*等待信號(hào)量或定時(shí)器溢出, 定時(shí)器tickets 最大為0xFFFE*/
#define WaitSemX(sem,tickets)do { (sem)=(tickets)+1; WaitX(0); if((sem)>1){ (sem)--;return 1;} } while(0);
/*發(fā)送信號(hào)量*/
#define SendSem(sem)do {(sem)=0;} while(0);
/*****小小調(diào)度器結(jié)束*******************************************************/
static unsigned char task0(void){
static int i;
_SS
for(i=0;i<11;i++){
WaitX(10U);
}
_EE
}
static unsigned char task1(void){
_SS
while(1){
WaitX(10U);
}
_EE
}
static unsigned char task2(void){
_SS
while(1){
WaitX(10U);
}
_EE
}
void InitT0()
{
TMOD = 0x21;
IE |= 0x82;// 12t
TL0=0Xff;
TH0=0XDB;
TR0 = 1;
}
void INTT0(void) interrupt 1 using 1
{
TL0=0Xff; //10ms 重裝
TH0=0XDB;//b7;
UpdateTimers();
RunTask(task0,0);//任務(wù)0具有精確按時(shí)獲得執(zhí)行的權(quán)限,要求:task0每次執(zhí)行消耗時(shí)間<0.5個(gè) ticket
}
int main(void)
{
InitT0();
InitTasks();
while(1){
RunTaskA(task1,1);//任務(wù)一具有比任務(wù)二優(yōu)先執(zhí)行的權(quán)限
RunTaskA(task2,2);
}
}
/****小小調(diào)度器開始**********************************************/
#define MAXTASKS 3U
volatile static unsigned char timers[MAXTASKS];
#define _SS static unsigned char _lc=0U; switch(_lc){case 0U:
#define _EE break; default:break;}_lc=0U;return 255U;
#define WaitX(tickets) _lc=(unsigned char)(__LINE__)%255U+1U;if(_lc){return (tickets);}break; case ((unsigned char)(__LINE__)%255U)+1U:
#define RunTask(TaskName,TaskID)do {if (timers[(TaskID)]==0U){ timers[(TaskID)]=(TaskName)(); } } while(0)
#define RunTaskA(TaskName,TaskID) { if (timers[(TaskID)]==0U) {timers[(TaskID)]=(TaskName)(); continue;} } /*前面的任務(wù)優(yōu)先保證執(zhí)行*/
#define CallSub(SubTaskName) do {unsigned char currdt; _lc=(unsigned char)(__LINE__)%255U+1U; if(_lc) {return 0U;} break; case (unsigned char)(__LINE__)%255U+1U:currdt=(SubTaskName)(); if(currdt!=255U) {return currdt;}} while(0);
#define InitTasks() do {unsigned char i; for(i=MAXTASKS;i>0U ;i--) {timers[i-1U]=0U;} } while(0)
#define UpdateTimers() do { unsigned char i;for(i=MAXTASKS;i>0U ;i--){ if(timers[i-1U]!=0U){ if(timers[i-1U]!=255U) { timers[i-1U]--;} } }} while(0)
#define SEM unsigned int
/*初始化信號(hào)量*/
#define InitSem(sem) (sem)=0;
/*等待信號(hào)量*/
#define WaitSem(sem) do{ (sem)=1; WaitX(0); if ((sem)>0) return 1;} while(0);
/*等待信號(hào)量或定時(shí)器溢出, 定時(shí)器tickets 最大為0xFFFE*/
#define WaitSemX(sem,tickets)do { (sem)=(tickets)+1; WaitX(0); if((sem)>1){ (sem)--;return 1;} } while(0);
/*發(fā)送信號(hào)量*/
#define SendSem(sem)do {(sem)=0;} while(0);
/*****小小調(diào)度器結(jié)束*******************************************************/
static unsigned char task0(void){
static int i;
_SS
for(i=0;i<11;i++){
WaitX(10U);
}
_EE
}
static unsigned char task1(void){
_SS
while(1){
WaitX(10U);
}
_EE
}
static unsigned char task2(void){
_SS
while(1){
WaitX(10U);
}
_EE
}
void InitT0()
{
TMOD = 0x21;
IE |= 0x82;// 12t
TL0=0Xff;
TH0=0XDB;
TR0 = 1;
}
void INTT0(void) interrupt 1 using 1
{
TL0=0Xff; //10ms 重裝
TH0=0XDB;//b7;
UpdateTimers();
RunTask(task0,0);//任務(wù)0具有精確按時(shí)獲得執(zhí)行的權(quán)限,要求:task0每次執(zhí)行消耗時(shí)間<0.5個(gè) ticket
}
int main(void)
{
InitT0();
InitTasks();
while(1){
RunTaskA(task1,1);//任務(wù)一具有比任務(wù)二優(yōu)先執(zhí)行的權(quán)限
RunTaskA(task2,2);
}
}
評(píng)論