Shop OBEX P1 Docs P2 Docs Learn Events
Better Time Keeping — Parallax Forums

Better Time Keeping

PhilldapillPhilldapill Posts: 1,283
edited 2008-01-04 20:29 in Propeller 1
Hey again. I'm working on making·a simple clock with the prop. I'm displaying it on the TV screen(thanks guys). I am testing to see the accuracy of it and I'm not very pleased. It loses about 1/2 second in three hours. I know this because I have a wall clock that keeps time very well and I'm watching the clock ticks vs. the prop seconds on the screen. I have the setup synchronized at first, but after 3 hours tonight I noticed the lag. The code is below but I have a feeling to regain that 1/2 second, I'll have to use some ASM code... Here is the basis of what I have. I've deleted what I know for a fact makes no difference.

PUB Main |·temp

· temp := cnt·· 'set the tick time to now
· repeat
··· 'Do some code that was deleted
··· text.dec(sec)·· 'output the seconds
··· sec++··· 'Increment seconds
····························
··· WAITCNT(temp)· 'Wait until specified time
··· temp += clkfreq·· 'increment temp 1 second - Maybe make this faster addition?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-04 05:35
    The first time through the loop, the WAITCNT will wait about a minute because the initial time is set to CNT. By the time the WAITCNT occurs, that will have passed and the cog will wait for the system clock to wraparound which takes about a minute at 80MHz. All subsequent WAITCNTs should be right on. The time it takes for the addition shouldn't matter as long as the WAITCNT occurs before the updated time (in less than a second). The time that the cog pauses is accurate to the nearest system clock tick. 1/2 second in 3 hours would be about 3 x 60 x 60 x 2 = 21,600 or less than 0.005% error. This is on the order of the typical crystal accuracy (20-100ppm). You can use an accurate frequency counter and trimmer capacitors in parallel with the Propeller's internal crystal capacitance to compensate for the error. You could also use an external crystal clock that's trimmed to a higher accuracy and temperature compensated.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-04 05:45
    Oops, that was actually an error when I was deleting out some useless code. The first itteration is fine.

    As for the cyrstal, it's fine for what I'm doing. It will only be used as a system clock for scheduling tasks Weekly. I just figured that a clock tick or two that it takes to add the temp and clkfreq was causing the offset. I understand it as this:

    Capture clock time and add 1 second(that's where my error was in the a.m. code)...
    -Loop-
    Wait until temp time
    Add 1 second to temp and loop back to starting loop(this seems like it would take a couple clock cycles to add which this cpu time would eventually add up to 1/2 second over 3 hours)

    Now, let's just say that the crystal is an atomic clock and perfectly accurate... 2 clock cycles over 3 hours would be... 1/80mhz * 2 cycles * 60sec * 60min * 3hrs = 0.00027 seconds. I guess 2 clock cycles isn't a big deal after all.

    Thanks Mike! I guess that wraps up that question.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-04 05:45
    The time you're seeing is lagging by one part in 21600 (46 ppm). All other influences (e.g. temperature) being constant (which they almost certainly are not), you could add subtract clkfreq / 21600 to from the clkfreq added to temp to make up the difference.

    But I'm not sure this will suffice over the long run. For the ultimate in accuracy, if that's what you're after, you may have to resort to external synchronization (GPS or WWV, for example) or an external chip (TCXO, for example) to do your timekeeping for you.

    -Phil

    Update: To make the clock run faster, you diminish the time increment, not augment it.

    Post Edited (Phil Pilgrim (PhiPi)) : 1/4/2008 8:41:00 AM GMT
  • PhilldapillPhilldapill Posts: 1,283
    edited 2008-01-04 07:45
    Thanks Phil. Nah, I'm not too concerned with accuracy. Like I said, this is only used for scheduling a task or two every week so timing isn't critical. If it get's to be off by 4 seconds a day, that's negligible. It would take about 2 weeks to be off by one minute. Actually, now that I think about it... That is quite a bit of time. I'd have to sync the time every other month or it will be off by 5 minutes... Hmmmmm, nah, for now I might just add a couple of clock ticks to the temp variable and see how well it keeps the time. Thanks guys for the input. I really appreciate it.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-04 08:32
    If you're running at 80 MHz, try adding subtracting (since you want the "seconds" to arrive sooner) about 3700 clock ticks (80_000_000 / 21_600).

    -Phil
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-04 09:31
    This a well observed fact from EVERYBODY trying to implement a "clock". You get around 0.01% with an "untempered" crystal, or 3 to 10 seconds per day.
    Crystals - though in principle 100 times more precise - are generally not good adapted to the environment of load to a microcintroller. As most microcontrollers need additional cap loads of around 22pF at each crystal lead anyhow they can easily be tuned to a better adaption by a 30pF trim cap. This will improve accuracy by an order of magnitude (1 sec/day). A software calibration as described by Phil has a similar effect.

    To get more you have to compensate for temperature (as Phil already said) using more expensive but still affordable compensated oscilators.

    Why is our cheap watch so more precise?
    (a) It's crystal is perfectly adpted.
    (b) It is only precise when you wear it (30°C)!
  • RaymanRayman Posts: 14,162
    edited 2008-01-04 14:05
    It's very easy to add a DS1307 i2c real-time clock with battery backup, if you want better timekeeping...
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-01-04 16:20
    I just recently bought my wife one of those thermometer displays that uses a 900 MHz external transmitter for the temperature (yes, she actually wanted one).· The instructions says that it uses WWVB to update the clock every night.· Being a Propeller hobbyist, my first thought was "Gee, I wonder what it would take to get the Propeller to use WWVB??"·

    WWVB is transmitted at a frequency of 60 kHz, which is within the A/D capabilities of the Prop, and it is amplitude modulated at two different levels to transmit the bits.· Seems feasible with minimal external componentry.

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


    The more I know, the more I know I don't know.· Is this what they call Wisdom?

    Post Edited (Ken Peterson) : 1/4/2008 4:34:49 PM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-04 17:46
    Ken,

    To make your life easier, you could tack on a cheap RF front-end module like this one, which DigiKey carries for $9.45.

    -Phil
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-04 17:56
    Since times unthinkable we have in Europe a long wave net called DCF 77 (controlled by the most precise clock of the PTB in Braunschweig (is it "Brunswigia"? )) which delivers the time for nearly all alarm and kitchen clocks at least in Germany. Most people buy it because clocks with it adjust for DST automatically smile.gif

    It needs a ferrite coil and a HF transistor. Code is available from Marcel Majoor. I don't know where he posted it, but I have a copy...
  • JonathanJonathan Posts: 1,023
    edited 2008-01-04 18:30
    Another really cheap way to keep a clock in sync is to monitor the 60Hz mains line. It is very accurate over time, suprisingly so. An H11A1 or similar can be used to detect the zero cross of the mains. In fact, any opto-isolator will do.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-04 18:33
    Jonathan said...
    ... suprisingly so.
    Not at all smile.gif They do it with purpose!
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-01-04 18:54
    Here is an article about power-line frequency.
    The article said...
    The frequency of large interconnected power distribution systems is tightly regulated so that, over the course of a day, the average frequency is maintained at the nominal value within a few hundred parts per million.
    I would imagine that it has to be much better than that. Hundreds of PPM is no better than the crystal that started this thread!

    -Phil
  • deSilvadeSilva Posts: 2,967
    edited 2008-01-04 19:10
    The difference is that the power lines have a tiny max deviation from "global atom time".
    A crystal has an evironment dependant bias, accumulating to arbitrary size if unchecked.

    As power line networks are most trickily interleaved they need to be phase locked to prevent great demage, but they don't need anything above a certain degree of sync. A small phase shift is unavoidable. Think of the energy involved: It's Peta Watt Hours per Year per nation!

    Edit:
    In fact the small frequency deviation is used as a control value to adjust the energy production to the consumption.

    Post Edited (deSilva) : 1/4/2008 7:22:46 PM GMT
  • JonathanJonathan Posts: 1,023
    edited 2008-01-04 19:27
    The system is also corrected during periods of low usage. So, if they got behind a little during the day, they overclock it a little until it catches up. While it isn't perfect, and a clock will accumulate some error over time, it's still a very good deal considering that an opto-isolator is < $2 US.

    @DeSilva- I found it suprising when I learned what effort they go to to keep the sync on the mains lines. It made sense once I thought about it.

    Another time keeping method: I use a GPS receiver to determine the time, then rebroadcast it locally using cheap RF modules. All of the clocks in my house save one run on this timebase. Sweet, never needs setting, comes right back after a power outage (common where I live, the power is out as I type this, I'm on my generator), and is very accurate. I have used a WWVB solution in the past, but where I live I can only sync to WWVB at night.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-01-04 19:58
    Powerline frequency is ok if you need a simple time base. I think many cheap clock radios use it.

    If you use UTC transmissions you never have to set the clock, and it will work for battery operated applications too. For this, the CMMR-6 solution seems simple ard reasonably priced. The thought in my mind is how much can be done in software to further minimize hardware.· [noparse][[/noparse]edit] I suppose this is more academic than practical for the average hobbyist[noparse][[/noparse]/edit]

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


    The more I know, the more I know I don't know.· Is this what they call Wisdom?

    Post Edited (Ken Peterson) : 1/4/2008 8:33:54 PM GMT
  • JonathanJonathan Posts: 1,023
    edited 2008-01-04 20:21
    I couldn't find a price on that unit. My OncoreII cost me $25US on E-Pay, and the antenna another $10. Cheap, and syncs anytime in minutes at most, has a built in backup RTC as well. It may seem like a waste or overkill, but it works and is cheap.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    www.madlabs.info - Home of the Hydrogen Fuel Cell Robot
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-01-04 20:29
    CMMR-6 at Digikey:
    search.digikey.com/scripts/DkSearch/dksus.dll?Detail?name=561-1014-ND

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


    The more I know, the more I know I don't know.· Is this what they call Wisdom?
Sign In or Register to comment.