Reputation: 1147
Currently I'm playing with the avr microprocessor atmega644p and trying to put it in sleep mode (mode: idle).
To do this: I set the SE bit = 1 and other 3 bits SM0 SM1 and SM2 is 0, then when I call function sleep_cpu()
, CPU will be in idle mode.
I use interrupt to execute tasks defined in my code so that if I want to execute it, i have to disable the sleep mode. This mean the function sleep_disable()
must be put in the first line in ISR(vector_name)
function.
Is my logic correct?
And how can I check whether CPU is in sleep mode or not? I tried to do as below:
if (SM0 == 0 && SM1 == 0 && SM2 == 0){
printf("Sleep mode begin");
}
Because I use an interrupt so oviously the if statement would be in the loop in main()
function but nothing is printed on putty terminal.
why this happens? I think this happens because the if statement is never executed due to wrong condition
EDIT
The position of the if statement is above sleep_cpu()
inside the loop in main()
function so that the line "Sleep mode begin" should be printed out before the CPU go to sleep mode, but there is still nothing print out on putty terminal
Upvotes: 0
Views: 1613
Reputation: 8449
At the bottom of pg 41 in this document, is a paragraph that describes how interrupts wake up the processor from sleep modes.
If an enabled interrupt occurs while the MCU is in a sleep mode, the MCU wakes up. The MCU is then halted for four cycles in addition to the start-up time, executes the interrupt routine, and resumes execution from the instruction following SLEEP.
So, you don't need to wake up the processor in your interrupt routine.
However, if you want the processor to go back to sleep, you do have to take extra steps after the ISR returns, like putting the sleep() function inside a loop.
Upvotes: 1