0

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.

hrishikesh
  • 11
  • 2
  • We want an [MRE](https://stackoverflow.com/help/minimal-reproducible-example). For C, we want a _single_ `.c` file [_with_ (e.g.) `#include `]. But, _no_ (e.g.) `#include "myprogram.h"`. For runtime errors, it should compile cleanly. We should be able to download it and build and run it on our local systems [if we so choose]. Your issue might be that your setup of the timer isn't correct. We need to see the code to tell. – Craig Estey Jun 08 '23 at 17:24

1 Answers1

1

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();
    }
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48