Close

STM8 Clock - RTC

K.C. LeeK.C. Lee wrote 11/20/2019 at 13:28 • 1 min read • Like

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.

Like

Discussions

K.C. Lee wrote 12/02/2019 at 23:19 point

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

Thomas wrote 11/21/2019 at 10:34 point

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

K.C. Lee wrote 11/21/2019 at 16:17 point

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

Thomas wrote 11/21/2019 at 18:43 point

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