[Icc-avr] Is there something wrong with this RTC interrupt?
BobGardner at aol.com
BobGardner at aol.com
Thu Jan 3 18:42:11 PST 2008
My guess is the problem is the cli and sei in the interrupt handler. The
first one does nothing, because the ints are off in the handler. The second one
reenables ints BEFORE the regs are popped, which could be the problem.
In a message dated 1/3/2008 8:53:39 P.M. Eastern Standard Time,
scottk at skelleyco.com writes:
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();
}
_______________________________________________
Icc-avr mailing list
Icc-avr at imagecraft.com
http://dragonsgate.net/mailman/listinfo/icc-avr
**************Start the year off right. Easy ways to stay in shape.
http://body.aol.com/fitness/winter-exercise?NCID=aolcmp00300000002489
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://dragonsgate.net/pipermail/icc-avr/attachments/20080103/b699b531/attachment.html
More information about the Icc-avr
mailing list