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

How to covert APP_TIMER ticks to MS

I am trying to record the time on nRF52840 with s140 softdevice. I used APP_TIMER to setup the TIMER and app_timer_cnt_get to get the count. And then uint32_t app_timer_cnt_diff_compute(uint32_t ticks_to, uint32_t ticks_from); to get the ticks for the time span. However, I am confused about how to get the actual time. There is not defined ticks to time macro in app_timer.h. I read some discussion about this and the solution is like

APP_TIMER_TICKS = ( MS * APP_TIMER_CLOCK_FREQ ) / ( ( PRESCALAR + 1 ) * 1000 )

But in SDK 14.0 the macro for time to ticks is like

#ifndef FREERTOS
#define APP_TIMER_TICKS(MS)                                \
            ((uint32_t)ROUNDED_DIV(                        \
            (MS) * (uint64_t)APP_TIMER_CLOCK_FREQ,         \
            1000 * (APP_TIMER_CONFIG_RTC_FREQUENCY + 1)))
#else
#include "FreeRTOSConfig.h"
#define APP_TIMER_TICKS(MS) (uint32_t)ROUNDED_DIV((MS)*configTICK_RATE_HZ,1000)
#endif

I don't know in SDK 14 the safe way to convert ticks to time anymore.

Parents
  • Hi mich_x!

    The macro in SDK v14. is the same as the one above. Take a look at this site for more information.

    You can also take a look at my answer in this case, as it should provide some useful information.

    However, in your case, lets make an example. If you use the macro in SDK v14, with a prescaler of 1 and 1000ms.

    APP_TIMER_TICKS = (1000 * 32768) / ((1 + 1) * 1000) = 16384 ticks. 
    

    This means that when you use the app_timer_cnt_get and the value is 16384, 1000ms has passed.

    Hope that cleared things up a bit. Let me know if something is unclear.
    Best regards,
    Joakim.

Reply
  • Hi mich_x!

    The macro in SDK v14. is the same as the one above. Take a look at this site for more information.

    You can also take a look at my answer in this case, as it should provide some useful information.

    However, in your case, lets make an example. If you use the macro in SDK v14, with a prescaler of 1 and 1000ms.

    APP_TIMER_TICKS = (1000 * 32768) / ((1 + 1) * 1000) = 16384 ticks. 
    

    This means that when you use the app_timer_cnt_get and the value is 16384, 1000ms has passed.

    Hope that cleared things up a bit. Let me know if something is unclear.
    Best regards,
    Joakim.

Children
Related