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

Timer 1 has no Different resolution

We're currently running into a lot of bugs using timer1(and 2) especially with the softdevice enabled. Every so often it glitches, and after further inspection of the code, we notice that regardless of NRF_TIMER1->BITMODE = x; the bitmode doesn't change.

we use the following code.

void TIMER1_IRQHandler(){
	NRF_TIMER1->EVENTS_COMPARE[0] = 0;
	NRF_TIMER1->CC[0] += 125; ///add an additional 125, so that it interrupts on the next 125 ticks	
	NRF_TIMER1->CC[0] = 75125;

	if (NRF_TIMER1->CC[0] > 0xffff)
	{
		while(1);
	}
	///it should also roll over after reaching the 4billion mark in 9 hours
	main_interrupt();
}

It will NEVER reach the infinite whileloop. and we initialize it like so

	NRF_TIMER1->POWER = 1;
	/// we want this function to be called every 1 ms
	NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer << TIMER_MODE_MODE_Pos;
	///8 bit timer
	NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_08Bit << TIMER_BITMODE_BITMODE_Pos;
	///16Mhz / 2 ^ 7 = 125000,
	NRF_TIMER1->PRESCALER = 7;
	///so to generate a 1Khz interrupt, we set compare register to 125
	NRF_TIMER1->CC[0] = 125;
	NRF_TIMER1->INTENSET = TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos;
	///set as the highest priority
	NVIC_SetPriority(TIMER1_IRQn, 0);
	NVIC_EnableIRQ(TIMER1_IRQn);
	nrf_delay_us(100);

If you notice that we even tried setting the bitmode to 8bit, and it still will count up to 16bit value before rolling over. We NEED a 32bit timer. Our current hardware is the nrf51822 QFAC A1

Parents
  • Well for a start TIMER1 and TIMER2 are 8 or 16 bit timers, they don't have a 24 or 32 bit mode. So I rather suspect

    NRF_TIMER1->CC[0]=75125 
    

    is actually setting it to 9589 (75125-65536) and so you're never going to get into your while loop.

    You can test that pretty easily by reading back CC[0] after you set it and see what it is.

    By the way, If you're using the softdevice then this

    NVIC_SetPriority(TIMER1_IRQn, 0);
    

    is using an interrupt priority you can't use. Not something causing your problem, but something which will cause you a problem in the softdevice eventually.

  • .. and a 20 second test with the debugger shows that indeed setting TIMER1->CC[0] to 75125 does indeed only set the lower 16 bits.

Reply Children
No Data
Related