3
\$\begingroup\$

I've seen several code examples of sleeping an AVR where they do this:

while (1) {
    // Business logic goes here

    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_enable();
    sleep_cpu();
    sleep_disable();
}

Now, my question is, why are they setting the sleep mode in the loop? Can't you just do this once during setup and be done with it? Or would something change the sleep mode during runtime? (Ghost in the machine?)

Note: I know the sleep_cpu() needs to be in the while lopp. I am wondering why or why not the line

set_sleep_mode(SLEEP_MODE_PWR_DOWN);

has to be inside the while loop.

\$\endgroup\$
6
\$\begingroup\$

This is useful because it illustrates you can select different sleep modes depeinding on control logic. For instance -- you may be taking an ADC reading and would therefore want to choose a sleep mode that kept that peripheral running but disabled others such as the CPU to reduce noise.

It is not required, but I consider it good practice to have that call right before the sleep instruction so you can ensure you are sleeping in the correct state. It is possible to sleep the source of your wakeup interrupt, resulting in a comatose state.

http://www.nongnu.org/avr-libc/user-manual/group_avr_sleep.html will show some good practices and examples.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you. One part of your answer was interesting though... can you really take ADC readings with the main CPU disabled? \$\endgroup\$ – David Högberg Oct 30 '13 at 15:15
  • 2
    \$\begingroup\$ First, here is a general reference from Atmel on how you can make the most out of your internal ADC: atmel.com/Images/doc4278.pdf Then you should read the more specific application note for the ATTINY ADC noise canceller atmel.com/Images/doc8352.pdf section 2.6 \$\endgroup\$ – HL-SDK Oct 30 '13 at 15:22
  • \$\begingroup\$ I'm not familiar with the ATtiny, but just for general education on atmel's xmega line of chips pretty much every peripheral has it's own clock which can be turned on and off for power reduction reasons. Similarly, they can be configured which interrupts wake it from different levels of sleep and which clocks are running during sleep modes etc. \$\endgroup\$ – NickHalden Oct 30 '13 at 16:19
  • 2
    \$\begingroup\$ @DavidHögberg yes, you can. When the CPU is disabled, the ADC is not and it can still perform conversions. Once it is done, it will generate an interrupt to wake the CPU up. I believe this is normally used to take higher precision ADC measurements because the chip will be generating less noise when the CPU is sleeping. Kind of the same idea as studying late at night after your noisy neighbors in the apartment above have gone to bed. \$\endgroup\$ – alex.forencich Oct 30 '13 at 20:15

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.