0

I was reading the ATMega34u2 datasheet and trying to figure out Sleep Mode. I know most people recommend using avr/sleep.h library. But I wanted to try out using the registers. I am fairly new to all this so sorry if this an easy question but I can't seem to really find anybody doing it like this.

When checking which registers to adjust it says this:

The SE bit must be written to logic one to make the MCU enter the sleep mode when the SLEEP instruction is executed. To avoid the MCU entering the sleep mode unless it is the programmer’s purpose, it is recommended to write the Sleep Enable (SE) bit to one just before the execution of the SLEEP instruction and to clear it immediately after waking up.

Now I understand setting the bit but what exactly is the sleep instruction? How do I call it in my arduino program?

1
  • google ATMega34u2 sleep mode assembly language
    – jsotola
    Nov 26, 2019 at 21:46

1 Answer 1

2

SLEEP is an assembly instruction. You can execute assembly with GCC's Inline Assembly facility.

In short:

asm volatile("SLEEP");

will do what you want.

The avr/sleep.h file has sleep_cpu() defined as:

#define sleep_cpu()                              \
do {                                             \
  __asm__ __volatile__ ( "sleep" "\n\t" :: );    \
} while(0)

Which is essentially the same thing - just wrapped in a block to isolate it from the rest of your code somewhat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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