Hi Community,
I am trying to program a timer to get a clean timebase of 50ms. But for some reason
A) the timer is little slower as expected. I have to add few counts to get close to my 50ms
B) I have a jitter of about 70us.
Do you have any idea what is wrong?
I measured the timer interval with an oscilloscope on a led pin.
Regards,
Arne
void TIMER1_IRQHandler(void) {
BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
NRF_TIMER1->EVENTS_COMPARE[0] = 0;
xSemaphoreGiveFromISR(counter_timebase_semaphore, &pxHigherPriorityTaskWoken);
{
static uint8_t led_state = 0;
set_led_state(led_state & 1);
led_state++;
}
portEND_SWITCHING_ISR(pxHigherPriorityTaskWoken);
}
void timer_init() {
NRF_TIMER1->TASKS_STOP = 1;
NRF_TIMER1->TASKS_CLEAR = 1;
NRF_TIMER1->INTENCLR = 0xffffffff;
NRF_TIMER1->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
NRF_TIMER1->BITMODE = (TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos);
NRF_TIMER1->SHORTS = (1 << TIMER_SHORTS_COMPARE0_CLEAR_Pos);
NRF_TIMER1->PRESCALER = 5; // Ensures 2 us resolution @ 16MHz
NRF_TIMER1->CC[0] = 250 + 1000 * 25; //the added 250 are due to Trial and error to get 50ms
NVIC_ClearPendingIRQ(TIMER1_IRQn);
NVIC_EnableIRQ(TIMER1_IRQn);
NRFX_IRQ_PRIORITY_SET(TIMER1_IRQn, 2);
NRF_TIMER1->TASKS_START = 1;
}