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

Milliseconds since startup?

Hello everybody,

I've been digging through the forum and the documentation for hours now. But to be honest I don't understand the whole timer thing.

I need a simple way to count the milliseconds since the application/board startup.

My first attempt was to use the timer1 in CTC mode like so:

	NRF_TIMER1->MODE = TIMER_MODE_MODE_Timer; // Set the timer in Timer Mode
NRF_TIMER1->TASKS_CLEAR = 1; // clear the task first to be usable for later
NRF_TIMER1->PRESCALER = 7; //Set prescaler. Higher number gives slower timer. Prescaler = 0 gives 16MHz timer
NRF_TIMER1->BITMODE = TIMER_BITMODE_BITMODE_16Bit; //Set counter to 16 bit resolution
NRF_TIMER1->CC[0] = 125; //Set value for TIMER1 compare register 0 => 1ms interval

// Enable interrupt on Timer 1, both for CC[0] compare match events
NRF_TIMER1->INTENSET = (TIMER_INTENSET_COMPARE0_Enabled << TIMER_INTENSET_COMPARE0_Pos);
NVIC_EnableIRQ(TIMER1_IRQn);
NRF_TIMER1->TASKS_START = 1; // Start TIMER1

and the ISR:

void TIMER1_IRQHandler(void) {

if ((NRF_TIMER1->EVENTS_COMPARE[0] != 0) && ((NRF_TIMER1->INTENSET & TIMER_INTENSET_COMPARE0_Msk) != 0)) {
	NRF_TIMER1->EVENTS_COMPARE[0] = 0;

	runtime_ms++;
}

}

Which should increment the value of runtime_ms every millisecond, but somehow, this happens much slower.

What am I doing wrong? Or maybe there's a much simpler way I am missing here?

Greetings Matthias

Parents
  • Hi Matthias, when the interrupt fires, your timer still runs and the interrupt will fire again, when the 16 bit timer overflows and reaches 125 again. What you need, is to reset the timer, once your compare value of 125 was reached. I think this should be possible, by connecting the EVENT_COMPARE event with the TASK_CLR by setting the COMPARE0_CLEAR bit in the SHORTS register.

    cheers, Torsten

Reply
  • Hi Matthias, when the interrupt fires, your timer still runs and the interrupt will fire again, when the 16 bit timer overflows and reaches 125 again. What you need, is to reset the timer, once your compare value of 125 was reached. I think this should be possible, by connecting the EVENT_COMPARE event with the TASK_CLR by setting the COMPARE0_CLEAR bit in the SHORTS register.

    cheers, Torsten

Children
No Data
Related