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

ticks to ms calculation

app_timer.h contains a useful macro for getting from milliseconds to a number of timer ticks, given a prescaler value with which the timer was started:

#define APP_TIMER_TICKS(MS, PRESCALER)\
        ((uint32_t)ROUNDED_DIV((MS) * (uint64_t)APP_TIMER_CLOCK_FREQ, ((PRESCALER) + 1) * 1000))

Supposing I know a certain number of ticks have elapsed since a certain time, and I want to know how many milliseconds that is. Is there a macro for that? What would one look like? I have something here but it looks like it's overflowing long before it should.

Parents
  • well just re-arrange the formula, ignoring the ROUNDED_DIV() for simplicity

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

    so

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

    I'm sure you can turn that into a define, not that it really helps you that much as the number of ticks will be in a variable so the compiler can't evaluate it and compile it out into a constant which is what happens most of the time with the current macro as usually everything on the right hand side is a compile-time constant.

Reply
  • well just re-arrange the formula, ignoring the ROUNDED_DIV() for simplicity

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

    so

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

    I'm sure you can turn that into a define, not that it really helps you that much as the number of ticks will be in a variable so the compiler can't evaluate it and compile it out into a constant which is what happens most of the time with the current macro as usually everything on the right hand side is a compile-time constant.

Children
No Data
Related