How can I get more time from the millis function?

Hi,

I created a function like Arduino's millis which returns the number of milliseconds elapsed since the micro started executing the current program. Look down:

The problem is that the timer overflows every 17 minutes or so. The Arduino millis function instead overflows every 50 days.
Is there a way to increase the overflow time of my millis function?

Thank you

Parents
  • Hi,

    If you are using a recent SDK and the app_timer implementation is app_timer2.c, then this is quite straight-forward, but you need to do a small change in the app_timer. The app_timer2 implementation use a 64 bit counter internally, so what you could do is to make get_now() non-static, and add that to  app_timer.h as shown in this diff:

  • Thanks for the answer but I don't understand what get_now returns. Milliseconds like millis?
    How do I get milliseconds?

  • I'm using a NINA-B3 module where the nRF52840 microcontroller is present.

  • No, it returns the internal counter value. Essentially the same as app_timer_cnt_get(), but where that returns a uint32_t with the 24 bit counter directly from the COUNTER register of the RTC, get_now() returns a uint64_t that is always increasing. It comes from the COUNTER though and has the same frequency, so you can calculate the time in milliseconds just as you did with app_timer_cnt_get(). I suggest you try both, and you should see that before the 24 bit counter wraps around, you will get the same result with both.

    So that would be something like this:

    Note that here I return an uint64_t. You can cast it to uint32_t and use that, but note that counting milliseconds in a uint32_t will mean that it will overflow after a bit more than 49 days.

  • Hi Einar Thorsrud,

    thanks for the answer.

    Currently I use the following code to make timings:

    Can I change my code and use the following?

    Specifically can I cast it to uint32_t?
    In this case there is no need to manage the overflow because the overflow time is very long (my device can stay on for max 2 days) right?

    Thanks for your time

  • Hi,

    Yes, if this never needs to run for as long as 49 days, then there is no problem to cast it to uint32_t.

Reply Children
No Data