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

App Timer Tick - 1ms

Hello, I am developing with: custom nRF52810 board, nRF5 SDK 17, S112.

I'm using APP_TIMER for my main tick. I have the following code I use to initialize and start the timer.

    APP_TIMER_DEF(main_tick_id);
    
    uint32_t err_code = app_timer_init();
    APP_ERROR_CHECK(err_code);

    err_code = app_timer_create(&main_tick_id, APP_TIMER_MODE_REPEATED, timer_handler);
    APP_ERROR_CHECK(err_code);
    
    err_code = app_timer_start(main_tick_id, APP_TIMER_TICKS(1), NULL);
    APP_ERROR_CHECK(err_code);

From what I can tell, "1" in APP_TIMER_TICKS(1) is in ms. However, when I toggle a pin on my main loop waiting for the tick and observe it on an oscilloscope, it appears that the timer is in 1024Hz not 1kHz.

  1. Is my implementation of the 1ms App Timer not correct to generate a 1ms tick? 
  2. If my implementation is correct, is there a way for me to change my app timer to actually run at 1kHz not 1024Hz?

I'm curious as to why the parameter description says milliseconds when it's not exactly 1ms tick.

Thank you in advance.

Parents
  • Hi,

    1. Your implementation is correct, but due to the frequency that the timer is running at it is not possible to get the resolution you desire to get this exactly correct. The app_timer runs off the RTC peripheral, which uses the 32.768 kHz LFCLK as its clock source. The RTC can set the CC register to either 32 or 33 in order to get as close as possible to your desired timing, giving either 1024 Hz (32768/32) or ~993 Hz (32768/33).

    2. No, but you can use the TIMER peripheral if you require higher resolution. The timer peripheral runs off the 16 MHz high-frequency clock, allowing you to set timeouts in 62.5 ns steps. See Timer Example for details on how to use the timer peripheral.

    Best regards,
    Jørgen

Reply
  • Hi,

    1. Your implementation is correct, but due to the frequency that the timer is running at it is not possible to get the resolution you desire to get this exactly correct. The app_timer runs off the RTC peripheral, which uses the 32.768 kHz LFCLK as its clock source. The RTC can set the CC register to either 32 or 33 in order to get as close as possible to your desired timing, giving either 1024 Hz (32768/32) or ~993 Hz (32768/33).

    2. No, but you can use the TIMER peripheral if you require higher resolution. The timer peripheral runs off the 16 MHz high-frequency clock, allowing you to set timeouts in 62.5 ns steps. See Timer Example for details on how to use the timer peripheral.

    Best regards,
    Jørgen

Children
No Data
Related