Cyrstal Compensation Code?
SRLM
Posts: 5,045
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:
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.
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.
Comments
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:
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:
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
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
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.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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 for tools & toys
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
This receivers works also for MSF(UK), WWVB(USA), JJY(JPN) and others:
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
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
·The next time you need a hero don't look up in the sky...Look in the mirror.
·
[noparse][[/noparse]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.
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...
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.
The user (human) will know the correct time from whatever source they have. Time.gov is a good one.
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.
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... [noparse]:)[/noparse]
Anyway, here is the thread 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?
Jonathan
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
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.
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
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.