This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf52, use Systick or RTC?

Hi,

After reading some questions about these topics I became a little confused.

I want to count milliseconds, so should I use Systick_Handler or use Real Time Counter Handler?

I've read that on sleep mode, Systick does not work, so this can be a pro to use RTC, but probably I will not set the NRF52 to sleep mode, so a simple and fast solution is to use Systick.

I've already tried with Systick, but the Systick_Handler never occurs, I'm not using sd_app_evt_wait(). What am I missing?

volatile uint32_t time = 0;

/* SysTick interrupt Handler. */
void SysTick_Handler(void)  {
    time++;
}


int main(void)
{
    SysTick_Config(SystemCoreClock / 1000);
    NVIC_EnableIRQ(SysTick_IRQn);

while (true) {}
}
Parents
  • Probably not a good idea to try to count milliseconds in code. Several things can and will go wrong with this. The first is that you will generate 1,000 interrupts every second, so depending on how much other code you run on the system the possibility of the NVIC dumping some of your interrupts due to overflow is real.

    Also, your code is secondary in the system. So if you choose to run the SD, your interrupts will only get serviced after the SD interrupts. Or, another way to think of it is that whenever any radio activity is going on, your code is not being serviced.

    Finally due to all of the other interrupts and their relative priority (eg, radio activity) the latency in servicing your millisecond handler could easily be nearly 1 millisecond.

    The only effective way to count milliseconds is in hardware. There are several dedicated counters that can be tied to the RTC or main system clock for this purpose. Then you only need to capture the counter value during events to find out the millisecond interval that has elapsed.

    Someone else recently had a very similar question. You can see it here: devzone.nordicsemi.com/.../

  • Thanks AmbystomaLabs, I used RTC module and it works! On the future we will decide better how to measure time.

Reply Children
No Data
Related