Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
sleep.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 15.11.2013 21:39:28 CET
3 // File: sleep.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __SLEEP_HEADER
12 #define __SLEEP_HEADER
13 
14 #include <Arduino.h>
15 #include <avr/sleep.h>
16 
17 namespace util {
18  void wake_up() {
19  //nothing to do
20  }
21 
22  void sleep(uint8_t const & interrupt_channel, uint8_t const & state = LOW) {
23  uint8_t adcsra_save = ADCSRA;
24  uint8_t prr_save = PRR;
25 
26  attachInterrupt(interrupt_channel, wake_up, state);
27 
28  ADCSRA = 0; // disable ADC
29 
30  PRR = 0b11101111; //diverse power saving settings
31 
32  set_sleep_mode (SLEEP_MODE_PWR_DOWN);
33  sleep_enable();
34 
35  // turn off brown-out enable in software
36  MCUCR = _BV (BODS) | _BV (BODSE);
37  MCUCR = _BV (BODS);
38  sleep_cpu(); // != sleep_mode(); sleep_mode doesn't disable brown-out
39 
40  //sleep here
41 
42  ADCSRA = adcsra_save;
43  PRR = prr_save;
44 
45  sleep_disable();
46  detachInterrupt(interrupt_channel);
47  }
48 }//end namespace util
49 
50 #endif //__SLEEP_HEADER
void wake_up()
Definition: sleep.hpp:18
void sleep(uint8_t const &interrupt_channel, uint8_t const &state=LOW)
Definition: sleep.hpp:22