first commit
This commit is contained in:
65
ATMEGA_SLEEP_ADC/ATMEGA_SLEEP_ADC.ino
Executable file
65
ATMEGA_SLEEP_ADC/ATMEGA_SLEEP_ADC.ino
Executable file
@@ -0,0 +1,65 @@
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/power.h> // Power management
|
||||
#include <avr/wdt.h>
|
||||
|
||||
const byte LED = 13;
|
||||
|
||||
// watchdog interrupt
|
||||
ISR (WDT_vect)
|
||||
{
|
||||
wdt_disable(); // disable watchdog
|
||||
} // end of WDT_vect
|
||||
|
||||
void myWatchdogEnable()
|
||||
{
|
||||
// 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
|
||||
|
||||
byte old_ADCSRA = ADCSRA;
|
||||
// disable ADC to save power
|
||||
ADCSRA = 0;
|
||||
|
||||
// slow clock to divide by 256
|
||||
// clock_prescale_set (clock_div_256);
|
||||
|
||||
// ready to sleep
|
||||
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
|
||||
sleep_enable();
|
||||
|
||||
// turn off brown-out enable in software
|
||||
MCUCR = bit (BODS) | bit (BODSE);
|
||||
MCUCR = bit (BODS);
|
||||
sleep_cpu ();
|
||||
|
||||
// cancel sleep as a precaution
|
||||
sleep_disable();
|
||||
power_all_enable (); // enable modules again
|
||||
// reenable ADC
|
||||
ADCSRA = old_ADCSRA;
|
||||
}
|
||||
|
||||
|
||||
void setup () {
|
||||
Serial.begin(9600);
|
||||
}
|
||||
|
||||
void loop ()
|
||||
{
|
||||
|
||||
pinMode (LED, OUTPUT);
|
||||
digitalWrite (LED, HIGH);
|
||||
pinMode (LED, INPUT);
|
||||
delay (100);
|
||||
digitalWrite (LED, LOW);
|
||||
delay(10);
|
||||
// sleep for a total of n x 8 seconds
|
||||
for (int i = 0; i < 8; i++) {
|
||||
myWatchdogEnable ();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user