[SOLVED] Asynchronous Mode for Timer2 Atmega8a

Hi All,

I’m building a project using Atmega8a. I need to use Timer2 in Asynchronous mode (I’m using a 16MHz crystal at XTAL) so that I can wakeup from POWER_SAVE sleep every 15 mins or so.

Here is a simple example (flash LED every 2048 ms i.e 2 secs => using 105 as overflow count) which I have used to test out Asynchronous mode for Timer2.

Below code is working fine.

But when I add code to set asynchronous mode, the LED doesnt flash. Instead it remains ON.

The code lines in questions are marked using // <---------------------------------

Here is the full program:

#include <avr/io.h>
#include <avr/interrupt.h>
  
// global variable to count the number of overflows
volatile uint16_t tot_overflow;

volatile boolean toggle = true;
  
// initialize timer, interrupt and variable
void timer2_init()
{
    //ASSR |= (1 << AS2);                   // <---------------------------------
    
    // set up timer with prescaler = 1024
    TCCR2 |= (1 << CS22)|(1 << CS21)|(1 << CS20);

    //while(ASSR & _BV(TCN2UB));            // <---------------------------------
  
    // initialize counter
    TCNT2 = 0;
  
    // enable overflow interrupt
    TIMSK |= (1 << TOIE2);
  
    // enable global interrupts
    sei();
  
    // initialize overflow counter variable
    tot_overflow = 0;
}
  
// TIMER0 overflow interrupt service routine
// called whenever TCNT0 overflows
ISR(TIMER2_OVF_vect)
{
    // keep a track of number of overflows
    tot_overflow++;
}
  
int main(void)
{
    // connect led to pin PC0
    pinMode(6, OUTPUT);
    digitalWrite(6, toggle);
  
    // initialize timer
    timer2_init();
  
    // loop forever
    while(1)
    {
        // check if no. of overflows = 12
        if (tot_overflow >= 125)  // NOTE: '>=' is used
        {
            toggle = !toggle;
            digitalWrite(6, toggle);
            TCNT2 = 0;            // reset counter
            tot_overflow = 0;     // reset overflow counter
        }
    }
}

Can anyone please help me out to set asynchronous mode for Timer2?

Thanks

I need to use Timer2 in Asynchronous mode (I'm using a 16MHz crystal at XTAL)

I do not believe that you can use a 16Mhz clock source for asynchronous mode. The data sheet for the Atmega8A states (Section 22.9 Asynchronous Operation of the Timer/Counter)

The CPU main clock frequency must
be more than four times the Oscillator frequency.

It is explained in application note AVR130 Setup and Use of AVR Timers this way

Asynchronous operation requires some additional consideration. Because the clocking of Timer2 is
asynchronous, the timer events have to be synchronized by the CPU. This requires a timer clock
frequency, which is at least four times lower than the system clock

The asynchronous mode is optimized for a 32.768kHz watch crystal.

Hi cattledog,

So does that mean I can’t use an external crystal for main clock and use Timer2 asynchronously at the same time? (Since both share the same pins?)

So does that mean I can't use an external crystal for main clock and use Timer2 asynchronously at the same time? (Since both share the same pins?)

You are probably correct that you can not do this.

That said, I have no direct experience and only know what I see in the data sheet.

Hi cattledog,

Thanks for pointing me out to that piece of info. Eventually I replaced the old Atmega8a(I had a chip lying around) with the newer arch Atmega88pa. Using Atmega88pa I have written the code to use Watchdog Timeout Interrupts to wake up from POWER DOWN sleep mode.