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