Last active
June 24, 2016 02:42
-
-
Save monpetit/86d6ac90e0321cca47e9a31852912e10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <avr/io.h> | |
| #include <util/delay.h> | |
| #include <avr/wdt.h> | |
| #include <avr/sleep.h> | |
| #include <avr/power.h> | |
| #include <avr/interrupt.h> | |
| // Routines to set and claer bits (used in the sleep code) | |
| #ifndef cbi | |
| #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) | |
| #endif | |
| #ifndef sbi | |
| #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) | |
| #endif | |
| // Variables for the Sleep/power down modes: | |
| volatile bool f_wdt = 1; | |
| ISR(WDT_vect) | |
| { | |
| f_wdt = 1; // set global flag | |
| } | |
| void reset_blink(volatile uint8_t countdown) | |
| { | |
| while (countdown--) { | |
| PORTB ^= (1 << 0); | |
| delay(20); | |
| } | |
| PORTB &= ~(1 << 0); | |
| delay(2000); | |
| } | |
| void setup_watchdog_timer(void) | |
| { | |
| cli(); | |
| wdt_reset(); | |
| CCP = 0xd8; | |
| WDTCSR = (1 << WDIF) | (1 << WDIE) | (0 << WDP3) | (0 << WDE) | (1 << WDP2) | (1 << WDP1) | (1 << WDP0); | |
| sei(); | |
| } | |
| void enter_sleep(void) | |
| { | |
| cbi(ADCSRA, ADEN); // switch ADC OFF | |
| /* Disable the peripherals. */ | |
| power_all_disable(); | |
| /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */ | |
| set_sleep_mode(SLEEP_MODE_PWR_DOWN); | |
| sleep_enable(); | |
| /* Now enter sleep mode. */ | |
| sleep_mode(); | |
| /* The program will continue from here after the WDT timeout*/ | |
| sleep_disable(); /* First thing to do is disable sleep. */ | |
| /* Re-enable the peripherals. */ | |
| power_all_enable(); | |
| sbi(ADCSRA, ADEN); // switch ADC ON | |
| } | |
| void setup() | |
| { | |
| DDRA |= 0xff; | |
| PORTA = 0x00; | |
| DDRB |= 0xff; | |
| PORTB = 0x00; | |
| reset_blink(30); | |
| setup_watchdog_timer(); | |
| } | |
| void loop() | |
| { | |
| if (f_wdt) { | |
| f_wdt = 0; | |
| PORTB ^= (1 << 0); | |
| delay(2); | |
| PORTB ^= (1 << 0); | |
| enter_sleep(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment