1
\$\begingroup\$

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

\$\endgroup\$
10
  • \$\begingroup\$ Why do you return(count) out of interrupt service? It is pretty meaningless. Also, is it declared as volatile? \$\endgroup\$
    – Mike Spark
    Aug 19, 2016 at 9:18
  • \$\begingroup\$ i have declared count as integer type globally with initially value 0.the return function i was using just to check might be using return it will work .but i will remove it. \$\endgroup\$ Aug 19, 2016 at 9:23
  • \$\begingroup\$ remove the return(count); line from ISR and add 'volatile' before 'int count' declaration. \$\endgroup\$
    – Mike Spark
    Aug 19, 2016 at 9:24
  • \$\begingroup\$ Also, where do you reset the 'count' variable? \$\endgroup\$
    – Mike Spark
    Aug 19, 2016 at 9:30
  • \$\begingroup\$ still doesnt work .i am not resetting the count variable.is this the reason code not working? \$\endgroup\$ Aug 19, 2016 at 9:35

1 Answer 1

2
\$\begingroup\$

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.
\$\endgroup\$
2
  • \$\begingroup\$ I did some research and used external xtal of 32.7khz and setting the flag bit accordingly. now it is working properly. \$\endgroup\$ Sep 9, 2016 at 11:43
  • \$\begingroup\$ I was in need of the minimum possible power and power down mode is fulfilling my need <1μA. now its working thankyou \$\endgroup\$ Sep 9, 2016 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.