EmbDev.net

Forum: µC & Digital Electronics Atmega168 - Sleep mode


von Jochen Spieker (Guest)


Rate this post
useful
not useful
Hi,
i have got some problems with my arduino that is equipped with an atmega 
168.
I want to put the atmega into sleep mode for x milliseconds. The wakeup 
is realized with a Timer2 interrupt. If I use in the loop function the 
"delay(1)" just for testing purposes the led that is connected to pin 13 
blinks in 5 sec interval. (as it should be)
But if I remove the delay and write the "sleep_now()" instead, the led 
flashes every 10 seconds. Does anybody have an explanation for this 
weird behavior?

Cheers,
Jochen


Here is the code:

#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>


int volatile time=5000; //sleep time
boolean volatile timerEnabled=false;
int volatile  counter = 0;

void sleepNow()         /
    set_sleep_mode(SLEEP_MODE_PWR_SAVE);   // sleep mode is set here

    sleep_enable();          // enables the sleep bit in the mcucr 
register
                             // so sleep is possible. just a safety pin

    sleep_mode();            // here the device is actually put to 
sleep!!
                             // THE PROGRAM CONTINUES FROM HERE AFTER 
WAKING UP

    sleep_disable();         // first thing after waking from sleep:
                             // disable sleep...
}

void setup() {
 // Serial.begin(19200);
  pinMode(13,OUTPUT);
  TCCR2A |= (1 << WGM21); //ctc mode
  OCR2A = 124; //set timer maximum to 124
  TCNT2 = 0;//reset timer
  TCCR2B = 0b00000100; //((1 << CS22)|(1 << CS20));  //prescaler 64
  sei();

}



ISR(TIMER2_COMPA_vect) {

  counter ++;
  if (counter == time) {
     TCCR2A =0; // stop timer compA interrupts
     counter=0;
     timerEnabled=false;
  }

}
/////////////////////////////////////////////////////////////////



void loop() {
 if(timerEnabled==true)
 sleepNow();
 //delay(1100);
  else {
      //Serial.println("TEST");
          timerEnabled=true;
          digitalWrite(13,HIGH);
          delay(10);
          digitalWrite(13,LOW);
          //time=1000;
          TCNT2 = 0;              //reset timer
          TIMSK2 |= (1 << OCIE2A);  //enable timer interrupt
        }
}

von atmegadennis (Guest)


Rate this post
useful
not useful
Hi Jochen,

perhaps, it is the missing bracket here

void sleepNow()

{         /
    set_sleep_mode(SLEEP_MODE_PWR_SAVE);   // sleep mode is set here


so this part of the programm will not work

von Stefan_KM (Guest)


Rate this post
useful
not useful
Right, the bracket is missing.

von Jochen Spieker (Guest)


Rate this post
useful
not useful
Hi guys,
thanks for the reply, but the bracket is definitly not the cause. I have 
deleted it by mistake when i was edinting the code to post it here in 
the forum.
The code compiles without problems.

Cheers
Jochen

von Tom (Guest)


Rate this post
useful
not useful
1) Deactivated ALL OTHER interrupts?? Maybe some pin is catching 
interference?
2) Watchdog disabled?

von Jochen Spieker (Guest)


Rate this post
useful
not useful
Maybe it is the startup time the atmega168 needs to wakeup. Does anybody 
know how much it is?

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.