[bascom] 8051: Crystal accuracy problem or Timer & Interrupts mistake?


From "Patrick L'huillier" <Patrick.Lhuillier@planet-interkom.de>
Date Sun, 17 Jun 2001 19:48:23 +0200

Hi,
I just checked the archive, found a lot of questions how to use timers and interrupts but did not find a suitable answer to my problem.
 
used components:
 
- Bascom 8051 V2.08
- AT89C2051
- 12 MHz crystal connected with 33 pF cap.
 
problem:
the programme I wrote needs a port pin to switch every minute. To measure time, timer0 was set in mode 2 as follows:
--------------------------------------------------------------------------------------------------
Enable Interrupts
Config Timer0 = Timer , Gate = Internal , Mode = 2
Load Timer0 , 250                                            '250 uS = 4 kHz
 Reset Tcon.0                                                 'triggered by rising edge
 Enable Int0
 Enable Timer0
 On Timer0 Timer_0_int
 On Int0 Receiverc5
...
Start Timer0
...
--------------------------------------------------------------------------------------------------
 
The interrupt routine must be called 4000 times until 1 second is over. The programme is working, but within 24 hours there is an error of approx. 9 seconds (too fast). I expected a much lower error and slower because of the programme code within the interrupt routine which is processed every time.
 
As an alternative I thought a configuration in Mode 1 would also be a possible way and configured timer0 as follows:
--------------------------------------------------------------------------------------------------
Config Timer0 = Timer , Gate = Internal , Mode = 1
 Reset Tcon.0                                                 'triggered by rising edge
 Enable Int0
 Enable Timer0
 Enable Interrupts
 On Timer0 Timer_0_int
 On Int0 Receiverc5
 Counter0 = 40536     ' (65536-40536)/(12MHz/12)=25msec
...
Start timer0
...
--------------------------------------------------------------------------------------------------
 
The interrupt routine must be called 40 times until 1 second is over. Within the interrupt routine the counter0 variable is loaded and timer re-started:
 
--------------------------------------------------------------------------------------------------
Timer_0_int:
Counter0 = 40536
Start Timer0
 
incr count
 
if count = 40                            ' 1 second is over
...
RETURN
---------------------------------------------------------------------------------------------------
 
In this case I observed that the routine is causes a delay,  i.e. within 45 minutes the error is approx. 6 seconds too slow.
 
First I thought the quality of the crystal caused this problem. But I doubt this now. Maybe I did something wrong or configured the timers wrong?
 
If there is a hardware problem I would expect that the timer in Mode 1 has the same/ similar result than in Mode 2 (too fast).
 
Note : during the tests no external error occured (RC 5 etc.)
 
Did I miss something?
 
Thank you very much
 
Regards
Patrick