PDA

View Full Version : Accurate 32.768 k clock



peterk
- 13th July 2004, 04:34
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

Melanie
- 13th July 2004, 11:46
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.

peterk
- 13th July 2004, 22:41
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

Melanie
- 13th July 2004, 23:50
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)...



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...

peterk
- 14th July 2004, 23:04
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