Scheduler, minimal resources microcontroller is worth a use

Source: Internet
Author: User
Tags semaphore

Source: again out of a scheduler, small resources microcontroller is worth a use

Self-thought has the following characteristics:

1) Super portability, CPU-independent, almost any CPU that supports C programming can be used! (This article only takes 51 single-chip microcomputer as an example, but can actually be transplanted arbitrarily)
2) Small and small, the principle is very simple, a look will understand.
3) province and province, can be said to the RAM and ROM to the extreme.
4) Take the essence of Protothread, the timer and the state machine and the pseudo-threading syntax into a framework, the task function can be written in two ways.
5) based on timer triggering, high scheduling efficiency, minimizing invalid code run time.

***********************************************************/#include<stc89c51.h>#include<stdio.h>/***************** Small Scheduler section starts ********************************************/  #define_ss static char lc=0; Switch (LC) {case 0:lc=0;#define_ee}; lc=0;#defineWaitx (A, B) SetTimer (&AMP;LC,__LINE__,A,B); return; Case __LINE__:structTASK {CharTD; void(*FP) (); }; #defineMaxtasks 5structTASK Tasks[maxtasks];//Set TimervoidSetTimer (Char*LC,CharLineCharTmrid,intd) {   *lc=Line ; TASKS[TMRID].TD=D;} //Logic timer processing, called in the timer interruptvoiddectimers () {unsignedChari;  for(i=0; i<maxtasks;i++){     if(tasks[i].td>0) tasks[i].td--; } } //Task Scheduler function, run in mainvoidruntasks () {unsignedChari;  for(i=0; i<maxtasks;i++)            {         if(tasks[i].fp!=0){                if(tasks[i].td==0) {TASKS[I].TD=-1;                 TASKS[I].FP (); }            }                  } } /**************** Small Scheduler partial end *******************************************************/sbit KEY= p3^2; unsignedCharCode numtab[ -]={0x24,0x6F,0xE0,0x62,0x2B,0x32,0x30,0x67,0x20,0x22,0x21,0x38,0xb4,0x68,0xb0,0xb1}; SFR iap_contr=0xC7; SFR wdt_contr=0xC1; //clear the watchdogvoidClr_wdt () {wdt_contr=0x3C; } //Initialize TimervoidInitT0 () {Tmod=0x21; IE|=0x82;//12ttl0=0Xff; TH0=0xb7; TR0=1; } //Timer InterruptvoidINTT0 (void) Interrupt1 using 1{TL0=0Xff;//10ms re-assemblyth0=0xb7; Dectimers (); } sbit LED1= p2^4; //task one, state machine notationvoidOntimer0 () {LED1=! LED1;//The LED1 pin is connected to the cathode of the light emitting tube, the led1=0 is bright, and the led1=1 is extinguished. //Reload Timer  if(LED1) tasks[0].td= $;//450mS extinguished  Elsetasks[0].td=5;//50ms Bright} //task two, state machine notationCharKeycount=0; voidTask1 () {if(key==0) {Keycount++; if(keycount> -) Iap_contr =0x60;//Press the key for 1 seconds to reboot and enter the firmware upgrade}Else{Keycount=0; } //Reload Timertasks[1].td=5; } //task three, pseudo thread notationvoidTask2 () {Static Chari; _ss while(1){  for(i=0; i<=9; i++) {//quick display from 0--9, interval 200mSWAITX (2, -);//wait 200mS, is actually set timer 2 for 200mSp1=numtab[i];}  for(i=0; i<=9; i++) {//slow display from 0--9, interval 500mSWAITX (2, -);//wait 500mS, is actually set timer 2 for 500mSp1=numtab[i];} } _ee}voidMain () {unsignedCharKeynum; P3M0=0x00; P3M1=0x00; //wdt_contr= 0x00; //turn off the watchdogP1 =0xFF;//off DisplayCLR_WDT ();         InitT0 (); KEY=1;//Key IO Portkeynum=0;//press the number of times//Load Task:tasks[0].fp=Ontimer0; tasks[1].fp=Task1; tasks[2].fp=Task2; //Cyclic scheduling         while(1) {runtasks ();         CLR_WDT (); } }

Optimization Endless! Oh, 330 floor seemingly can not optimize again, but I try to do once again optimization:

Please review this version to see if you can also optimize:

/** * * Small scheduler start **********************************************/#defineMaxtasks 2StaticUnsignedChartimers[maxtasks];unsignedCharCurrdt;#define_SS static unsigned char _lc; Switch (_LC) {default:#define_ee;}; _lc=0; return 255;#defineWAITX (tickets) do {_lc=__line__+ ((__line__%256) ==0), return tickets;} while (0); Case __line__+ ((__line__%256) ==0):#defineRunTask (Taskname,taskid) do {if (timers[taskid]==0) Timers[taskid]=taskname ();} while (0);#defineCallsub (subtaskname) do {_lc=__line__+ (__line__%256) ==0), return 0; case __line__+ ((__line__%256) ==0): currdt= Subtaskname (); if (currdt!=255) return Currdt;} while (0);#defineUpdatetimers () unsigned char i; for (i=maxtasks;i>0; i--) {if ((timers[i-1]!=0) && (timers[i-1]!=255)) timers[i-1]--;}#defineSEM unsigned int//Initialize the semaphore#defineINITSEM (SEM) sem=0;//wait for the semaphore#defineWAITSEM (SEM) do{sem=1; WAITX (0); if (sem>0) return 1;} while (0);//wait for semaphore or timer overflow, timer tickets max is 0xFFFE#defineWAITSEMX (sem,tickets) do {sem=tickets+1; WAITX (0);  if (sem>1) {sem--; return 1;} } while (0);//Send Signal Volume#defineSENDSEM (SEM) does {sem=0;} while (0);/** * * Small scheduler End *******************************************************/sbit LED1= p2^1; Sbit LED2= p2^2;voidInitT0 () {Tmod=0x21; IE|=0x82;//12ttl0=0Xff; TH0=0XDB;//22M---b7;TR0 =1;}voidINTT0 (void) Interrupt1 using 1{updatetimers (); TL0=0Xff;//10ms re-assemblyth0=0XDB;//B7; }voidTask1 () {_SS while(1) {WAITX ( -); LED1=!LED1; }_ee}voidTask2 () {_SS while(1) {WAITX ( -); LED2=!LED2; }_ee}voidMain () {InitT0 ();  while(1) {RunTask (Task1,0); RunTask (Task2,1); }}

Compiled under Keil, it also reduces the ROM by 18 bytes (more than 10%). Should run more efficiently.

------------------The following is a description-----------------------------------

Small Scheduler task function of the main note, there are three main points:

1) The internal variables of the task function are defined by static local variables.
2) The switch statement cannot be used within the task function.
3) within the task function, you cannot use the return statement. Because return has been given the specific meaning of the task delay. (This is a mandatory requirement for the return-type task function version)

These three points, and does not obviously cause the writing process inconvenient.
---------------------------
The cost of running from bare to operating system OS or scheduling system is mainly:

The cost of hardware resources (the consumption of RAM and ROM), the cost of learning (learn its principles, and master its usage), the cost of porting (the amount of work migrated to different CPUs), the cost of efficiency (additional CPU burden after using the dispatch system), the commercial cost (copyright fee), Stability costs (whether to introduce potential instability factors or increase bug tracking debugging effort).

In these respects, the cost of applying a small scheduler is very small.
1) hardware resource cost: The previous optimized version already indicates the problem. Keil, this routine ram consumption: 22 bytes, rom consumption 126 bytes.
2) Learning Cost: The small scheduler has only a total of more than 10 lines of code, if we do a simple explanation, the understanding is actually very fast. I believe that learning time is shorter than other scheduling systems.
3) transplant cost: almost no transplant workload, almost all-in-one for a variety of CPUs.
4) Efficiency cost: We have been trying to optimize, I believe the efficiency of the dispatch is not low. For example, the task switching time, should be able to do the US level, or even the sub-US level.
5) Commercial Cost: Small This scheduler is free to use without paying any fees.
6) Stability Cost: The small scheduler is essentially just a few macros, does not involve any internal register or stack operation, avoids the introduction of unstable risk factors, all operations are predictable, can be controlled under the premise.
--------------------------------------------------------------------------

The purpose of this scheduler is: to realize the multi-task parallel processing mechanism based on natural grammar at the minimum cost. and has a high degree of portability of the code.
Each task consumes 3 bytes of RAM. There is no limit to the number of tasks.

Its working principle is very simple, we can on this basis any DIY own scheduling method.

Scheduler, minimal resource microcontroller worth a use (turn)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.