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

Timer event capture clarification

I want to use a timer to generate an event at a rate of 16kHz. So just as a test I'm setting up TIMER4 and a CC event to toggle a gpio pin. As I understand the clock source should be 16MHz so I'm loading the CC register with 1000 to generate an interrupt @16kHz.

I'm expecting the gpio to toggle at a frequency if 16kHz (giving a 8 kHz squarewave) but I'm getting an actual output of 6.74 kHz on my PCA10040 board. Am I misunderstanding the operation of the timer module, or doing something wrong?

This is my timer initialization

void timers_init()
{
  NRF_TIMER4->MODE =      TIMER_MODE_MODE_Timer;
  NRF_TIMER4->PRESCALER = 0;                                    
  NRF_TIMER4->TASKS_CLEAR = 1;                                  
  NRF_TIMER4->BITMODE =   TIMER_BITMODE_BITMODE_32Bit;		
  NRF_TIMER4->CC[0] =     1000;
  NRF_TIMER4->INTENSET =  (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos);
  NVIC_EnableIRQ(TIMER4_IRQn);
  NRF_TIMER4->TASKS_START = 1;               // Start TIMER
}

and IRQ

void TIMER4_IRQHandler(void)
{
  if ((NRF_TIMER4->EVENTS_COMPARE[0] != 0) && ((NRF_TIMER4->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0)) 
  {
    NRF_TIMER4->EVENTS_COMPARE[0] = 0; //Clear compare register 0 event
    NRF_TIMER4->TASKS_CLEAR = 1;
    NRF_TIMER4->TASKS_START = 1;
    nrf_gpio_pin_toggle(PIN_OUT);
  }
}

Parents
  • Hi,

    your interrupt handler takes some time, that's why frequency is lower. Instead of restarting timer in interrupt handler, add a shortcut for clearing it by COMPARE[0] event (timer is not stopped on CLEAR task):

      NRF_TIMER4->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;

    Also you can toggle a pin with GPIOTE module, without waking up CPU at all.

Reply
  • Hi,

    your interrupt handler takes some time, that's why frequency is lower. Instead of restarting timer in interrupt handler, add a shortcut for clearing it by COMPARE[0] event (timer is not stopped on CLEAR task):

      NRF_TIMER4->SHORTS = TIMER_SHORTS_COMPARE0_CLEAR_Msk;

    Also you can toggle a pin with GPIOTE module, without waking up CPU at all.

Children
Related