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?
Bookmarks