[Icc-avr] Is there something wrong with this RTC interrupt?
Scott
scottk at skelleyco.com
Thu Jan 3 17:51:34 PST 2008
My RTC is running slow. I have a number of devices that are
running this code, and all of them are losing somewhere
around 3 or 4 minutes per month.
Looking at my code, it seems that I must be missing ticks
while the isr is running. If I did my math right, it seems
like I must be missing 3 or 4 ticks per second.
If this is right, I think what I need to do is to init the
timer as a countdown, with a preset value equal to 32,768
minus the number of lost ticks. Am I right about this?
This is an ATMega64 running at 3.69 MHz. Does it make sense
that it would lose this many ticks? Seems like a lot when
there is more than 1,000:1 ratio between the main clock and
the RTC frequency...
void rtc_init(void) { // init timer for 1 second interrupt
CLI(); //disable all interrupts - prevent errant interrupts
until set up
TCCR0 &= ~(1<<WGM00); TCCR0 &= ~(1<<WGM01); // Set timer for
normal operation WGM00 = 0, WGM21 = 0
TCCR0 &= ~0x02; TCCR0 |= 0x05; // counter 0 prescaler = 128
CS02=1, CS01=0, CS00=1 (Timer Clock = 32,768 / TCCR2)
TIMSK &= ~((1<<TOIE0)|(1<<OCIE0)); // Disable Timer0
Interrupt TOIE0 = 0, OCIE0 = 0
ASSR |= (1<<AS0); //set Timer/Counter0 for asynchronous
operation - 32,768 Hz crystal AS0 = 1
TCNT0 = 0x00;
TIMSK |= (1<<TOIE0); // Enable Timer0 overflow Interrupt
SEI(); //set the Global Interrupt Enable Bit
} //void rtc_init(void)
void timer0_overflow_isr(void) { // Clock ISR - overflow
occured TCNT2=OCR2
CLI(); // turn Off interrupts
GeneralTimer++;
CommunicationsTimeout ++;
SecondFlag = 1;
WDTimerFlag = 1;
if (++second>=60) { //keep track of time, date, month, and
year
second=0;
MinuteFlag = 1; // Additional clock service is now
passed back to the main code
}
SEI();
}
More information about the Icc-avr
mailing list