不用OS,俺也能实现多任务切换。算是个“开裆裤”吧
#include<setjump.h> jmp_buf jumper0,jumper1,jumper2,jumper3; void Task0() { static int n=0; if(setjmp(jumper0)>0) { while(1) { if(setjmp(jumper0)==0) longjump(jumper1,1); //任务切换 n++; //一段任务代码 if(setjmp(jumper0)==0) longjump(jumper1,1); //任务切换 n++; //一段任务代码 } }
} void Task1() { static int n=0; if(setjmp(jumper1)>0) { while(1) { if(setjmp(jumper1)==0) longjump(jumper2,1); n++; if(setjmp(jumper1)==0) longjump(jumper2,1); n++; } }
} void Task2() { static int n=0; if(setjmp(jumper2)>0) { while(1) { if(setjmp(jumper2)==0) longjump(jumper3,1); n++; if(setjmp(jumper2)==0) longjump(jumper3,1); n++; } }
} void Task3() { static int n=0; if(setjmp(jumper3)>0) { while(1) { if(setjmp(jumper3)==0) longjump(jumper0,1); n++; if(setjmp(jumper3)==0) longjump(jumper0,1); n++; } }
} void InitTask() { Task0(); Task1(); Task2(); Task3(); } main() { InitTask(); longjmp(jumper0,1) }
//以上代码在keil c中调试通过 //非占先式任务切换 //任务内变量必须是静态的,子程序不用 //任务内不要用寄存器变量 |
|