I am looking for help on embedded application on 16bit device. I need to run several simple "tasks/functions" via function pointers. Those tasks run in predeterimned intervals.
typedef struct
{
int timeToRun;
void (*fcn)(void);
} task_t;
task_t tasks[] =
{
{ 13_MSEC, fcn1 },
{ 50_MSEC, fcn2 },
{ 0, NULL }
};
volatile unsigned int time;
main()
{
for (ptr = tasks; ptr->timeToRun !=0; ptr++)
{
if (!(time % ptr->timeToRun))
(ptr->fcn)();
}
}
I have possibility to run timer interrupt at 1ms.
interrupt void TimerTick(void)
{
time++;
}
Any idea how to calculate elapsed time? How to make sure that % (modulo) works in definded rate if time overflows. Anyway how to avoid time overflow and have correct timing via % (modulo)?
I would do something like this:
This should work as long as you can guarantee that a) no period is greater than half the rollover time (0x8000 ms), and b) you can execute all the functions within the shortest period.