版别一、
- /****小小调度器开端**********************************************/
- #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
- //初始化信号量
- #define InitSem(sem) sem=0;
- //等候信号量
- #define WaitSem(sem) do{ sem=1; WaitX(0); if (sem>0) return 1;} while(0);
- //等候信号量或定时器溢出, 定时器tickets 最大为0xFFFE
- #define WaitSemX(sem,tickets)do { sem=tickets+1; WaitX(0); if(sem>1){ sem–;return 1;} } while(0);
- //发送信号量
- #define SendSem(sem)do {sem=0;} while(0);
- /*****小小调度器完毕*******************************************************/
- 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);
- }
- }
/****小小调度器开端**********************************************/
#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;} } /*前面的使命优先确保履行*/
#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
/*初始化信号量*/
#define InitSem(sem) (sem)=0;
/*等候信号量*/
#define WaitSem(sem) do{ (sem)=1; WaitX(0); if ((sem)>0) return 1;} while(0);
/*等候信号量或定时器溢出, 定时器tickets 最大为0xFFFE*/
#define WaitSemX(sem,tickets)do { (sem)=(tickets)+1; WaitX(0); if((sem)>1){ (sem)–;return 1;} } while(0);
/*发送信号量*/
#define SendSem(sem)do {(sem)=0;} while(0);
/*****小小调度器完毕*******************************************************/
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);//使命0具有准确准时取得履行的权限,要求:task0每次履行耗费时刻<0.5个 ticket
}
int main(void)
{
InitT0();
InitTasks();
while(1){
RunTaskA(task1,1);//使命一具有比使命二优先履行的权限
RunTaskA(task2,2);
}
}