I found that using a different timer API works better for me. I created a timer module that has two API calls:
void timer_milliseconds_reset(unsigned index);
bool timer_milliseconds_elapsed(unsigned index, unsigned long value);
The timer indices are also defined in the timer header file:
#define TIMER_PRINT 0
#define TIMER_LED 1
#define MAX_MILLISECOND_TIMERS 2
I use unsigned long int for my timer counters (32-bit) since that is the native sized integer on my hardware platform, and that gives me elapsed times from 1 ms to about 49.7 days. You could have timer counters that are 16-bit which would give you elapsed times from 1 ms to about 65 seconds.
The timer counters are an array, and are incremented by the hardware timer (interrupt, task, or polling of counter value). They can be limited to the maximum value of the datatype in the function that handles the increment for a no-rollover timer.
/* variable counts interrupts */
static volatile unsigned long Millisecond_Counter[MAX_MILLISECOND_TIMERS];
bool timer_milliseconds_elapsed(
unsigned index,
unsigned long value)
{
if (index < MAX_MILLISECOND_TIMERS) {
return (Millisecond_Counter[index] >= value);
}
return false;
}
void timer_milliseconds_reset(
unsigned index)
{
if (index < MAX_MILLISECOND_TIMERS) {
Millisecond_Counter[index] = 0;
}
}
Then your code becomes:
//this is a bit contrived, but it illustrates what I'm trying to do
const uint16_t print_interval = 5000; // milliseconds
if (timer_milliseconds_elapsed(TIMER_PRINT, print_interval))
{
printf("Fault!\n");
timer_milliseconds_reset(TIMER_PRINT);
}