is it possible to get a 24 hour timer interrupt using timer5 in dspic30f4011?

59 Views Asked by At

I am trying to trigger an alarm after 24 hours using timer5 of dspic30f4011 using 1:256 as prescale.

using maximum value of timer (0xFFFF) i tried iterating for counter of 125 and 250, for this values it works but if i enter any value after 250 my timer seems to be stuck and it doesnot trigger alarm.

1

There are 1 best solutions below

0
Craig Estey On

As mentioned in my top comments, we want an MRE.

But, 24 hours is a long time for an interval timer. So, I don't think this can be done purely with the timer hardware as the timer compare registers are [AFAICT] limited to 16 bits. Even at one interrupt / second, this is 86400 (0x15180) interrupts which is beyond 16 bits

We'll have to do this in S/W [with a "tick" counter]. This is how most systems do it.

Here is an example. It assumes that the timer ISR is called once per millisecond [adjust to suit]:

#define TICKS_PER_SECOND    1000LL

uint64_t tick_count = 0;                // monotonic tick counter
uint64_t tick_interval = TICKS_PER_SECOND * 24 * 60 * 60; // ticks for 1 day
uint64_t tick_fire = tick_interval;     // time of next callback

void
timer_ISR(void)
{

    // advance tick counter
    tick_count += 1;

    // have we hit the required interval?
    if (tick_count >= tick_fire) {
        // advance to next interval
        tick_fire = tick_count + tick_interval;

        // NOTE: doing that first allows the callback function to adjust the
        // firing time of the next event

        // called once every 24 hours ...
        timer_24_hours();
    }
}