From 18082ba49d6bc11e2812f2d0611b27558c49ec78 Mon Sep 17 00:00:00 2001 From: LowPowerLab Date: Mon, 21 Mar 2016 21:29:51 -0400 Subject: [PATCH] Add DeepSleep sketch --- Examples/DeepSleep/DeepSleep.ino | 90 ++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 Examples/DeepSleep/DeepSleep.ino diff --git a/Examples/DeepSleep/DeepSleep.ino b/Examples/DeepSleep/DeepSleep.ino new file mode 100644 index 0000000..46be191 --- /dev/null +++ b/Examples/DeepSleep/DeepSleep.ino @@ -0,0 +1,90 @@ +//*********************************************************************************************************** +// Sample sketch that achieves the lowest power on a Moteino of ~6.5uA +// Everything is put to sleep including the MCU, the radio (if any) and the FlashMem chip +//**** SETTINGS ********************************************************************************************* +#define WITH_RFM69 //comment this line out if you don't have a RFM69 on your Moteino +#define WITH_SPIFLASH //comment this line out if you don't have the FLASH-MEM chip on your Moteino +//*********************************************************************************************************** + +#include // assumes Arduino IDE v1.0 or greater +#include +#include +#include + +#ifdef __AVR_ATmega1284P__ + #define LED 15 // Moteino MEGAs have LEDs on D15 + #define FLASH_SS 23 // and FLASH SS on D23 +#else + #define LED 9 // Moteinos have LEDs on D9 + #define FLASH_SS 8 // and FLASH SS on D8 +#endif + +#if defined(WITH_RFM69) || defined(WITH_SPIFLASH) + #include //comes with Arduino IDE (www.arduino.cc) + #if defined(WITH_RFM69) + #include //get it here: https://www.github.com/lowpowerlab/rfm69 + RFM69 radio; + #define NETWORKID 100 + #define NODEID 123 + #define FREQUENCY RF69_915MHZ + #endif + #if defined(WITH_SPIFLASH) + #include //get it here: https://www.github.com/lowpowerlab/spiflash + SPIFlash flash(FLASH_SS, 0xEF30); //EF30 for 4mbit Windbond chip (W25X40CL) + #endif +#endif + +//watchdog interrupt +ISR (WDT_vect) { + wdt_disable(); +} + +void setup () { + for (uint8_t i=0; i<=A5; i++) + { + pinMode(i, OUTPUT); + digitalWrite(i, LOW); + } + +#if defined(WITH_RFM69) + radio.initialize(FREQUENCY,NODEID,NETWORKID); + radio.sleep(); +#endif + +#if defined(WITH_SPIFLASH) + if (flash.initialize()) + flash.sleep(); +#endif + + power_timer1_disable(); + power_timer2_disable(); + power_twi_disable(); +} + +void loop () +{ + // 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 + // BODS must be set to one and BODSE must be set to zero within four clock cycles + MCUCR = bit (BODS) | bit (BODSE); + // The BODS bit is automatically cleared after three clock cycles + MCUCR = bit (BODS); + interrupts(); + sleep_cpu(); + + //cancel sleep as a precaution + sleep_disable(); +} \ No newline at end of file