thank God for this forum!!!

anyway, i've recently bought a PIC16F84. I'm still tinkering with it though. My question is how can i accurately set up a clock with it?

I've searched thru the PICLIST and they have this delays done thru loops (which uses the instruction's clock cycles...very cool ;) ) But that's just it, source codes. Do you guys have a general formula for that?

And another thing. For the PIC16F84, the timer0 has a max of 256 (with 8 bits). the prescaler has a max of 1:128. If I try to use timer0 as my clock, I'll have only a maximum of:

1/(4MHz/4) * 256 * 128 = 32.768 milliseconds of clock pulse. Am I right??

Please shed some light.

and again, thanks for this forum!!!
Posted on 2003-03-29 13:11:08 by flotsam
i guess you had know the formula :grin:
and to made a precision clock, use precision crystal, of course :grin: and dont forget about the cycles that divide that crystal freq, and calc it like what you did.
Posted on 2003-03-29 21:15:25 by dion
thanks for the reply!

yeah, i'm using a xtal oscillator for my internal clock. it's just that timer0 can only give a max of 32ms. so i'm asking for the general formula for the delay loops done with instruction sets. i want to do like more than 32ms clock pulse ;)
Posted on 2003-03-30 00:56:35 by flotsam
TMR0 can generate an interupt when it rolls over, so you can use that to count 32.768 milliseconds intervals. You may have to play some tricks to get the resolution you need if it the likely case this interval isn't an exact multiple of 32.768.

I did a job once where I took an external pulse wave and timed it over 10 (100? I forget) cycles so I could get an accurate frequency reading on it (accurate to about 8 bits). Another time I needed to generate an accurate 3 MHz square wave, did that with a 24 MHz clock for a 6MHz instruction rate, and just bit banged a port.

Microchip has some clever routines if you're doing a clock where they add or subtract a tick to the time in such a way to get an extremely good long term accuracy
Posted on 2003-03-30 06:41:30 by Ernie
tnx for the reply!

i guess i'll have to play around with TMR0 and maybe change my xtal osc.

thanks for the ideas!!
Posted on 2003-03-30 12:46:17 by flotsam
just wanna to tell if you really want a quick and accurate one, use available real time clock chip, i.e. ds1307 or 1308 . go google to find the datasheet :grin:
Posted on 2003-03-31 05:53:17 by dion
Hi, Flotsam,

This is probably my seventh attempt to reply. Something's not going right. The only piece of hardware I can kick is my computer and it cost me money, so I thought twice.

The RTCC is only 8 bits wide, but that should not be a problem: every time it rolls over, increment a RAM location, just like you do for minutes, hours, etc. When it reaches a certain value, reset it and increment the location for the seconds. So you again divide by 256 (at most). If one location isn't enough, use one more. You can now divide by up to 65,536!
Also, to make a really accurate clock, you can apply corrections. These will make your clock run accurately even if the crystal's frequency is off significantly, as long as the frequency is stable, which it will be.

Before you get to the hardware and change crystals, I suggest you read AN590 from Microchip. Here is the link:

This application note describes a clock built with PIC16C54 and 7-segment LED displays. Don't worry, as you probably know, PIC devices are extremely similar. Also, in this appnote you will find the formulas you need and it also tells you how the corrections are done to compensate for the crystal's frequency inaccuracy.
You will build yourself a really accurate clock.

Good luck!
Posted on 2003-03-31 21:09:40 by VVV

thanks a lot for the link! i'll look thru it right now! thanks again!

Posted on 2003-04-01 12:07:45 by flotsam