In case you like to fiddle with the more modern aspects of C++, you might like my HiResClock library (
https://github.com/luni64/HiResClock). It implements a std::chrono compliant c++ clock (see e.g. here
https://en.cppreference.com/w/cpp/chrono) which uses F_CPU (600MHz) as time base. Thus it has a resolution of 1.666ns and measures time in nanoseconds since 1970-01-01. By default it syncs to the RTC at start. Here an example how you generate a RTC synced ns timestamp with this clock:
Code:
#include "HiResClock.h"
using namespace std::chrono;
void setup()
{
HiResClock::begin(); // start the clock and sync to rtc (works with and without battery)
}
void loop()
{
// long form:
auto now = HiResClock::now(); // get current timepoint
auto dur = now.time_since_epoch(); // calculate the chrono::duration since 1970-01-01 (in some system units, here 1/600Mhz)
auto dur_in_ns = duration_cast<nanoseconds>(dur); // cast to a chrono::duration in units of nanoseconds (or milliseconds, or hours...)
uint64_t ns = dur_in_ns.count(); // read out the actual nanoseconds from the duration
// short form:
// uint64_t ns = duration_cast<nanoseconds>(HiResClock::now().time_since_epoch()).count();
Serial.print(ns / 1E9, 9); // print the timestamp in the desired form (e.g. 1687969637.753705324)
//--- quick check ---
Serial.print(" - ");
time_t s = (time_t)(ns / 1E9); // transform to time_t...
Serial.print(ctime(&s)); // and pretty print corresponding time
delay(250); // repeat every 250ms
}
Prints:
Code:
1687981600.000000000 - Wed Jun 28 19:46:40 2023
1687981600.250008344 - Wed Jun 28 19:46:40 2023
1687981600.500017404 - Wed Jun 28 19:46:40 2023
1687981600.750026226 - Wed Jun 28 19:46:40 2023
1687981601.000035285 - Wed Jun 28 19:46:41 2023
1687981601.250044345 - Wed Jun 28 19:46:41 2023
1687981601.500053167 - Wed Jun 28 19:46:41 2023
1687981601.750062227 - Wed Jun 28 19:46:41 2023
1687981602.000071287 - Wed Jun 28 19:46:42 2023
1687981602.250080585 - Wed Jun 28 19:46:42 2023
1687981602.500089406 - Wed Jun 28 19:46:42 2023
1687981602.750098228 - Wed Jun 28 19:46:42 2023
1687981603.000107288 - Wed Jun 28 19:46:43 2023
1687981603.250116348 - Wed Jun 28 19:46:43 2023
1687981603.500125408 - Wed Jun 28 19:46:43 2023
1687981603.750134229 - Wed Jun 28 19:46:43 2023
The integer part of the calculated values corresponds to a normal time_t value. Resolution is 1.66ns. There are a few more examples how to use it in the lib repository and here
https://github.com/TeensyUser/doc/wiki/Durations-Timepoints-and-Clocks and here:
https://github.com/TeensyUser/doc/wiki/Implementing-a-high-resolution-Teensy-clock