Shop OBEX P1 Docs P2 Docs Learn Events
Cyrstal Compensation Code? — Parallax Forums

Cyrstal Compensation Code?

SRLMSRLM Posts: 5,045
edited 2009-02-06 20:27 in Propeller 1
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.

Comments

  • mirrormirror Posts: 322
    edited 2009-01-22 04:21
    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
  • SRLMSRLM Posts: 5,045
    edited 2009-01-22 04:37
    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.
  • mirrormirror Posts: 322
    edited 2009-01-22 04:51
    I edited my previous post.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • mctriviamctrivia Posts: 3,772
    edited 2009-01-22 05:48
    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
  • dMajodMajo Posts: 855
    edited 2009-01-22 08:46
    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
  • mirrormirror Posts: 322
    edited 2009-01-22 09:24
    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.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • virtuPICvirtuPIC Posts: 193
    edited 2009-01-22 09:40
    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 for tools & toys
  • mirrormirror Posts: 322
    edited 2009-01-22 10:07
    www.google.com -> "what is the time"

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • dMajodMajo Posts: 855
    edited 2009-01-22 14:08
    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:
  • kwinnkwinn Posts: 8,697
    edited 2009-01-24 20:46
    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.
  • BeanBean Posts: 8,129
    edited 2009-01-24 20:57
    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

    Bean.

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


    ·
  • SRLMSRLM Posts: 5,045
    edited 2009-01-24 22:09
    Okay, I've finally got a chance to work some more on this, so I'll do each topic by itself.

    [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.
    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... [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?
  • mctriviamctrivia Posts: 3,772
    edited 2009-01-24 23:25
    $100 for a crystal. might as well get a GPS receiver.
  • JonathanJonathan Posts: 1,023
    edited 2009-01-24 23:41
    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 - Home of the Hydrogen Fuel Cell Robot
  • dMajodMajo Posts: 855
    edited 2009-01-26 08:50
    @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_foolshanghai_fool Posts: 149
    edited 2009-01-26 10:01
    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.
  • kwinnkwinn Posts: 8,697
    edited 2009-01-26 14:15
    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.
  • dMajodMajo Posts: 855
    edited 2009-02-06 18:59
    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
  • SRLMSRLM Posts: 5,045
    edited 2009-02-06 20:00
    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.
  • mctriviamctrivia Posts: 3,772
    edited 2009-02-06 20:27
    wow the crystal is built into the ic. cool.
Sign In or Register to comment.