Created
October 9, 2014 13:27
-
-
Save joefutrelle/61d7c359ee29f6138c3a to your computer and use it in GitHub Desktop.
ATtiny85 blinker using watchdog timer interrupt
This file contains 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 <avr/interrupt.h> | |
#include <avr/wdt.h> | |
#include <avr/sleep.h> | |
#include <util/delay.h> | |
// cribbing from http://electronics.stackexchange.com/a/74850 | |
void prevent_wdt_reset() { | |
if(MCUSR & _BV(WDRF)){ // If a reset was caused by the Watchdog Timer... | |
MCUSR &= ~_BV(WDRF); // Clear the WDT reset flag | |
WDTCR |= (_BV(WDCE) | _BV(WDE)); // Enable the WD Change Bit | |
WDTCR = 0x00; // Disable the WDT | |
} | |
} | |
void setup_wdt() { | |
cli(); // with global interrupts disabled, | |
// Set up Watch Dog Timer for Inactivity | |
WDTCR |= (_BV(WDCE) | _BV(WDE)); // Enable the WD Change Bit | |
WDTCR = _BV(WDIE) | // Enable WDT Interrupt | |
_BV(WDP2) | _BV(WDP1); // Set Timeout to ~1 seconds (or something) | |
sei(); // now re-enable global interrupts | |
} | |
#define FLED 0 | |
volatile uint8_t FLAGS = 0x00; | |
// ISR for watchdog timer just toggles a bit for now | |
ISR(WDT_vect) { | |
FLAGS ^= _BV(FLED); | |
} | |
int main(void) { | |
// first things first | |
prevent_wdt_reset(); | |
// now re-set up WDT | |
setup_wdt(); | |
DDRB |= 1 << PB0; | |
for(;;) { | |
// turn off LED | |
PORTB &= ~_BV(PB0); | |
// now set it to state of led flag | |
PORTB |= ((FLAGS >> FLED) & 1) << PB0; | |
_delay_ms(10); | |
} | |
return 0; // never reached | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment