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

Two timers on RTC1

Hi, This is loosely related to the other question I asked a few weeks ago (devzone.nordicsemi.com/.../two-interrupt-events-on-timer0) but this time I want to have two timers on RTC1 generating interrupt events. I'm using the internal RC clock. I'm trying the following code:


void lfclk_config()
{
  NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos);
  NRF_CLOCK->TASKS_LFCLKSTART = 1;
  while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0) {}
  NRF_RTC1->PRESCALER = 32; //1kHz frequency
  NRF_RTC1->CC[0] = 9 * 1000;
  NRF_RTC1->CC[1] = 2 * 1000;

  NRF_RTC1->EVTENSET = RTC_EVTENSET_COMPARE0_Msk;
  NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk;
  NRF_RTC1->EVTENSET = RTC_EVTENSET_COMPARE1_Msk;
  NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE1_Msk;
  NRF_RTC1->TASKS_START = 1;
  NVIC_EnableIRQ(RTC1_IRQn);
}

void RTC1_IRQHandler()
{
  if (NRF_RTC1->EVENTS_COMPARE[0] != 0)
  {
    NRF_RTC1->EVENTS_COMPARE[0] = 0;
  }
  if (NRF_RTC1->EVENTS_COMPARE[1] != 0)
  {
    NRF_RTC1->EVENTS_COMPARE[1] = 0;
  }
}

int main(void) {

  lfclk_config();
  while(1) {
  }
}

Here I want to setup two comparators, a 9sec and a 2sec one. Each should generate a periodic interrupt on their respective interval which is then handled by RTC1_IRQHandler. The behaviour I see is that it seems that both comparators fire on time once but afterwards there's no interrupt generated. My intention is to have periodic interrupts, not a single one.

What am I doing wrong?

Thanks! florin

Parents
  • Hello,

    Because you need to re-set the comparators (or reset the tick event, but it is not what you want in your case) to be notified when the counter will reach the comparators values.

    To do that, you need to re-set the CC comparator in your interrupt like this for example:

    void RTC1_IRQHandler()
    {
      if (NRF_RTC1->EVENTS_COMPARE[0] != 0)
      {
        NRF_RTC1->EVENTS_COMPARE[0] = 0;
        NRF_RTC1->CC[0] += 9 * 1000;
      }
      if (NRF_RTC1->EVENTS_COMPARE[1] != 0)
      {
        NRF_RTC1->EVENTS_COMPARE[1] = 0;
        NRF_RTC1->CC[1] += 2 * 1000;
      }
    }
    

    If you don't do that, you need to wait the next time that the counter will reach the value of your comparator : reach the overflow value (24 bits counter = 0xFFFFFF/32768 = 511 seconds with prescaler = 0) and restart to count from 0.

Reply
  • Hello,

    Because you need to re-set the comparators (or reset the tick event, but it is not what you want in your case) to be notified when the counter will reach the comparators values.

    To do that, you need to re-set the CC comparator in your interrupt like this for example:

    void RTC1_IRQHandler()
    {
      if (NRF_RTC1->EVENTS_COMPARE[0] != 0)
      {
        NRF_RTC1->EVENTS_COMPARE[0] = 0;
        NRF_RTC1->CC[0] += 9 * 1000;
      }
      if (NRF_RTC1->EVENTS_COMPARE[1] != 0)
      {
        NRF_RTC1->EVENTS_COMPARE[1] = 0;
        NRF_RTC1->CC[1] += 2 * 1000;
      }
    }
    

    If you don't do that, you need to wait the next time that the counter will reach the value of your comparator : reach the overflow value (24 bits counter = 0xFFFFFF/32768 = 511 seconds with prescaler = 0) and restart to count from 0.

Children
Related