A small system used on a single chip microcomputer for the Scheduler state machine

Source: Internet
Author: User

Task by TIME Chip LUN practice

eg:

while (1)

{

if (task_delay[0]==0)

Task1 ();

if (task_delay[1]==0)

Task2 ();

}

void Time0 (void) interrupt1

{

for (i=0;i<task_max;i++)

{

if (Task_delay[i])

task_delay[i]--;

}

This structure is suitable for each task time below 10ms

If the length of time as 18B20 initialization can be divided into multiple fragments ( state machine )

eg:

Case 0: ...

Flag=1;

Case 1:

flag=2;

The system is implemented by the priority level

while (1)

{

for (i=0;i<max_task;i++)

{

if (task_delay[i]==0) {run (task[i]);

}

}

Note the break !!! in the program He's the one who can prioritize programs .

void (* code task[]) () = {task0,task1,task2,task3};// get task pc pointer

void run (void (*ptask))// pointer function pointing to function

{

(*ptask) ();

}

When used:run (task[i]);

#include <iostream> #include <unistd.h> #include <signal.h> #include <time.h> #include <sys/ Time.h>using namespace std;void hello1 () {    cout << "Hello1" <<endl;} Void hello2 () {    cout << "Hello22" &LT;&LT;ENDL;} Void hello3 () {    cout << "hello333" &LT;&LT;ENDL;} int task_delay[3] = {10,50,100};void  (* task[]) ()  = {hello1,hello2,hello3}; Void run (void  (*ptask) ()) {     (*ptask) ();} Void timer (Int signo) {    for  (int i = 0; i <  3; ++i)     {        if  (task_delay [i])         {             task_delay[i] --;        }&nBsp;   }    signal (Sigalrm, timer);    return;} Int main (int argc, char *argv[]) {    signal (SIGALRM, timer);     struct itimerval value;    value.it_value.tv_sec =  0;    value.it_value.tv_usec = 10000;    value.it_ interval.tv_sec = 0;    value.it_interval.tv_usec = 10000;     setitimer (Itimer_real, &value, &value);    while  (True)     {        if  (! task_delay[0])          {             run (task[0]);            task_delay[0]  = 10;        }        else if  (! task_ DELAY[1])         {             run (task[1]);             task_delay[1] = 50;        }         else if  (! task_delay[2])         {             run (task[2]);             task_delay[2] = 100;         }    }    return 0;}


This article is from the "Don't Build a Wheel" blog, please make sure to keep this source http://xubuntu.blog.51cto.com/7426832/1869954

A small system used on a single chip microcomputer for the Scheduler state machine

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.