8535 Timer2 (RTC) problem

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys,

I faced a strange problem when trying to use Timer 2 for RTC using AT90S8515.
I am using AVR Studio with ICE200 emulator.

I can successfully setup Timer 0, and step into SIGNAL(SIG_OVERFLOW0), which shows that the timer 0 is working fine.

But when I tried to setup Timer 2 registers (specially for ASSR), the Timer 0 interrupt will not work. Of course the Timer 2 overflow interrupt did not work, even after I disable Timer 0 overflow interrupt.

I connected a 32.768 kHz crystal directly to pin 29(TOSC2) and pin 28(TOSC1), without any capacitor. I have also tried to change another crystal (4MHz) in case the first one is bad.

Atmel provides AT90S8535 errata ver E, which states that Timer 2 RTC will not work properly for higher supply voltage, and recommend to use supply voltage at 4V. As I am using emulator, I don't suppose this will affect me, let me know if I am wrong .......

This is the code, hope you can help to provide insight to this problem.
Thanks.

// set up Timer 0
outp(0, TCNT0); // reset counter.
outp(2, TCCR0); // prescale clk/8.
outp(1<<TOIE0, TIMSK); // enable timer0 overflow interrupt.

// Setup Timer 2
outp( (0<<OCIE2)|(0<<TOIE2), TIMSK); // disable timer 2 interrrupt.
outp(1<<AS2, ASSR); // AS2=1 will disconnect Timer2 from system clock, and use separate crystal 32.768kHz.
outp(0,TCNT2); // reset counter to 0.
outp(5,TCCR2); // Prescale clk/128, giving Timer2 overflow at every 1 sec (for RTC).

while ( inp(ASSR) == 0x0d) {} // wait for AS2, TCN2UB, TCR2UB to be set to 1.

outp((1<<TOIE2),TIMSK); // Enable the T/C2 overflow interrupt in the T/C interrupt mask register.

SIGNAL(SIG_OVERFLOW0)
{
global_timer++;
outp(0,TCNT0); //Restart T0 counter.

} // End of Timer Overflow.

SIGNAL(SIG_OVERFLOW2) // Timer 2 is used for RTC (Real Time Clock), with separate watch crystal at 32.768kHz.
{
global_timer_rtc ++;

outp(0,TCNT2); //Restart T2 counter.
// ......
// ......

} // end of SIGNAL(SIG_OVERFLOW2)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think ATMEL's errata E. is very imporant when using TIMER2 interrupt even in emulator. try pulling it down to 4.4V by a sillicon diode. REGARDS, DAMDAM

admin's test signature
 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Joseph:

You can use this section of code. I've used it and works fine (it generates an interrupt every 1/128 sec, but you can change that).

void StartRTCTimer(void)
{
cli();
isTick=0;
TIMSK &=~_BV(TOIE2); /* Disable TCNT2 overflow interrupt */
ASSR |=_BV(AS2); /*Timer2 source from external clk */
TCNT2=0x00; /* reset counter to get this interrupt again */
TCCR2=0x01; /* Start count with Ext. clock/128 */
/* (1/32768)* 256=7.8125ms */
/* 7.8125ms * 128 = 1 sec (128 ticks)*/
while(ASSR&0x07); /* Waits until the Async. reg. is updated */
TIMSK|=_BV(TOIE2); /* Enable TCNT2 overflow interrupt */
sei();
}

SIGNAL(SIG_OVERFLOW2) /* signal handler for tcnt2 overflow interrupt */
{
isTick=1;
}

Regards,

Alex

admin's test signature