Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 11:04 01 Aug 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Father Time

Author Message
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6283
Posted: 05:18am 14 Jul 2020
Copy link to clipboard 
Print this post

Before you decide on a suitable time reference, you need to decide what your priorities are.
How accurate does the time have to be?
Are you looking for short term stability or long term accuracy?

Back when I worked as a power station operator, we had to control the system frequency to keep the mains frequency within certain limits.
We had a mechanical chronometer and a gauge that compared system time with standard time.
The mechanical chronometer was kept in step with standard time using the time signals broadcast by radio stations such as WWV.

In the mornings, before the load peaked, I would deliberately run the system a bit fast so we  were 1-2 seconds in advance. That way, as load increased and frequency dropped, I didn’t have such a hard task keeping the system time within the required couple of seconds.

Most mains powered clocks used the system frequency as their time-base so any clock, once set, would be within 2 seconds of standard time forever, as long as the power remained on.

To the present time.

Computers have RTC modules that keep track of time while the PC is shut down. When the PC starts up, it reads the RTC and from then on, it uses its main oscillator to keep track of time while the PC is running. Most oscillators are in the range of 10ppm accuracy but can be up to 25ppm or more.

Now that most computers are internet connected, they can use a time service to adjust the PC time as required. Windows PCs used to check every week or more and would often be a few seconds out. I haven’t checked what the current Windows schedule is. I use a NTP program to manage PC time and, as with Linux PCs, the time is controlled much more often. Any PC using NTP should be with a fraction of a second within 10 minutes of starting up.

The original Maximite/Micromite family also have the facility to us an external RTC module which sets the ‘mite time on startup and then uses its internal main oscillator for subsequent time keeping.

Micromites that don’t use a crystal oscillator, have the OPTION CLOCKTRIM command to allow adjustment to the internal time-base.

Some micromites use a crystal oscillator and don’t have any adjustment available.
The crystal will probably be in the 10ppm range.

The STM series including the CMM2 have two oscillators – the main 8MHz and the RTC 32.768kHz.
While the 32.768kHz timebase cannot be altered, the OPTION RTC CALIBRATE command adds or removes pulses over a 32 second period to keep the RTC on time. 32768 x 32 = 1,048,576 pulse. By adjusting by one pulse, we can achieve a precision better the 1ppm or 0.0824 second per day or 30 seconds per year

How about the DS3231/3232 RTC modules.
Their precision is quoted as:
Accuracy ±2ppm from 0°C to +40°C
Accuracy ±3.5ppm from -40°C to +85°C
Like all crystals, they will also change over time.
The DS3231/3232 RTC have an aging register so you can trim the frequency to a very close precision but if you do use the aging registers, the nice temperature stability is lost.
At room temperature, a calibrated STM is better.



If you want long term accuracy you will need an external reference of some variety.

I have a lightning detector which needs accurate timekeeping for strike location. It uses a GPS derived timebase and has the advantage of being used as an NTP server. I synchronise all my RPi and PC systems to the server, keeping them within a few milliseconds.

I have a data acquisition system using a MX170 micromite. A PC connects to it regularly to retrieve the data so I have the PC program set the micromite clock daily. That keeps the micromite within a second or two which is close enough for the purpose.

I intend to change it to a STM F4 based system with local storage so a local clock with more precision is desired.

It is likely to be interrogated once a month so something better than the watch crystal as a timebase is preferred.

The same system I intend to use for the F4 can be used on a CMM2.

Using Internet time sources would require an Internet connection. Something I might do eventually.

My preferred source of time for full independence is through a GPS module.
Cheap, reliable but does require use of a serial port and they have a relatively high power requirement. Some modules have TCXOs for timekeeping when satellites are not in view.

If you intend to turn the CMM2 on each day and off each night, simply using a DS3232 module and setting the CMM2s RTC on power-up might suffice. You could use the Nunchuk port for this without interfering with its normal use.
I had a DS3231 connected this way and had the alarm output switching on a FET which in turn bypassed the power switch.



I could set the alarm to switch the CMM2 on at 7am each day. I don’t recommend Queen’s “We are the Champions” at full volume as an alarm clock if you want matrimonial harmony.

The system works well and will get used it to fire up a micromite once a week to monitor a battery powered system I have. I just have to change the FET switch arrangement to work at 12 volts without frying the DS3231.

You use software to reset the alarm which turns the power off.
The DS3231 turns the ‘mite on
Software reads the time from the DS3231 and sets the micromite time.
The main program runs as required.
When it has done its thing, the program turns itself off by resetting the alarm register in the DS3231.

I decide on a different approach for the CMM2.
Because of the way the STM boards uses dropped pulses to adjust the timebase, leaving the oscillator alone, we can combine the wide temperature stability of the DS3232 with the OPTION RTC CALIBRATE command to get a 1pps precision over a wide temperature range.  The best of both worlds.

I decided to use a DS32kHz module. It is the TCXO part of the DS3231/3232 without the aging registers and has the advantage of running its VCC of 5V
I connected VCC to 5V before the power switch so battery is used less often.
The Vbat is connected to the existing Vbat of the CMM2.
The 32768Hz output of the DS32kHz is connected to the 32kOSCin pin (PC14) via an 820k ohm resistor and 0.01uF capacitor. The existing 32kHz crystal was replaced with a 33k resistor. The resistor is needed to bias the voltage going to the OSC input. It is best placed on the board where the crystal was to minimise lead lengths.
The RC network reduces the oscillator level and reduces the power drain. Important when on battery. The values of 820k and 0.01 seem to give reliable operation on both powered state and standby.





Replace the xal with resistor

The DS32kHz is available as a 14pin Dip package but my supplier had no stock so I went with the 16pin SO package.

The first assembly used a carrier board so I could use it on a breadboard.
Out of the box, it gave a frequency better than 1ppm. Using OPTION RTC CALIBRATE was on no use in this case.

The second assembly used a chip soldered dead-bug style onto a piece of strip-board.
I had to keep the height down so it would fit under the motherboard when in the case.


Dead bug under the main board. Out of sight!

This module had a slightly worse precision and I was able to trim it using OPTION RTC CALIBRATE -1 to give a precision which is very hard for me to measure.
Over 14 hours, I detected a 1mS change compared to my GPS reference.
Accepting that the next 1mS might be about to appear, I can say that the accuracy is better than 4mS per day or one second in 8 months.
That will do me.
Unfortunately, the DS32kHz will age over that time so the tremendous accuracy will only be short lived.

I like the DS32kHz solution because it is “fit and forget”. There is no change to the MMBasic firmware and no software to run apart for the occasional RTC CALIBRATE.

The next step up would be one of the Ublox GPS modules. Get one that has a TCXO backup oscillator and configure its output pulses to 32768Hz
Then you will have something that keeps time.
Until the next leap second…
VK7JH
MMedit
 
OA47

Guru

Joined: 11/04/2012
Location: Australia
Posts: 986
Posted: 05:51am 14 Jul 2020
Copy link to clipboard 
Print this post

Great info TJ
OA47
 
CaptainBoing

Guru

Joined: 07/09/2016
Location: United Kingdom
Posts: 2170
Posted: 07:28am 14 Jul 2020
Copy link to clipboard 
Print this post

very good post.

I notice you say
  Quote  ... using a MX170 ... so I ... set the micromite clock daily. That keeps the micromite within a second or two which is close enough for the purpose.

Wow! I just can't get anywhere near that on the internal clock. The timing of your post is really good for me as I have been playing with this a bit lately too (and had a thing about it for years). I have a 44pin 170 on my desk now with a 3231 RTC, just sat doing not much.

It needs a CLOCKTRIM of -7 to get anywhere near accuracy but is still worse than a "once a day" kind of accuracy.

> option clocktrim -7
...
>  ?time$:rtc gettime:?time$,date$
08:29:18
08:29:19        10-07-2020
> ?time$:rtc gettime:?time$,date$
09:29:20
09:29:07        12-07-2020


like about 6 or 7 seconds a day. Going to -8 just goes too slow, I could do with about -7.3  

I tend to set my clocks via RTC about every hour because of this.

Obviously it varies from chip to chip but they always seem to be in the same ball-park.

This isn't a moan (or even a problem) just observation - I learned 40 years ago that CPUs are seldom as good at keeping time as you'd hope  

Incidentally, years ago (before I discovered MMs) I used a PIC16F877 to build an aquarium sump controller and sneaked a 50Hz clock off the low voltage AC side for my "RTC" - it was *really* accurate - a few seconds a month if I recall properly.
 
Sasquatch

Guru

Joined: 08/05/2020
Location: United States
Posts: 377
Posted: 01:40pm 14 Jul 2020
Copy link to clipboard 
Print this post

Nice Post with lots of good info.  

I agree GPS timing has ZERO long term drift and is likely the way to go if you need high accuracy and are using the micromite in a location where acquiring a GPS signal is possible.
-Carl
 
Paul_L
Guru

Joined: 03/03/2016
Location: United States
Posts: 769
Posted: 08:03pm 14 Jul 2020
Copy link to clipboard 
Print this post

If a CPU clock oscillator does not use a crystal in an oven it is likely to be an unreliable source of timing control.

Use of the power line frequency will be very dependable in most developed countries of the world. In the states there will be upwards of 3000 very large generators driving the electrical grid in parallel at any given instant. Those generators not only have to be synchronized in frequency but also in phase angle ... that is all the armatures have to be pointing north at the same time. If they're not synchronized then one will try to drive another as a motor which is not good. The power line frequency will therefore be a very good source of timing information second only to a GPS signal if available.

Paul in NY
Edited 2020-07-15 06:04 by Paul_L
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5089
Posted: 08:15pm 14 Jul 2020
Copy link to clipboard 
Print this post

I have a cheap Casio around my wrist, I have it for 6 years now. It is still within 1 minute accurate. What is wrong with a watch crystal ? If sufficient care is given to the oscillator design this should be sufficient for years.

However: I admit I think replacing the 32kHz resonator with something more accurate is a great solution, and completely transparant.

Volhout
PicomiteVGA PETSCII ROBOTS
 
Andrew_G
Guru

Joined: 18/10/2016
Location: Australia
Posts: 871
Posted: 10:53pm 14 Jul 2020
Copy link to clipboard 
Print this post

Hi Jim,
A good discussion!
I have a network of about a dozen MM170 devices (mostly temperature via DS18B20s), including a weather station "up a pole". All are equipped with HC-12.
The latter has a GPS receiver which every two hours broadcasts the time and date.
(I use my own NMEA-like sentence with a checksum.)
Every device picks up that broadcast and updates its TIME$ and DATE$ (obviously I could alter the frequency of the broadcast but 2hr seems OK).
If a station restarts, or joins in, it sends a request sentence - the GPS sends an immediate broadcast and all stations update their times and dates. Request sentences are repeated until a valid time and date message is received.

For daylight saving I just send a message (DST On/Off) to the weather station which calculates and broadcasts the new date and time.
If any stations can't reliably "see" the weather station I have a couple of stations within the house that re-broadcast the time/date messages - the duplication and slight time difference are not critical to my use.

It works for me.

Cheers,

Andrew
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025