timestamp problem

Hardware: The chip is 52832, the low-frequency crystal frequency is 32.768kHz, and the crystal precision is ±20ppm  

Our application is to make a second level timestamp (Unix timestamp)function, it can synchronize the time through the phone.  I start a 1-second app timer and increment the timestamp by 1s as every second time out.  The code is as follows  

 

Test procedure: Synchronize the time at a point where the phone time is consistent with the firmware timestamp.  After running 9 hours, the firmware timestamp is read from the mobile phone. The time when the firmware is read is displayed on the log. The firmware time is 11s faster than that of the mobile phone.We want to know what causes this.

  • Because your calculations are wrong. On the last line you add an integer number of milliseconds that have passed since last time. To compensate for rounding errors, the remain_tick variable contains a remainder that is added the next time. But it's an incorrect remainder. If, for example, diff_tick contains ~1.001007 seconds, 1001 ms will be added on the last time, but ~1007 microseconds will be saved and added the next time, resulting in exactly a one millisecond extra drift.

    Using an app timer that performs cpu work like this every second is not really battery efficient anyway. Instead, whenever you want to retrieve the time, you can have a function that gets the time directly from the COUNTER register of the RTC, correctly adjusted with how many RTC overflows have happened, plus the synchronisation value that converts it to a Unix timestamp.

    Also make sure you configure the project to actually use the LFXO and not the internal oscillator.

  • If, for example, diff_tick contains ~1.001007 seconds, 1001 ms will be added on the last time,1007 microseconds will be saved and added the next time.

    This is the result of RTC COUNTER.That means it actually ran 1.001007s,If discard it, that will lose 1007us

  • Hi 

    I have the same problem, and it sounds like a great tip.

    1.Can you provide simple sample code? For RTC overflow calibration problem

    2.How to detect the accuracy of external LFXO crystal oscillator?

    Best wishes

Related