Timed lista de tarefas
Pergunta
Eu estou procurando ajuda no aplicativo embarcado em 16bit dispositivo.Eu preciso para executar diversas simples "tarefas/funções através de ponteiros de função.Essas tarefas são executadas em predeterimned intervalos.
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)();
}
}
Eu tenho possibilidade de executar interrupção do timer em 1ms.
interrupt void TimerTick(void)
{
time++;
}
Alguma idéia de como calcular o tempo decorrido?Como certificar-se de que, % (módulo) trabalha em definded taxa se o tempo estoura.De qualquer maneira como evitar sobrecarga e ter o timing correto através % (módulo)?
Solução
Gostaria de fazer algo como isto:
typedef struct
{
unsigned int nextRunTime
int period;
unsigned int rollover;
void (*fcn)(void);
} task_t;
main()
{
//setup goes here
/*...*/
//loop
while (1)
{
for (ptr = tasks; ptr->period!=0; ptr++)
{
if ((time > ptr->nextRunTime) && (time <= ptr->rollover) )
{
ptr->nextRunTime+=ptr->period;
ptr->rollover = (ptr->nextRunTime < ptr->period)? 2*ptr->period : 0xFFFF;
(ptr->fcn)();
}
ptr->nextRunTime = timeToRun;
}
}
}
Isso deve funcionar como long como você pode garantir que: (a) nenhum período é maior do que a metade do rollover de tempo (0 x 8000 ms), e b) você pode executar todas as funções dentro do período mais curto.
Outras dicas
Aqui está um código de um aplicativo semelhante de minas, adequado para pequenas MCU aplicações, e MISRA-C conformes.Ele é baseado na atribuição de software de "timers" estaticamente no aplicativo de chamada.Vários módulos em seu projeto pode usar o temporizador mesmo módulo, como é usando uma lista ligada internamente para manter o controle de todos os temporizadores.
Chamada tim_traverse_timers() a partir do seu 1ms de interrupção.Se você tem uma precisão muito elevada exigências, você pode ter que limpar a fonte de interrupção antes de chamar a função, para que o "código de jitter" sobrecarga da função em si não afeta o temporizador.
Se você precisa de mais atrasos, em seguida, 65535ms, basta alterar o contador e o intervalo para uint32.
typedef struct timer
{
struct timer* next; /* Next timer in the linked list */
uint16 counter; /* 16-bit timer counter */
uint16 interval; /* The interval between triggers */
BOOL is_enabled; /* Timer enabled/disabled */
void (*callback_func)(void); /* Callback timer function */
} Timer;
static Timer* timer_list;
void tim_init (void)
{
timer_list = NULL;
}
void tim_add (Timer* timer,
void (* callback_func)(void),
uint16 interval_ms,
BOOL enabled)
{
tim_enable_interrupt (FALSE); /* hardware function disabling timer interrupt */
timer->callback_func = callback_func;
timer->counter = 0U;
timer->interval = interval_ms;
timer->is_enabled = enabled;
timer->next = timer_list;
timer_list = timer;
tim_enable_interrupt (TRUE);
}
void tim_enable (Timer* timer, BOOL enable)
{
if(enable)
{
timer->counter = 0U; /* Reset counter each time function is called */
}
timer->is_enabled = enable;
}
void tim_traverse_timers (void)
{
Timer* timer;
for(timer=timer_list; timer!=NULL; timer=timer->next)
{
if(timer->is_enabled == TRUE)
{
timer->counter++;
if(timer->counter == timer->interval)
{
timer->counter = 0U;
timer->callback_func();
}
}
}
}
#include "timer.h"
void my_func (void); /* lights some LED etc... */
void my_other_func (void);
void main (void)
{
Timer some_task;
Timer some_other_task;
...
tim_init();
...
tim_add(&some_task, &my_func, SOME_DELAY_IN_MS, TRUE);
tim_add(&some_other_task, &my_other_func, SOME_OTHER_DELAY_IN_MS, TRUE);
...
}