Atmega168 - Sleep mode

Guest #1317062
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
        }
}
Guest #1317297
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
Guest #1317398
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

Reply

A question of your own? This thread is over 17 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.