新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 極小資源單片機(jī)調(diào)度器

極小資源單片機(jī)調(diào)度器

作者: 時(shí)間:2016-11-11 來源:網(wǎng)絡(luò) 收藏
版本一、
  1. /****小小調(diào)度器開始**********************************************/
  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. //初始化信號(hào)量
  13. #define InitSem(sem) sem=0;
  14. //等待信號(hào)量
  15. #define WaitSem(sem) do{ sem=1; WaitX(0); if (sem>0) return 1;} while(0);
  16. //等待信號(hào)量或定時(shí)器溢出, 定時(shí)器tickets 最大為0xFFFE
  17. #define WaitSemX(sem,tickets)do { sem=tickets+1; WaitX(0); if(sem>1){ sem--;return 1;} } while(0);
  18. //發(fā)送信號(hào)量
  19. #define SendSem(sem)do {sem=0;} while(0);
  20. /*****小小調(diào)度器結(jié)束*******************************************************/
  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)

/****小小調(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);
}
}


關(guān)鍵詞: 單片機(jī)調(diào)度

評(píng)論


技術(shù)專區(qū)

關(guān)閉