Hi everyone!
I have been developing an application for some time and everything has been running smoothly, until recently testing it and finding out some inaccuracies.
In the application, among others, I use a high priority (IRQ = 3) 16MHz timer to sample a flow sensor through TWI (IRQ = 2) every 10ms. The time of each sample along with the flow sensor measurements are saved in a .txt file
The timer timeout handler creates the sampling events for the flow sensor, and in the same time, to keep the time of the samples, I have added a variable, which is incremented 10 (ms) every time a timer timeout occurs.
static void flow_sens_handler(nrf_timer_event_t event_type, void *p_context)
{
if(event_type == NRF_TIMER_EVENT_COMPARE0)
{
//Add 10 ms to the clock counter after a period of the timer has run out
flow_timer_dur_ms = flow_timer_dur_ms + 10 -(((float)flow_timer_tick) / 16/1000);
//Count the number of ticks
flow_timer_tick = nrfx_timer_capture(&flow_sens_timer, NRF_TIMER_CC_CHANNEL1);
//Add the number of new ticks to the duration
flow_timer_dur_ms = flow_timer_dur_ms + (((float)flow_timer_tick) / 16/1000);
//Sample the flow sensor's flow and temperature registers
flow_sens_flow_get();
//Set the flag to display the incoming measurement to 1
incoming_meas = 1;
//Call LVGL Tick Handler
lv_tick_inc(10);
}
}
I am also calling the nrfx_timer_capture function to keep the exact moment of the flow sensor's sampling.
The problem is that I have noticed that the timestamp column of the flow sensor measurements suffers from slippage. More accurately, every 1000secs, the timer counts 1010secs, meaning that approximately 10ms are added every second. This has also been confirmed with an application timer that runs at the same time and saves accurate timestamps. The same has been noticed with a 2ms timer period.
It baffles me how a high frequency (16MHz) timer can perform poorer than a lfclk used for the application timer and I am sure something is going wrong, but I cannot find out what that is.
Do you have any idea what could be the case here?
Thank you,
Kostas