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

    // controller sleep mode
    set_sleep_mode(SLEEP_MODE_PWR_SAVE); // sleep mode selected
    sleep_enable();  // set SE bit
    sleep_cpu(); // sleep mode activated
        if(count>=1000) //  min delay
            sleep_disable();   // SE bit reset
            print("waking up.....");


    } while (1);

the corresponding ISR is as follows



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

  • \$\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 '16 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\$ – pranjal khanduri Aug 19 '16 at 9:23
  • \$\begingroup\$ remove the return(count); line from ISR and add 'volatile' before 'int count' declaration. \$\endgroup\$ – Mike Spark Aug 19 '16 at 9:24
  • \$\begingroup\$ Also, where do you reset the 'count' variable? \$\endgroup\$ – Mike Spark Aug 19 '16 at 9:30
  • \$\begingroup\$ still doesnt work .i am not resetting the count variable.is this the reason code not working? \$\endgroup\$ – pranjal khanduri Aug 19 '16 at 9:35

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.
  • \$\begingroup\$ I did some research and used external xtal of 32.7khz and setting the flag bit accordingly. now it is working properly. \$\endgroup\$ – pranjal khanduri Sep 9 '16 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\$ – pranjal khanduri Sep 9 '16 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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