Hi guys
I usually need to run multiple tasks in a PIC, but recently I found myself need to assign some task with higher priority, and some task with lower priority.
Here is how I normally do it (with no priority):
I can see there is limitation with this method, and one thing is, with this, there is no priority. How do you guys do it if you need to add priority to the tasks. Or what is the better way to do this?
thanks guys!!
I usually need to run multiple tasks in a PIC, but recently I found myself need to assign some task with higher priority, and some task with lower priority.
Here is how I normally do it (with no priority):
C:
// function pointer
typedef void (*TASK)(void);
// TICK is my structure for timer tick, the structure is not show
typedef struct {
TICK LastRun; // ticks of this task is last executed
TICK interval; // how often the task need to be executed
TASK task; // pointing to the task
uint8_t isTaskEmpty : 1; // to tell if this structure is assigned a task or not
} TASK_T;
TASK_T task[MAX_TASK];
void createTask(TASK t, TICK interval)
{
// a function to:
// loop through task[]
// find an empty slot
// assign the values to task[] and initial conditions
}
// create some tasks
createTask(task_01, TICK_10MS);
createTask(task_02, TICK_20MS);
createTask(task_03, TICK_30MS);
createTask(task_04, TICK_40MS);
void taskScheduler(void)
{
// loop through all tasks
for (uint8_t i = 0; i < MAX_TASK; i++)
{
// if task is valid
if (!task[i].isTaskEmpty)
{
// if task expired
if ((TICK_NOW - task[i].LastRun) > task[i].interval)
{
task[i].LastRun = TICK_NOW; // update time
task[i].task(); // run task
}
}
}
}
// main loop
while(1)
{
taskScheduler();
}
thanks guys!!