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

Timer interrupt interval question

I have been working on making at least 1us Timer interval(expect faster than 1MHz) using interrupt.

To do this we need to set the prescaler of Timer to 4, since formula of making Timer Tick is TimerTick = 16MHz / 2^prescaler.

or set the cc to 16 and precaler to 0 on the Timer, since 1 tick time of 16Mhz is (1/16)us.

I thought it should be simple to implement. However the generated clock speed was not exact what I expected. The maximum interrupt interval I got was less than 200kHz

In addition, I tried on slow interval rate (such as 125kHz or slower). However the interrupt time was not exact time I expected. The interrupt time was delayed as follows of I added more source code.

void TIMER0_IRQHandler()
{
if (NRF_TIMER0->EVENTS_COMPARE[0] != 0) 
{
		NRF_TIMER0->EVENTS_COMPARE[0]  = 0;
		NRF_TIMER0->TASKS_CLEAR         = 1;
		nrf_gpio_pin_toggle(GPIO_TOGGLE_PIN);
  }
}

void timer0_config(void)
{
    // Start 32 MHz crystal oscillator
    NRF_TIMER0->MODE        = TIMER_MODE_MODE_Timer;
    NRF_TIMER0->PRESCALER   = 0U;
	NRF_TIMER0->BITMODE     = TIMER_BITMODE_BITMODE_16Bit;  // 16 bit mode.
	NRF_TIMER0->TASKS_CLEAR = 1;            // clear the task first to be usable for later.

	NRF_TIMER0->CC[0] = 16;

     NRF_TIMER0->INTENSET = TIMER_INTENSET_COMPARE0_Enabled
    << TIMER_INTENSET_COMPARE0_Pos; 
	NVIC_EnableIRQ(TIMER0_IRQn);             // Enable Interrupt for the Timer in the core.		

}

I don't get which part of modification to generate high frequency interrupt.

Parents
  • Hi,

    when your expected interrupt frequency is 1MHz it means that you have 16 instruction per interrupt. I'm affraid that it won't be enough to enter,execute and exit the interrupt so you are probably loosing some interrupts.

    However, there are few things that can be recommended:

    use SHORTS register in NRF_TIMERx to clear the counter once COMPARE0 event happens. Then you don't need TASK_CLEAR = 1 in the interrupt

    if the purpose of your interrupt is to generate external 1 MHz clock by pin toggling look into PPI module and you can do it without core usage.

Reply
  • Hi,

    when your expected interrupt frequency is 1MHz it means that you have 16 instruction per interrupt. I'm affraid that it won't be enough to enter,execute and exit the interrupt so you are probably loosing some interrupts.

    However, there are few things that can be recommended:

    use SHORTS register in NRF_TIMERx to clear the counter once COMPARE0 event happens. Then you don't need TASK_CLEAR = 1 in the interrupt

    if the purpose of your interrupt is to generate external 1 MHz clock by pin toggling look into PPI module and you can do it without core usage.

Children
No Data
Related