How to get accurate 1-second clock tick in RAK4631

I am programming the RAK4631 via Arduino IDE.
I need a fairly accurate 1-second clock. When measuring the NRF_RTC1->COUNTER value, I can see that it runs slightly faster than 1 second: in 60 seconds, it is almost 2 seconds fast. This equates to a gain of > 40 minutes per day!

How can I correct this, or use another counter that is more accurate?
I don’t think millis() would work as I have some delay() functions in the code, which would pause the CPU, thus rendering millis() inaccurate, correct?

Thank you for any advice!

Please disregard my question above. I had not realized that the NRF_RTC1->COUNTER value is NOT milliseconds! It is instead the 32768hz clock divided by the NRF_RTC1->PRESCALER + 1, which by default is set to 31. So 32768/(31 + 1) = 1024. Therefore, if we divide NRF_RTC1->COUNTER by 1024, we get accurate seconds.

Refer to Adafruit_nRF52_Arduino/nrf51.h at 527e62d5f480c3f7f529bba06d88cd165de14626 · adafruit/Adafruit_nRF52_Arduino · GitHub

Sorry for the stupid question.

2 Likes