74 lines
1.9 KiB
C++
Executable File
74 lines
1.9 KiB
C++
Executable File
//Waking from sleep with a timer
|
|
//
|
|
//
|
|
//Well, this sleeping as all very well, but a processor that stays asleep isn't
|
|
//particularly useful. Although, I have seen an example of that: the TV Begone
|
|
//remote control. What that does is send out some codes (to turn TVs off) and then
|
|
//goes to sleep permanently. The "activate" button on the gadget is the reset button.
|
|
//When you press that it does its stuff again.
|
|
//
|
|
//Meanwhile this sketch below shows how you can use the watchdog timer to sleep
|
|
//for 8 seconds (the maximum you can set up a watchdog for) and then flash the
|
|
//LED 10 times, and go back to sleep. Whilst asleep it uses about 6.54 µA of current,
|
|
//so presumably the watchdog timer has a bit of an overhead (like, 6.2 µA).
|
|
|
|
|
|
#include <avr/sleep.h>
|
|
#include <avr/wdt.h>
|
|
|
|
const byte LED = 9;
|
|
|
|
void flash ()
|
|
{
|
|
pinMode (LED, OUTPUT);
|
|
for (byte i = 0; i < 10; i++)
|
|
{
|
|
digitalWrite (LED, HIGH);
|
|
delay (50);
|
|
digitalWrite (LED, LOW);
|
|
delay (50);
|
|
}
|
|
|
|
pinMode (LED, INPUT);
|
|
|
|
} // end of flash
|
|
|
|
// watchdog interrupt
|
|
ISR (WDT_vect)
|
|
{
|
|
wdt_disable(); // disable watchdog
|
|
} // end of WDT_vect
|
|
|
|
void setup () { }
|
|
|
|
void loop ()
|
|
{
|
|
|
|
flash ();
|
|
|
|
// disable ADC
|
|
ADCSRA = 0;
|
|
|
|
// clear various "reset" flags
|
|
MCUSR = 0;
|
|
// allow changes, disable reset
|
|
WDTCSR = bit (WDCE) | bit (WDE);
|
|
// set interrupt mode and an interval
|
|
WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 seconds delay
|
|
wdt_reset(); // pat the dog
|
|
|
|
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
|
|
noInterrupts (); // timed sequence follows
|
|
sleep_enable();
|
|
|
|
// turn off brown-out enable in software
|
|
MCUCR = bit (BODS) | bit (BODSE);
|
|
MCUCR = bit (BODS);
|
|
interrupts (); // guarantees next instruction executed
|
|
sleep_cpu ();
|
|
|
|
// cancel sleep as a precaution
|
|
sleep_disable();
|
|
|
|
} // end of loop
|