Clock with timer interrupt0

# Thread: Clock with timer interrupt0

1. ## Clock with timer interrupt0

Hello,
I have problem with the stability of my clock using 12F625. I have 4 mhz crystal with 2 x 22 pF.
My assumption was, that instruction takes 1 microsecond. Timer0 has 8 byte = 256 microsecond. I have prescaler 1:256 = 65536 microseconds.

So my code looks like:

day VAR BYTE ' Define day in the week
hour var byte ' Define hour variable
minute var byte ' Define minute variable
second var byte ' Define second variable
ticks var byte ' Define pieces of seconds variable
command var byte
SERIALBYTE var byte

OPTION_REG = \$07

on interrupt goto intManagement
enable
WHILE 1
if manageIntMinute == 1 then
if (manageIntMinute // 10) second = second - 1
manageIntMinute = 0
minute = minute + 1 ' manage time update
if (LIGHTCOUNTER > 0) then
LIGHTCOUNTER = LIGHTCOUNTER - 1
if (LIGHTCOUNTER == 0) then low PORTA.2
endif
If minute >= 60 Then
minute = 0
hour = hour + 1
If hour >= 24 Then
hour = 0
day = day + 1
if day >= 7 THEN day = 0
Endif
Endif
endif
serin porta.3,2,10, cont, command 'T9600
serin porta.3,2,5, cont, SERIALBYTE 'T9600
serout porta.0,2,["R ", #command, " ", #SERIALBYTE, 10]
gosub manageSerialByte
cont:
WEND
END

intManagement:
disable
if (INTCON.2 == 1) then
ticks = ticks + 1 ' Count pieces of seconds
tmr0 = 61
If ticks < 21 Then tiexit
toggle PORTA.1
ticks = 0 'One second elasped - update time
second = second + 1
If second >= 60 Then
second = 0
manageIntMinute = 1
ENDIF
tiexit:
INTCON.2 = 0
endif
INTCON.7 = 1
enable
resume

As is seen, I will each time initialize tmr0 to 61: 256 x 61 = 15616
So I asumed 20 times * (65536 - 15616) = 998400 microseconds.
So I have counted that hour has 3600 seconds, the error is 3600 x 0,001600 = 5,76 second per hour so each 10 min one sec down...

But the problem is, that my clock is not running like that, it looks like, that each minute i will loose about 7 second.
Any ideas?

2. Hello boban,
I am getting a little rusty being away from here, while at home. Preload your timer with a value to compensate for the error. As I recall you lose a few cycles each time the timer is called and the preload will offset that. instead of tmr0 = 61 try a larger value until it keeps time.

3. Looks like you have a good xtal... wait until you find a bad one...

Most xtals are 4.0 MHz... their accuracy isn't 4.000000000 MHz, some could even be 4.09 or 3.90 MHz.

You want to spend \$20 on a xtal, you'll get better accuracy, but what are you asking from a 20 cent device? Put in a 30pF trmmer capacitor and trim for exactly 4.0000000MHz with a 10-digit Frequency Counter phase-locked to a 10MHz Atomic Standard. Oh... don't forget to enclose the xtal and PIC in a temperature controlled oven +/- 0.1C as well... and then, and only then, you can start getting accuracy to better than 10 seconds per DAY.

Lets face it, I've used a Dallas/maxim DS1307 (which costs a fortune) and a quality EPSON 32kHz xtal, and I whoop for joy if I get an accuracy better than 15 minutes in a YEAR.

All this timekeeping using microprocessors is complete rubbish. You need to lock it to a high quality, high stability clock source for any kind of accuracy even approaching your cheap 99 cent Laser-Trimmed, digital wristwatch.

4. 7 seconds a minute is 11.6% error, probably not a crystal error but the code.

Changing timer zero's count is very funky to do as there is a built-in two instruction cycle delay before it starts counting again. The last timer I built (2% accuracy using a resonator) uses a Timer 2 as the time beat. Timer 2 is preferable as it has a compare value; I have the same clock (1MHz) and load PR2 with 250 so I get a steady evenly spaced interupt every .25 miliseconds without having to reload any registers.

That's kind of fast for once a sec updating but you also have pre and post scaler to work with.

BTW, what processor are you using? Microchip says "your search 12F625 did not match any documents. please refine your search."

5. If you are running from AC, You can get good accuracy by timing the 50 or 60Hz from the power transformer. If the AC goes away, you can turn on your
timer interrupt.
Just like the little alarm clocks with battery backup.

6. The comment about using the AC line frequency is pretty sharp - The Hammond Clock company used to sell AC powered clocks with synchronous motors, and they would make gifts of these clocks to operators of the power utilities. The power plant operators would then have an incentive to regulate the AC line frequency.

I think in your case, you should look into a real-time clock IC. There are many of these to choose from, and you can interface to them using a variety of techniques. As has been stated in this thread, trying to use the counter in a PIC to keep accurate time in the long term is a waste of, um, time.

7. ## Search and Research

Darrel Taylor's
A better Search tool for the Forum

Finds:

Paul Borgmeier's
Easy and Accurate Clocks without RTC IC