This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Timer seems to expire earlier than expected

Hello Nordic team,

I am testing a timer using code snippet which Nordic team recommended as a reference. My code is something like this:

uint64_t start, stop;
void timer_start()
{
  NRF_TIMER2->MODE = TIMER_MODE_MODE_Timer; // Set the timer in Counter Mode
  NRF_TIMER2->TASKS_CLEAR = 1; // clear the task first to be usable for later
  NRF_TIMER2->PRESCALER = 4; // Set prescaler. Higher number gives slower timer. Prescaler = 0 gives 16MHz timer
  NRF_TIMER2->BITMODE = TIMER_BITMODE_BITMODE_32Bit; // Set counter to 16 bit resolution
  NRF_TIMER2->CC[0] = 1000000; // Set value for TIMER2 compare register 0. set timer value to 1000ms.
  NRF_TIMER2->CC[1] = 0; // Set value for TIMER2 compare register 1

  // Enable interrupt on Timer 2, both for CC[0] and CC[1] compare match events
  NRF_TIMER2->INTENSET
    = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos);
  NVIC_EnableIRQ(TIMER2_IRQn);
  NRF_TIMER2->TASKS_START = 1; // Start TIMER2
  start = app_timer_get_now();
}

void TIMER2_IRQHandler(void)
{
  stop = app_timer_get_now();  
  NRF_LOG_INFO("start =%ld stop = %ld delta=%ld", start, stop, stop-start);
  if ((NRF_TIMER2->EVENTS_COMPARE[0] != 0) && ((NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0))
  {
    NRF_TIMER2->EVENTS_COMPARE[0] = 0;
  }
}

As you can see in the code, I added some code to read out the RTC counter value to compare the time when timer start and expires with respect to RTC time. The result was:

start =9 stop = 16290 delta=16281

The time between start and stop is 16281, while I am expecting the delta is to be around 16384 ( 16384 is 1000ms in RTC , right?). it seems the timer expires a little earlier(103 ticks = 6.2866 ms). Can you please advise what I was wrong in testing the timer operation?

Thanks,

Robin

Parents
  • Hi Robin

    Which clock sources are you using for the high frequency and low frequency clocks? 

    By default the chip will use the internal clock sources, which typically have an inaccuracy around 5%. If this is the case then the numbers you report are actually pretty accurate (about 0.6% offset). 

    Best regards
    Torbjørn

Reply
  • Hi Robin

    Which clock sources are you using for the high frequency and low frequency clocks? 

    By default the chip will use the internal clock sources, which typically have an inaccuracy around 5%. If this is the case then the numbers you report are actually pretty accurate (about 0.6% offset). 

    Best regards
    Torbjørn

Children
No Data
Related