Electrical – Sleep mode in ATMEGA16 not working

atmegaavrcsleeptimer

I am trying to put my controller in sleep mode and waking it up on a certain number of timer overflow. The code is as below:

//*******TIMER 2 INITILIZATION  will be used
    TCCR2=0X07;   //clock source select, prescaler
    TIMSK=0X40;  //timer 2 interrupt selected
    TCNT2=0X00;   // timer resistor
    sei();

    // controller sleep mode
    set_sleep_mode(SLEEP_MODE_PWR_SAVE); // sleep mode selected
    sleep_enable();  // set SE bit
    sleep_cpu(); // sleep mode activated
    do 
    {
        if(count>=1000) //  min delay
        {
            sleep_disable();   // SE bit reset
            _delay_ms(1000);
            print("waking up.....");
            _delay_ms(2000);
            cmd(0xc0);
            print("AT+CFUN=1");
            _delay_ms(1000);
            transmit("AT+CFUN=1\r\n");
            _delay_ms(1000);
            _delay_ms(2000);count=0;
            break;              

        }
        else
        {
            print("sleep");
            sleep_enable();
            sleep_cpu();                
        }       

    } while (1);

the corresponding ISR is as follows

ISR(TIMER2_OVF_vect)
{
    count++;

}

The problem it is not coming out of sleep mode.

I am using internal clock 8MHz and timer2.
edit: i found this description in datasheet ,so this means i have to use asynchronous clock for timer 2 (i.e provided by external crystal osc).Is there any way i can do this with the help of internal clock.
power save mode description in datasheet

Best Answer

As you said, timer 2 can only be used with external Xtal in power save mode.

  • Either use the Idle sleep mode (you may slow down MCU frequency before entering sleep to gain consumption)
  • Or you could use the watchdog to wake you up (unsing interrupt and reset mode). Of course this will be a bit less precise but you could use power down sleep mode which is even better.