1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Xtal oscillators for timekeeping.

Discussion in 'Microcontrollers' started by dr pepper, May 1, 2012.

  1. dr pepper

    dr pepper Well-Known Member

    Joined:
    Oct 6, 2008
    Messages:
    4,658
    Likes:
    247
    Location:
    North west UK
    I have implemented a real time clock in software, not the first time I've done this but the first time using the primary xtal oscillator.
    I'm using a 4.096Mc's xtal for the instruction clock and timer1, no interrupts just polling one of the bits.
    Accuracy is out by a few seconds a day.
    Would I get better accuracy using a 32.768Kc xtal on timer1's oscillator?, if I use the pic's internal oscillator for the instruction cycle I'd free up 2 pins for timer1's external osc.
     
    Last edited: May 1, 2012
  2. JimB

    JimB Super Moderator Most Helpful Member

    Joined:
    Sep 11, 2004
    Messages:
    6,329
    Likes:
    585
    Location:
    Peterhead, Scotland
    ONLINE
    Without knowing the EXACT frequency of the oscillator, there is no point in debating which one is better.

    Also, polling sounds like a bad idea.
    How do you know if you are polling at the correct point in time?
    I think that you need to use interupts.

    JimB
     
  3. kubeek

    kubeek Well-Known Member

    Joined:
    Mar 11, 2006
    Messages:
    1,491
    Likes:
    186
    Location:
    Prague, Czechia (not Chechnya)
    32768 crystals have lower drift than others. The lower frequency also means lower power consumtion in the coutner, so that is why they are used in RTCs.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. dr peppers

    dr peppers New Member

    Joined:
    Feb 21, 2012
    Messages:
    120
    Likes:
    4
    Location:
    North West England.

    I use interrupts within the code allready to do other stuff, the interrupts run at 500 hz, and the bit I'm polling toggles at 125 hz, so I know theres no chance of a missed poll, if I was missing polls then I'dve thought the clock would have been slow not fast.

    I didnt know drift was less with 32.768 kc xtals, the low power bit makes sense as these are common in watches which obviously are battery powered.
    I'll have to mess about shifting one bit of my 7 segment display bits to another port pin, but that looks like the way to improve accuracy.

    Incidently my initial idea used interrupts for timekeeping rather than polling timer1, and that didnt work it ran slow, I think what was happening was that every time I reloaded the timer to get the correct interrupt duration the prescaler was reset (the pic resets the prescaler when you write to the timer register) causing an error of a few clock cycles every interrupt.
     
    Last edited: May 1, 2012
  6. dr peppers

    dr peppers New Member

    Joined:
    Feb 21, 2012
    Messages:
    120
    Likes:
    4
    Location:
    North West England.
    I have just reconfigured the circuit, the 4 mc xtal is now removed and the chip runs from its internal rc, I've shifted one of the bits off portb to porta (not very elegant but works), and timer1 now runs from a 32,768 kc xtal.
    hSo I'll leave the clock to run overnight and we'll see how accurate it is.
    The circuit is built on vero so I'm not expecting the best possible result, but hopefully better than 10 secs per day.
     
    Last edited: May 1, 2012
  7. dr pepper

    dr pepper Well-Known Member

    Joined:
    Oct 6, 2008
    Messages:
    4,658
    Likes:
    247
    Location:
    North west UK
    Obviously I'm missing something, with a 32kc xtal accuracy is worse.
     
  8. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,189
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi dr,
    You can buy 32.768KHz xtals in different stability ranges, some 'bog' standard 32KHz xtals have very poor temperature stability.

    The other point are you using high stability xtal caps.?
     
  9. dr pepper

    dr pepper Well-Known Member

    Joined:
    Oct 6, 2008
    Messages:
    4,658
    Likes:
    247
    Location:
    North west UK
    That could be the issue, the caps are ceramics out of the scrap box, judging by the lack of the black dot at the top they dont look like npo's.
    What do you mean by high stability, silver mica or polystyrene, or the likes?
     
  10. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,189
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    I would use poly or silver mica, if you 'know' the temperature drift range of your xtal, you can choose caps which drift with temperature in the 'opposite' sense.

    One method I have used in the past is a xtal small oven, running at say 5 or 10C above the worst case ambient.

    If you could find an old watch xtal, thats also an option.

    If you require really precise time keeping, there are improved versions of the DS1307 IC available, for about £5.

    EDIT:

    http://uk.farnell.com/jsp/search/br...arametricAttributeId=&prevNValues=2031+203436
     
    Last edited: May 2, 2012
  11. Gobbledok

    Gobbledok Active Member

    Joined:
    Mar 9, 2008
    Messages:
    452
    Likes:
    34
    Location:
    Toowoomba, Australia
    I have a PIC32 (with RTCC module) and am using a 32.768KHz crystal for the clock.

    It is accurate to about 2 seconds a day (compared to my computer). That's a minute a month without any compensation (software or oscillator tuning).

    Accurate enough for me :)
     
  12. dr pepper

    dr pepper Well-Known Member

    Joined:
    Oct 6, 2008
    Messages:
    4,658
    Likes:
    247
    Location:
    North west UK
    Interesting, I ahve used the dallas timekeepers before, I didnt want to this time to keep parts count down and the pic doesnt have to do much else, I didnt know there was a more accurate version of the chip, I'll look out for one of those.

    I sussed the problem, as often happens something completely diffo than what I was looking at, the circuit switches relays to control the outside world, noise from the same must've been affecting tmr1's oscillator, a dose of 100n caps and a little sort out with the wiring and from 5pm last nite till 5am today there is less than a second diffo between my msf clock and the pic.
    Oh yes I'm using a watch crystal, my supplier lists a few of them, total pain to solder in the board, even though they are through hole thye are smaller than a grain of rice.
     
  13. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,189
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    This is the RTC IC I was suggesting, DS3231
     
  14. Mr RB

    Mr RB Well-Known Member

    Joined:
    Jul 22, 2008
    Messages:
    4,716
    Likes:
    194
    Location:
    Out there
    What caps do you have on the xtal? They affect it's frequency, as will the track capacitance of the veroboard.

    You can get 40pF trimmer caps to replace the OSC1 pin cap and trim the frequency.

    Also 10 seconds per day is better than most crystals will handle especially if the device is not kept at body temperature.

    Mains freq is usually quite good, have you considered using the mains frequency for the clock or using it to trim the clock (called "disciplining")?

    This web page has a lot of simple techniques for making accurate 1 second periods with a PIC from any xtal including being able to fine tune the 1 second period in software not hardware;
    http://www.romanblack.com/one_sec.htm

    This one has some stuff on mains freq synchronising including ways to use the PIC xtal as the main clock (in case of blackout) but also tie that freq to mains freq when it is present;
    http://www.romanblack.com/onesec/High_Acc_Timing.htm
     
  15. dr pepper

    dr pepper Well-Known Member

    Joined:
    Oct 6, 2008
    Messages:
    4,658
    Likes:
    247
    Location:
    North west UK
    I like the pic measuring its own frequency bit, might try that.

    I have added some 100nF caps to the board and shotened the 5v supply line, its been running 3 days now and as far as I can tell its still within a second, I think my issues were noise upsetting the osc, the '88 is a low power device and as you know low power osc's are sensitive to noise, the caps on the xtal are ceramic npo's.

    I've never messed with diciplining, gonna loo into that too.

    Another project of mine uses the xtal oven of your design roman, if I need further accuracy thats the next move, however now it seems more than good enough.

    I have a radio 4 frequency standard, it tunes a 10mc xtal with a varicap on one side, that works quite well.
     

Share This Page