RSS
Sort by » date activity comments

1 blog post tagged with millis

10
comments
by schef

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

1

Tags