I have upgrade the code inside the clock with a numerically-controlled oscillator (NCO) with a 24-bit phase accumulator. The NCO allows me to finely compensate against the crystal's tolerance. It is a step up in the resolution from tweaking the 16-bit timer divider in my timer project. While the timer divider could be extended in firmware using overflow and timer compare, the NCO approach actually is much simpler.
I have modified my RTC code which was running from a 10Hz interrupt. The timer now generates a 20Hz (+/- clock tolerance) interrupt with the NCO code implementing the divide by (2 + tolerance tweak).
There will be additional jitters (of 50ms) of this approach, but it is not too noticeable.
// 20Hz IRQ
// tick comes from Direct Digital Synthesis
DDS_Accum += DDS_PhaseInc;
if (DDS_Accum & DDS_CARRY)
{
// 10Hz RTC code here
}
DDS_Accum &= DDS_MASK;
The clock has been running close to 32 hours and so far it is within 100ms (or so) to the internet time at https://time.is/ So likely it would be within a few seconds for a month. Just 3 lines of code. Why pay for an extra RTC chip?
Meanwhile my Win10 clock has drifted to 1.6 seconds in the same period. (Win10 would resync the clock once in a while.)
The clock is limited by the drift and temperature coefficient of the cheap 12MHz crystal. It is sitting on my computer desk which is unlikely to get more than 10C of temperature change.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.
2 more days to the 1 month mark, the clock is running barely less than 1 second slower. Not too bad considering that it is still running on the same calibration and the error includes both tolerances and minor temperature drifts. STM8 is unregulated "5V" from host powered cheap USB hub.
https://blog.dan.drown.org/rtc-comparison/
There are no temperature compensation like the better RTC chips, but my own tweaks are pretty close to properly calibrated ones.
The temperature drifts in winter time isn't too bad as my place has heating. There are some cooler nights and sometimes warm exhaust from PC running full load overnight. It'll certainly get worse in summer time.
-----------------------
update: Clock is off by 1 sec after 4 weeks 6 days.
Are you sure? yes | no
That's really good. 👍 I'll certainly use your design when I make more clocks as this avoids implementing NTP or GPS.
Are you sure? yes | no
I have been calibrating using a binary search for the tweak value by checking if the clock is running fast/slower just comparing when the seconds changes. Probably done 6-7 such cycles.
My latest calibration has been running for 6 days 18 hours, I still cannot even tell the time difference between internet time vs the clock. The seconds still change roughly same time as the internet time. At this rate, it might already be less than +/-1 second per month.
Not bad for some old 12MHz crystals that I have been using for USB projects. They have really horrible tolerances.
Are you sure? yes | no
Why not add a heater, a cheap NTC, and some insulation to create a temperature controlled RTC? Keeping such a thing at 40°C shouldn't consume much energy.
Are you sure? yes | no
I think I can live with missing a few seconds a month. The clock sit next to my computer which rarely gets below 25C or above 35C which is the "flat" region of common AT cut crystals. http://www.wenzel.com/wp-content/uploads/xtalcrv.gif (The accuracy of the angle affects the tempco)
Some hackers managed to get their TI Chorons watch to +/- 5sec by calibrating and compensating with a temperature calibration curve. Sadly the STM8 doesn't have temperature inside the ADC.
TCXO is another option these days thanks to mobile phones. The NCO can deal with oddball frequency.
Are you sure? yes | no
I didn't know about TCXOs! The stability is really astounding. e.g. https://support.epson.biz/td/api/doc_check.php?dl=brief_TG5032CGN&lang=en
Are you sure? yes | no