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

RTC1 problem of accuracy

Hello,

I'm trying to use the APP_TIMER API to have tempo, but I've noticed a problem of accuracy when using the RTC1. I use 0 as prescaler, so the RTC1 runs at 32768Hz. But when I want a tempo of 32768 ticks (in order to have 1 second of tempo), I've got only 936ms (measured with an oscilloscope).

Here is my code :

#define APP_TIMER_PRESCALER              0                                        
#define APP_TIMER_MAX_TIMERS             9                                        
#define APP_TIMER_OP_QUEUE_SIZE          4                                                                        

static app_timer_id_t                    m_uneSeconde_timer_id;   



void initialisationLed(void)
{
	nrf_gpio_cfg_output(21);
	nrf_gpio_pin_clear(21);
}


static void forToggle_handler(void * p_context)
{
    nrf_gpio_pin_toggle(21);
	UNUSED_PARAMETER(p_context);
}


static void timers_init(void)
{
    uint32_t err_code;

    // Initialize timer module.
    APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, false);

    // Create timers.
    err_code = app_timer_create(&m_uneSeconde_timer_id,
                                APP_TIMER_MODE_REPEATED,
                                forToggle_handler);
    APP_ERROR_CHECK(err_code);
		NRF_CLOCK->TASKS_LFCLKSTART = 1;
	
}


static void application_timers_start(void)
{
    uint32_t err_code;

    // Start application timers.
    err_code = app_timer_start(m_uneSeconde_timer_id, 32767, NULL);
    APP_ERROR_CHECK(err_code);
}




int main(void)
{
    // Initialize.
    timers_init();
		initialisationLed();

    // Start execution.
    application_timers_start();

    // Enter main loop.
    for (;;)
    {
				__WFI();
    }
}

Is there an erro in my code, or do I have to put about 35000 ticks to have a 1.000s tempo ?

Parents
  • Interesting .... Your code looks correct, RTC1 uses LFCLK, which LFCLK have you selected? I see that the code starts the LFCLK but cant see which one. I am not sure if APP_TIMER selects any clock by default (and i do not have the code handy to verify) If you (or app_timer) have selected internal RC LFCLK, then the error seems to be inline with this clock jitter. Try to select the XTAL LFCLK explicitly and see if there is any improvement

Reply
  • Interesting .... Your code looks correct, RTC1 uses LFCLK, which LFCLK have you selected? I see that the code starts the LFCLK but cant see which one. I am not sure if APP_TIMER selects any clock by default (and i do not have the code handy to verify) If you (or app_timer) have selected internal RC LFCLK, then the error seems to be inline with this clock jitter. Try to select the XTAL LFCLK explicitly and see if there is any improvement

Children
No Data
Related