keskiviikko 28. maaliskuuta 2018

Keeping time 3: Accuracy


To keep clock in time, there needs to be a timing source. In case of embedded systems, the most typical is 32768 Hz clock crystal (why 32768? For any programmer worth their pay, this is obvious, but that happens to be nice power of 2; 2^15 to be exact. And due to how digital systems work, powers of two use least amount of energy, giving optimal battery life).

The datasheet of the clock chip you're using will specify the required frequency and load capacitance needed for clock functionality. If the frequency is 32768 Hz, you're almost there; there are many different types of crystals to choose from. Then you'll only need to pick one with correct load capacitance and fine tune the circuit.

Furthermore, datasheet of the RTC chip will specify a circuit (something like one below) that should be used and the load capacitance required for proper function. Datasheets of clock crystals specify their load capacitance. So just pick ones that match, right?

Unfortunately that isn't that easy. In some cases this works directly (typically with chips that are specifically tuned for one type of crystal), sometimes they work but are inaccurate, and sometimes oscillator completely fails to oscillate. And only way to find out is to test the circuit in practice.

The arrangement below is just about the most complex one you might encounter, as specified by datasheet of a Microchip PIC24 MCU. The values for resistors and capacitors can be approximated (or even guessed), but for best results, you'll have to test them in practice.


Typically resistor R110 is not needed, and R67 can work as 0 ohm resistor (or short) - if in doubt, you may use above 100k as first guess (try 10k or 1k or even short if oscillator doesn't start). For the capacitors - C109 and C110 here - a good starting value might be around 10pF for both. These are the most critical passives, as they set the accuracy of your clock (and, with some chip/crystal combinations, they're not needed at all as chip and crystal just are tuned well enough already.)

Unfortunately this cannot be tested on bread- or prototyping board; it has to be the circuit board you are going to use in production. This is because the capacitances in circuit are so small; different board layout, with its stray capacitances will be enough to throw testing way off. But worry not; just make room for the components, make educated initial guess and start testing. (just be aware that even if you find out that you don't need some of the components there - resistors most likely - the capacitance of circuit will be changed just by removing the pads for them. Re-test with every change of board layout!)

Set time on your chip (careful! you'll need very good reference time here for this to work. Fortunately web is full of those these days). Let it run and come back few days later. Check how badly clocks had drifted, and correct circuit accordingly. If it runs fast, make capacitors bigger (10pF -> 12pF or 15pF). If it runs slow, make then smaller (10pF -> 8pF or 6pF). Repeat and adapt, until you get close enough.

The problem is that unless you have very, very good way of setting and getting time out of the chip, this takes time. Typical crystals - and thus your target accuracy - are 10 or 20 ppm range; in practice, this means 12 seconds of drift per week - or less than 2 seconds per day. Accurately timing this kind of short time periods with manual methods is practically impossible. Thus, when you get close enough, you'll need to let system run for long periods before checking the results.

This will take some time. Even many, many weeks of trial and error before you zero on correct capacitances. Unfortunately, there really isn't much better way to do this, especially for your first attempt.

Good hunting.



Ei kommentteja:

Lähetä kommentti