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" <<ENDL;} Void hello3 () { cout << "hello333" <<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