EmbDev.net

Forum: µC & Digital Electronics Atmega168 - Sleep mode


Author: Jochen Spieker (Guest)
Posted on:

Rate this post
0 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
        }
}

Author: atmegadennis (Guest)
Posted on:

Rate this post
0 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

Author: Stefan_KM (Guest)
Posted on:

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

Author: Jochen Spieker (Guest)
Posted on:

Rate this post
0 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

Author: Tom (Guest)
Posted on:

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

Author: Jochen Spieker (Guest)
Posted on:

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

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.