1
0
mirror of https://github.com/taigrr/arduinolibs synced 2025-01-18 04:33:12 -08:00

Reduce power consumption

This commit is contained in:
Rhys Weatherley 2012-05-21 10:01:43 +10:00
parent 9eeb91b173
commit e627f5642f
6 changed files with 231 additions and 4 deletions

View File

@ -610,7 +610,7 @@ WARN_LOGFILE =
# directories like "/usr/src/myproject". Separate the files or directories
# with spaces.
INPUT = ../libraries/FreetronicsLCD ../libraries/BlinkLED ../libraries/I2C ../libraries/RTC ../libraries/Melody .
INPUT = ../libraries/FreetronicsLCD ../libraries/BlinkLED ../libraries/I2C ../libraries/RTC ../libraries/Melody ../libraries/PowerSave .
# This tag can be used to specify the character encoding of the source files
# that doxygen parses. Internally doxygen uses the UTF-8 encoding, which is

View File

@ -70,4 +70,8 @@ The default implementation simulates the time and date based on the value of
realtime clock and the FreetronicsLCD library to implement an alarm clock
where the internal battery is charged by hand-cranking a dynamo.
\section main_other Other
\li \ref power_save "Power saving utility functions"
*/

View File

@ -0,0 +1,156 @@
/*
* Copyright (C) 2012 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "PowerSave.h"
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/interrupt.h>
/**
* \defgroup power_save Power saving utility functions
*
* The functions in this module assist with reducing power consumption on
* Arduino boards by disabling features that are not used or putting the
* device to sleep when it is inactive.
*/
/*\@{*/
/**
* \fn void unusedPin(uint8_t pin)
* \brief Marks an I/O \a pin as unused.
* \ingroup power_save
*
* This function sets \a pin to be an input with pullups enabled, which will
* reduce power consumption compared to pins that are left floating.
*/
/** @cond */
ISR(WDT_vect)
{
wdt_disable();
}
/** @endcond */
/**
* \enum SleepDuration
* \brief Duration to put the CPU to sleep with sleepFor().
* \ingroup power_save
*
* \sa sleepFor()
*/
/**
* \var SLEEP_15_MS
* \brief Sleep for 15 milliseconds.
* \ingroup power_save
*/
/**
* \var SLEEP_30_MS
* \brief Sleep for 30 milliseconds.
* \ingroup power_save
*/
/**
* \var SLEEP_60_MS
* \brief Sleep for 60 milliseconds.
* \ingroup power_save
*/
/**
* \var SLEEP_120_MS
* \brief Sleep for 120 milliseconds.
* \ingroup power_save
*/
/**
* \var SLEEP_250_MS
* \brief Sleep for 250 milliseconds.
* \ingroup power_save
*/
/**
* \var SLEEP_500_MS
* \brief Sleep for 500 milliseconds.
* \ingroup power_save
*/
/**
* \var SLEEP_1_SEC
* \brief Sleep for 1 second.
* \ingroup power_save
*/
/**
* \var SLEEP_2_SEC
* \brief Sleep for 2 seconds.
* \ingroup power_save
*/
/**
* \var SLEEP_4_SEC
* \brief Sleep for 4 seconds.
* \ingroup power_save
*/
/**
* \var SLEEP_8_SEC
* \brief Sleep for 8 seconds.
* \ingroup power_save
*/
/**
* \brief Puts the CPU to sleep for a specific \a duration.
* \ingroup power_save
*
* The analog to digital converter and the brown out detector will
* be disabled during sleep mode.
*/
void sleepFor(SleepDuration duration)
{
// Turn off the analog to digital converter.
ADCSRA &= ~(1 << ADEN);
power_adc_disable();
// Turn on the watchdog timer for the desired duration.
wdt_enable(duration);
WDTCSR |= (1 << WDIE);
// Put the device to sleep, including turning off the Brown Out Detector.
set_sleep_mode(SLEEP_MODE_IDLE);
cli();
sleep_enable();
#if defined(sleep_bod_disable)
sleep_bod_disable();
#endif
sei();
sleep_cpu();
sleep_disable();
sei();
// Turn the analog to digital converter back on.
power_adc_enable();
ADCSRA |= (1 << ADEN);
}
/*\@}*/

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2012 Southern Storm Software, Pty Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef PowerSave_h
#define PowerSave_h
#include <WProgram.h>
inline void unusedPin(uint8_t pin)
{
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
enum SleepDuration
{
SLEEP_15_MS,
SLEEP_30_MS,
SLEEP_60_MS,
SLEEP_120_MS,
SLEEP_250_MS,
SLEEP_500_MS,
SLEEP_1_SEC,
SLEEP_2_SEC,
SLEEP_4_SEC,
SLEEP_8_SEC
};
void sleepFor(SleepDuration duration);
#endif

View File

@ -0,0 +1,2 @@
unusedPin KEYWORD2
sleepFor KEYWORD2

View File

@ -28,6 +28,7 @@
#include <SoftI2C.h>
#include <DS1307RTC.h>
#include <Melody.h>
#include <PowerSave.h>
#include "FrontScreen.h"
#include "EditTime.h"
@ -68,6 +69,15 @@ EditTime alarm3(mainForm, "Alarm 3");
EditTime alarm4(mainForm, "Alarm 4");
void setup() {
// Reduce power consumption on I/O pins we don't need.
unusedPin(A2);
unusedPin(0);
unusedPin(1);
unusedPin(2);
unusedPin(10);
unusedPin(11);
unusedPin(13);
// Enable the screen saver.
lcd.setScreenSaverMode(FreetronicsLCD::BacklightOnSelect);
lcd.enableScreenSaver(3);
@ -128,7 +138,12 @@ void loop() {
}
// If the alarm is playing and a button was pressed, then turn it off.
if (event != LCD_BUTTON_NONE)
alarmMelody.stop();
alarmMelody.run();
if (alarmMelody.isPlaying()) {
if (event != LCD_BUTTON_NONE)
alarmMelody.stop();
alarmMelody.run();
} else {
// No alarm playing, so put the device to sleep to save power.
sleepFor(SLEEP_15_MS);
}
}