首页 论坛 原厂专区 ST MCU 推荐一个纯C的调度器,适合于用于小资源的单片机上,已经量产验证过

发帖 回复

[资料] 推荐一个纯C的调度器,适合于用于小资源的单片机上,已经量产验证过
730 查看
8 回复
 楼主 | 发布于 2018-01-30 | 只看楼主
分享到:

这是转载的一个网友的小调度器,是根据Protothread的思想编写的片轮调度器,

初步具有多任务的思想,占用资源极少,适合小资源的单片机使用,已经量产验证过,比较稳定,不过编写代码时还是有些注意事项

作者的github:https://github.com/smset028/tiny-scheduler

代码如下:有兴趣可以一起讨论

/****begin of scheduler ********************************************/
#define MAXTASKS 2
volatile unsigned short timers[MAXTASKS];
#define _SS static unsigned char _lc=0; switch(_lc){default: 
#define _EE ;}; _lc=0; return 65535;
#define WaitX(tickets)  do {_lc=(__LINE__%255)+1; return tickets ; case (__LINE__%255)+1: ;} while(0)
#define InitTasks() {unsigned char i; for(i=MAXTASKS;i>0 ;i--) timers[i-1]=0; }
#define RunTask(TaskName,TaskID) do { if (timers[TaskID]==0) timers[TaskID]=TaskName(); }  while(0)
#define CallSub(SubTaskName) do {unsigned short currdt; _lc=(__LINE__%255)+1; return 0; case (__LINE__%255)+1:  currdt=SubTaskName(); if(currdt!=65535) return currdt;} while(0) 
#define UpdateTimers() {unsigned char i; for(i=MAXTASKS;i>0 ;i--){if((timers[i-1]!=0)&&(timers[i-1]!=65535)) timers[i-1]--;}}
/*--end of scheduler-------------------------------------------------*/

unsigned short task1(){
_SS
  pinMode(BUILTIN_LED, OUTPUT);
  while(1){
      digitalWrite(BUILTIN_LED,HIGH);
      WaitX(50);   
      digitalWrite(BUILTIN_LED,LOW);
      WaitX(50);
  }
_EE
}

unsigned short  task2(){
_SS
  pinMode(14, OUTPUT);
  while(1){
      digitalWrite(13,HIGH);
      WaitX(100);   
      digitalWrite(13,LOW);
      WaitX(100);
  }
_EE
}


void setup() {
  // put your setup code here, to run once:
   InitTasks();
}

void loop() {
  // put your main code here, to run repeatedly:
    delay(10);
    UpdateTimers();
    RunTask(task1,0);                   
    RunTask(task2,1);                   
}

(1 ) (1 )
回复 举报

回复于 2018-01-30 沙发

多谢分享~~
(0 )
评论 (0) 举报

回复于 2018-01-30 2#

此文章来源于融创芯城(www.digiic.com)—— 半导体电子领域共享平台
https://github.com/smset028/tiny-scheduler
(0 )
评论 (0) 举报

回复于 2018-01-30 3#

点赞 谢谢分享
(0 )
评论 (0) 举报

回复于 2018-01-31 4#

感谢分享;
(0 )
评论 (0) 举报

回复于 2018-01-31 5#

谢谢分享
(0 )
评论 (0) 举报

回复于 2019-03-13 6#

多谢分享!!!!!
(0 )
评论 (0) 举报

回复于 2019-03-16 7#

谢谢分享
(0 )
评论 (0) 举报

回复于 2019-03-16 8#

支持下,谢谢分享!
(0 )
评论 (0) 举报
  • 发表回复
    0/3000





    举报

    请选择举报类别

    • 广告垃圾
    • 违规内容
    • 恶意灌水
    • 重复发帖

    全部板块

    返回顶部