Here are two simple functions to use when you want to use timestamps for time measurement.
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
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)
This is what i used for help and the requirement was:
if(millisPassed(lastMessage) > 15 * MINUTE)
Is there another solution for the OVERFLOW without using hardcoded number but the PRESCALER?
I made an error, the overflow should be #define OVERFLOW ((uint32_t)(0xFFFFFF/32.768)) because the app_timer_cnt_get() use the RTC who is a 24 bits counter.
You won't be able to measure millisecond with a prescaler of 32768
I had some more ploblems with #define OVERFLOW ((uint32_t)(0xFFFFFFFF/32.768)).
It should have been something like #define OVERFLOW ((uint32_t)((512*32768)/32.768)) because of PRESCALER which is 32768.
You are right. I will edit the post.