Using millis() like in Arduino.

Here are two simple functions to use when you want to use timestamps for time measurement.

uint32_t millis(void)
{
  return(app_timer_cnt_get() / 32.768);
}

and for comperison of current with given timestamp with rollover correction (thanks to Nicolas Brunner for the correction):

#define OVERFLOW ((uint32_t)(0xFFFFFFFF/32.768))

uint32_t compareMillis(uint32_t previousMillis, uint32_t currentMillis)
{
  if(currentMillis < previousMillis) return(currentMillis + OVERFLOW + 1 - previousMillis);
  return(currentMillis - previousMillis);
}

You can use it like:

uint32_t myTimeStamp = millis();
//... some code here
if(compareMillis(myTimeStamp, millis()) > 2000)
{
  printf("2 seconds have passed\n");
}

Here are unit tests https://github.com/schef/nrf_millis

Parents
  • Aha..i had problems yesterday thank God i connect the buzzer to beep on every startup and it was beeping every 8 minutes :)

    The use-case was:

    #define MINUTE 60*1000
    
    uint32 millisPassed(uint32_t localMillis)
    {
        return(compareMillis(localMillis, millis()));
    }
    

    This is what i used for help and the requirement was:

    if(millisPassed(lastMessage) > 15 * MINUTE)
    {
        sd_nvic_SystemReset();
    }
    

    Is there another solution for the OVERFLOW without using hardcoded number but the PRESCALER?

Comment
  • Aha..i had problems yesterday thank God i connect the buzzer to beep on every startup and it was beeping every 8 minutes :)

    The use-case was:

    #define MINUTE 60*1000
    
    uint32 millisPassed(uint32_t localMillis)
    {
        return(compareMillis(localMillis, millis()));
    }
    

    This is what i used for help and the requirement was:

    if(millisPassed(lastMessage) > 15 * MINUTE)
    {
        sd_nvic_SystemReset();
    }
    

    Is there another solution for the OVERFLOW without using hardcoded number but the PRESCALER?

Children
No Data