您的位置 首页 编程

极小资源单片机调度器

版本一、/****小小调度器开始**********************************************/#defineMAXTASKS2staticunsignedchartim

版别一、

  1. /****小小调度器开端**********************************************/
  2. #define MAXTASKS 2
  3. static unsigned char timers[MAXTASKS];
  4. unsigned char currdt;
  5. #define _SS static unsigned char _lc; switch(_lc){default:
  6. #define _EE ;}; _lc=0; return 255;
  7. #define WaitX(tickets)do {_lc=__LINE__+((__LINE__%256)==0); return tickets ;} while(0); case __LINE__+((__LINE__%256)==0):
  8. #define RunTask(TaskName,TaskID) do { if (timers[TaskID]==0) timers[TaskID]=TaskName(); }while(0);
  9. #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);
  10. #define UpdateTimers() unsigned char i; for(i=MAXTASKS;i>0 ;i–){if((timers[i-1]!=0)&&(timers[i-1]!=255)) timers[i-1]–;}
  11. #define SEM unsigned int
  12. //初始化信号量
  13. #define InitSem(sem) sem=0;
  14. //等候信号量
  15. #define WaitSem(sem) do{ sem=1; WaitX(0); if (sem>0) return 1;} while(0);
  16. //等候信号量或定时器溢出, 定时器tickets 最大为0xFFFE
  17. #define WaitSemX(sem,tickets)do { sem=tickets+1; WaitX(0); if(sem>1){ sem–;return 1;} } while(0);
  18. //发送信号量
  19. #define SendSem(sem)do {sem=0;} while(0);
  20. /*****小小调度器完毕*******************************************************/
  21. sbit LED1 = P2^1;
  22. sbit LED2 = P2^2;
  23. void InitT0()
  24. {
  25. TMOD = 0x21;
  26. IE |= 0x82;// 12t
  27. TL0=0Xff;
  28. TH0=0XDB;//22M—b7;
  29. TR0 = 1;
  30. }
  31. void INTT0(void) interrupt 1 using 1
  32. {
  33. UpdateTimers();
  34. TL0=0Xff; //10ms 重装
  35. TH0=0XDB;//b7;
  36. }
  37. voidtask1(){
  38. _SS
  39. while(1){
  40. WaitX(50);
  41. LED1=!LED1;
  42. }
  43. _EE
  44. }
  45. voidtask2(){
  46. _SS
  47. while(1){
  48. WaitX(100);
  49. LED2=!LED2;
  50. }
  51. _EE
  52. }
  53. void main()
  54. {
  55. InitT0();
  56. while(1){
  57. RunTask(task1,0);
  58. RunTask(task2,1);
  59. }
  60. }
版别二、
在IAR6.7下编译经过。(选用:Misra C 2004)

/****小小调度器开端**********************************************/
#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);
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/biancheng/275121.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部