PDA

View Full Version : Cyrstal Compensation Code?



SRLM
01-22-2009, 11:02 AM
Well, I've taken a step back and decided to make a clock on the propeller. I've got everything working except for my crystal compensation code. Basically, what I need to do is to compensate for long term crystal deviations. According to the PE Lab documents, the crystal can give the propeller up to a 3 second loss per day. That turns out to be about 18 minutes per year: time lost! What I would like to do is have the user program in the start time (after power up) with a known good time, and four months later (or so) come back and program the known good time in again. From these two data points, the propeller should be able to figure out what it's error is and compensate for it in future time keeping applications.

Now, if this were a purely mathematical problem, it would be simple: calculate the ratio as follows:



Seconds Off Clock Cycles off
---------------- = --------------------
Total Seconds Total Clock Cycles




Where clock cycles off is the unknown, and from there calculate clock cycles off per second (to compensate with) by dividing 'Clock Cycles off' by 'Total Seconds'.

The challenge is to do this in integer math without losing precision. Any ideas on how to actually implement this? I'll continue to work on it, but sometimes it helps to get outside opinion.

mirror
01-22-2009, 11:21 AM
1) If you're running an 80MHz crystal then you'll get 80M increments of CNT per second.

2) Crystal error is normally quoted in parts per million. So you'll have about 80 of those errors per second.

3) Find out what your error is and add it in every second. eg:

waitcnt(clock += CLKFREQ + compensation)

That's it! You really don't need to do any extra maths for the method you're proposing.

If you want to take it further, then get a temperature sensor, and do a temperature based compensation. eg:

waitcnt(clock += CLKFREQ + compensation(degrees))

The magic is in getting the right number for compensation.

If you're losing 3 seconds per day, then thats 3 in 86400 which is 34.72 ppm. So, you want each second to count for a little bit longer. You would set the compensation value to 34.72 * 80 = 2778.

---

Post Edit:

I just realised that I didn't really answer your question. However, 4 months is almost 10 million seconds during which you're expecting about 360 seconds of error (3 seconds per day). A naive calculation is as follows:


compensation = (error * clkfreq) / expected-number-of-seconds


however: (error * clkfreq) can overflow 32 bits.
We know that we can fit about 53 seconds worth of 80Mhz into 32 bits, so that leads to the following solution:



pub calc_compensation(error, expected) : comp | div, clk
clk := clkfreq
div := $7fff_ffff / clk
repeat while div < abs(error) ' Scale the numerator and denominator until the compensation calculation will work.
div *= 2
expected /= 2
clk /= 2
comp := (error * clk) / expected



This is completely untested! But I think it should work, it doesn't require floating point, and it should give the highest possible accuracy for the effort.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


Post Edited (mirror) : 1/22/2009 5:08:37 AM GMT

SRLM
01-22-2009, 11:37 AM
I'm using the standard Propeller crystal of 5 MHz with an internal clock speed of 80 MHz. I'm going to have the crystal kept at a constant temperature with a PI loop, a temperature sensor, and a resistor for heat. So, the only thing that I need to compensate for is the +-30 PPM that the crystal is rated for. The problem lies in the fact that that is the worst case error (right?), so the real value could be +10 PPM, -3.25PPM, or +21PPM. I don't think I can just add the maximum error since that might end up giving me a new error to correct for.

As a side note, the clock will be programmed by a human, so time scales should be pretty large in order to compensate for human reaction times (when it's a reaction time of 1 second to correct for 3 seconds error vs. 120 seconds error, the latter will produce less overall error). I tried outputting a square wave, and then adjust the correction based on an oscilloscope reading, but my scope (the parallax USB) said that it was a perfect 1 second, so that didn't work. I've also thought about simply having a pot and RC time it, and have the user adjust the knob to speed up or slow down the clock. The problem with this method is that it takes alot of time to be able to tell the effect of your adjustment.

mirror
01-22-2009, 11:51 AM
I edited my previous post.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

mctrivia
01-22-2009, 12:48 PM
i don't think there is a completely accurate way to compenste for crystal error. Most cheap(older) alarm clocks just count the number of cycles on the ac power. Is 9 minutes per half year to much? don't forget they will half to change the time twice a year anyways for DST

dMajo
01-22-2009, 03:46 PM
SRLM said...
I'm using the standard Propeller crystal of 5 MHz with an internal clock speed of 80 MHz. I'm going to have the crystal kept at a constant temperature with a PI loop, a temperature sensor, and a resistor for heat. So, the only thing that I need to compensate for is the +-30 PPM that the crystal is rated for. The problem lies in the fact that that is the worst case error (right?), so the real value could be +10 PPM, -3.25PPM, or +21PPM. I don't think I can just add the maximum error since that might end up giving me a new error to correct for.


As per my knowledge this is true if you change the crystal. The errors on crystals is the same as for the resistors: there is a base frequency error due to the manufacturing process (cut to the quarz and package assembly) which should be in the tollerance of xx ppm (but is constant on the same crystal) and there is the termal drift xx ppm/°C.


I also agree with mctrivia. You can compensate in the long period counting the "AC mains" cycles. Here the frequency error is variable because the companies (power producers) compensate for it and taking a long term measure is accurate.
You can do this with 2 diodes and a resistor·taking the source on the secondary of you power supply transformer (or in a safety way also directly on the mains as long as this is a phase-neutral system: in this situation you will put three resistors serial to be tolerant also on component possible failures)


Edit: Am I wrong? Please correct me

Post Edited (dMajo) : 1/22/2009 8:51:51 AM GMT

mirror
01-22-2009, 04:24 PM
There are semiconductors available from manufacturers that have registers to achieve what SRLM wants. I think the idea of taking a long term average (4 months as mentioned) and then using that as a basis for error correction could well produce quite an accurate result.

It is possible that every couple of months you could check the drift and incrementally adjust the correction factor.

One requirment would be that the correction is measured and applied in the environment in which the product will be running - otherwise you could get an indoor vs outdoor (office vs factory) conditions error.

I think your result will be better than expected because you're at least making an attempt to control the temperature at which the crystal is running.

Please let us know in a couple of months how your compensation is looking. Unfortunately, I don't think there's any practical way to do accelerated testing.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

virtuPIC
01-22-2009, 04:40 PM
Side question: Where do you get your reference clock? I mean, you have a quartz with an unknown error of some 30 ppm which doesn't change over time due to the thermostate. How do you make your unknown error a known error?

I would have an idea what to use here in Germany. But I'm curious...

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Airspace V - international hangar flying!
www.airspace-v.com/ggadgets (http://www.airspace-v.com/ggadgets) for tools & toys

mirror
01-22-2009, 05:07 PM
www.google.com -> "what is the time"

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

dMajo
01-22-2009, 09:08 PM
virtuPIC said...
I would have an idea what to use here in Germany. But I'm curious...


If I guess correctly DCF77 (also from Italy and most of west-central Europe)


This receivers works also for MSF(UK), WWVB(USA), JJY(JPN) and others:

kwinn
01-25-2009, 03:46 AM
SLRM, the simplest way to do this is to use a spreadsheet to calculate the error and adjust the number of cycles required for one second. For example if your time is low by 20ppm after 4 months then subtract 20*80 (assuming the prop is running at 80Mhz) from 80,000,000.

Bean
01-25-2009, 03:57 AM
You should be able to get a 10MHz TCXO pretty cheap. That will have about 10ppM error.

At my work we make TCXOs that are less than 1ppM. Some are less than 0.1ppM from -40C to +85C.

We also make OCXOs that move less than 10ppB (parts per billion) from -20C to +70C.

Pretty cool stuff, I work on them all the time.

P.S. TCXO = Temperature compensated xtal oscillator; OCXO = Oven compensated xtal oscillator.

www.greenrayindustries.com (http://www.greenrayindustries.com)

Bean.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
·The next time you need a hero don't look up in the sky...Look in the mirror.


·

SRLM
01-25-2009, 05:09 AM
Okay, I've finally got a chance to work some more on this, so I'll do each topic by itself.

[quote=mctrivia]don't forget they will half to change the time twice a year anyways for DST
Some states don't have DST (like Arizona), and I don't think any foreign countries do either, although I'm guessing here. I don't plan on using it outside the US, but I'll be posting it in the completed projects forum, so I don't want to make assumptions.


dMajo with reference to mctrivia said...
You can compensate in the long period counting the "AC mains" cycles.

I briefly considered this, but I'm a beggining level EE, and don't want to kill myself or anybody else with a bit of bad wiring...


mirror said...
It is possible that every couple of months you could check the drift and incrementally adjust the correction factor.

That's what I'm leaning towards. A pair of buttons (+ and -) that increment and decrement the clock correction factor. Not as elegant as an internal calculation, but a whole lot simpler and easier to implement.


virtuPIC said...
Where do you get your reference clock? ... How do you make your unknown error a known error?

The user (human) will know the correct time from whatever source they have. Time.gov is a good one.


kwin said...
SLRM, the simplest way to do this is to use a spreadsheet to calculate the error and adjust the number of cycles required for one second.

Hmmm... You've got me thinking in a new direction. With a crystal error of 30 ppm, that's only about 60 entries into a table. Doesn't sound that difficult.


Bean said...
You should be able to get a 10MHz TCXO pretty cheap. That will have about 10ppM error.

Yeah, I've looked at those 'fancy' crystals, but they are still relatively expensive. If the price can come down to about $5 or less per piece, that would be good. But the OCXO's that I could find were $100+! I want a clock, not an investment... :)

Anyway, here is the thread (http://forums.parallax.com/showthread.php?p=776732) where I tried to find the best solution for accurate timekeeping. I decided to go with a home made temperature controlled crystal (via a temperature sensor and a resistor for heat), and to correct for crystal deficiencies in code.

Here's what I'm thinking will be better for all involved to calibrate the clock:

Two buttons: + and -
User sets the clock when first powered up
Leave for t amount of time.
When they come back, they find the clock is off.
Press the + and - until the correct time is displayed.
Wait and repeat indefinitely.

Internally, the clock would keep track of how many seconds total have passed, and the number of clock cycles. When it detects a button press, it changes the calibration error variable, and recalculates the time passed since the clock was first programmed, which it then displays as the current time.

This way, the user just pushes the buttons until they see the correct time. I'd have to have a switch that allows for daylight savings time hour correction, but that shouldn't be much of a problem. The math will take some working out, but it should be doable... Any thoughts?

mctrivia
01-25-2009, 06:25 AM
$100 for a crystal. might as well get a GPS receiver.

Jonathan
01-25-2009, 06:41 AM
If you are going to have the date available, I have a small snip that can calculate DST start and end days for the next 50 years or so. I use it in my clock system, works great, I never have to set the clock back/forward. Send me a PM or email if you want it.

Jonathan

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.madlabs.info (http://www.madlabs.info) - Home of the Hydrogen Fuel Cell Robot

dMajo
01-26-2009, 03:50 PM
@SLRM, I have given a look at your other tread.

If you go for the RTC device I suggest you the ramtron FM31L278: in one IC you get the substitute for the boot eeprom, the RTC with a correction factor implemented, and many others. And yes, you can thermally stabilize the 32768 crystal with a resistor driven by prop.

shanghai_fool
01-26-2009, 05:01 PM
I recently purchased a few of these and am delighted with them. The FRAM is faster. The memory has same address as eeprom and clock has same address as DS1307 but clock registers are different. be sure and read the data sheet carefully. I will be soon writing a small utility to set the clock from the PC clock via USB. PM me if you are interested.

kwinn
01-26-2009, 09:15 PM
SLRM, I like the idea of 2 buttons to correct the time, simple, inexpensive, and you need buttons to set the time any way. A 60 entry table to correct for drift? Why so complicated? I would suggest 2 entries, 1- seconds since time was set, 2- seconds of correction. Call them s1 and s2, so s1/s2 gives correction factor which can be used to adjust the number of clock cycles counted for 1 second (I am assuming you are using the prop with a 5Mhz crystal). Even at pll1x (5Mhz) clock rate the correction is accurate to 0.2ppm which would be about 6 seconds a year.

dMajo
02-07-2009, 01:59 AM
SRLM,
probably it's too late but while I was browsing my HDD datasheets library, searching some data, I came across this and I remember of you

SRLM
02-07-2009, 03:00 AM
Ohh. That's a pretty cool chip, and cheap too! Only $10 at digikey. That's equivalent to a breakoutboard for one of the regular RTCs from Futurlec. It's too late now, but thanks. I'll keep it in mind for future clock projects that need a calender.

Technically, the method I'm working on right now should be able to get <1 in 80,000,000 parts accuracy at constant temperature, so I'm not worried about time issues any more.

mctrivia
02-07-2009, 03:27 AM
wow the crystal is built into the ic. cool.