Accurate 32.768 k clock


Closed Thread
Results 1 to 5 of 5
  1. #1
    peterk's Avatar
    peterk Guest

    Question Accurate 32.768 k clock

    Hi,

    Using the 12F675, great chip!, and an ext 32.786 khz xtal,
    can anyone suggest a simple program to give an accurate 1 second counter?

    The pause 1 gives 122.33333 ms, which will give an error over time, pardon the pun!.

    Thanks.

    Peter

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    There’s only one real advantage running a PIC at 32kHz, and that it’s power consumption is minimal. Beyond that you leave yourself with no room to manoeuvre with instructions.

    The 32,768 Hz Oscillator gives you an instruction time of one quarter of that... 122.0703uS. That’s only 8192 ($2000) instructions per second.

    Knowing that a 16-bit Timer generates an interrupt, or pops up a Flag when it rolls over from $FFFF across to $0000, so therefore if you take $0000 and subtract $2000 from it you get $E000. Preload the 16-Bit Timer with $E000, and every second you’ll get an interrupt or Flag waving about.

    Each time you see the Flag you need to reload the Timer with $E000 for the start of the next second, and reset the Flag. Those actions will themselves take a few instructions, so you may have to play and adjust $E000 accordingly, adding a little to trim the timekeeping... therefore you probably might end up with something like $E008.

    However this ‘Background’ method of timekeeping leaves the PIC with eight thousand instructions each second to play with and do other useful things before the next Flag pops up.

    For really accurate timekeeping, you need a FASTER Clock, not a slower one. At 32.768kHz each instruction cycle (clock tick) you add or take away costs you 122.07uS. At 20MHz, each clock tick costs you 200nS. You can see that you can achieve a much finer adjustment allowing for far more precise timekeeping if you go FASTER rather than slower.

  3. #3
    peterk's Avatar
    peterk Guest


    Did you find this post helpful? Yes | No

    Smile

    Hi Melanie,

    True, a faster clock will give more accurate timing as you explained, however I also needed low power consumption.

    (Dont wont much do I!)

    However, the remaining instruction time left is more than plenty for my particular application.

    Now to the PIC basic Code.

    I have never played with loading the Timer.

    Any suggestions?

    Peter

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    1. Arm yourself with the PICs Datasheet.

    2. Have a look at Section 5 - Timer1 Module

    3. Have a look at section 5-1 T1CON... see if you can figure why I would set it to...

    T1CON=%00000001

    4. Find TMR1H, TMR1L anf PIR1... see why I would load them thus...

    TMR1H=$E0
    TMR1L=$00
    PIR1.0=0

    5. Read section 5.6 it might interest you...

    6. Have a look at Bit 0 of PIR1 (Datasheet 2-5) what would this piece of code do?...

    While PIR.0=0:Wend

    Answers to these questions will qualify you as a Timer1 expert, and gain you the appropriate shoulder badge...

    Very brief example (assuming your 32kHz clock)...

    Code:
        LED var GPIO.0
    
        TRISIO=%00000000
    
        T1CON=%00000001
    Loop:
        TMR1H=$E0
        TMR1L=$00
        PIR1.0=0
        Toggle LED
        While PIR1.0=0:Wend
        Goto Loop
    
        End
    *smiles*

    Next step will be the Interrupt Navigators badge.... however, I think I've mentioned before that interrupts on a 12 series PIC are a bit limited...

  5. #5
    peterk's Avatar
    peterk Guest


    Did you find this post helpful? Yes | No

    Default

    1. Arm yourself with the PICs Datasheet.

    ' Done

    2. Have a look at Section 5 - Timer1 Module

    ' Yep

    3. Have a look at section 5-1 T1CON... see if you can figure why I would set it to...

    T1CON=%00000001

    ' You enable Timer 1, allow it work

    4. Find TMR1H, TMR1L anf PIR1... see why I would load them thus...

    TMR1H=$E0
    TMR1L=$00
    PIR1.0=0

    ' You preload the timer with the hex value of E000
    ' You reset the Timer Overflow flag

    5. Read section 5.6 it might interest you...

    ' Very interesting that I can run the clok with the device in sleep
    ' mode, thus even less consumption

    6. Have a look at Bit 0 of PIR1 (Datasheet 2-5) what would this piece of code do?...

    While PIR.0=0:Wend

    ' It will will wait until PIR bit 0 = 1, or when the timer roll's over

    Answers to these questions will qualify you as a Timer1 expert, and gain you the appropriate shoulder badge...

    ' OK, so I will try the code....

    Very brief example (assuming your 32kHz clock)...

    code:--------------------------------------------------------------------------------
    LED var GPIO.0 ' Assign Led name to GPIO port 0

    TRISIO=%00000000 ' Set all ports as outputs

    ' Question..... does this affect the xtal ports 4,5 ?

    T1CON=%00000001 ' Set bit 0 High in T1Con

    Loop:
    TMR1H=$E0 ' Preload the High Byte with E0
    TMR1L=$00 ' Preload the Low Byte with 00
    PIR1.0=0 ' Reset the interupt flag
    Toggle LED ' Change the LED status, ie from on
    ' to off or off to on
    While PIR1.0=0:Wend ' wait for the roll over
    Goto Loop redo the whole process

    End
    --------------------------------------------------------------------------------

    *smiles*

    ' No smiles I' afraid
    ' I believe there are a few more things to do
    ' Some more registers to set
    ' I'll keep digging, and reading
    ' However, the above may help other persons as well

    Next step will be the Interrupt Navigators badge.... however, I think I've mentioned before that interrupts on a 12 series PIC are a bit limited...

    ' Woooha, interupts! Lots to learn, nice to get help!
    ' Will keep on
    ' Thanks, Peter
    Last edited by peterk; - 14th July 2004 at 23:10.

Similar Threads

  1. Single digit 7 Seg LED clock - PIC16F88
    By thirsty in forum Code Examples
    Replies: 4
    Last Post: - 17th July 2009, 08:42
  2. EM4095 Chip, get Clock and Data signals?
    By mindthomas in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 19th August 2008, 06:27
  3. Accurate clock using 12C508
    By srob in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 28th September 2007, 09:19
  4. Shiftout/in
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 23rd August 2007, 11:48
  5. Help with sound command in 2 programs
    By hyperboarder in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th July 2007, 20:36

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts