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