Created
May 26, 2021 16:32
-
-
Save kristianlm/fe2f85c0ad193e85fc72c5b9a45c8c38 to your computer and use it in GitHub Desktop.
My atmega328 and attiny85 avr-gcc snippet collection
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
// prescaler timings: | |
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms | |
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec | |
void watchdog_init(uint8_t prescaler) { | |
MCUSR &= ~(1<<WDRF); // <-- reset wdt | |
WDTCSR |= (1<<WDCE) | (1<<WDE); // <-- start timed sequence | |
WDTCSR = (1<<WDCE) | (1<<WDIE) | | |
((prescaler>>3) & 1)<<WDP3 | | |
((prescaler>>2) & 1)<<WDP2 | | |
((prescaler>>1) & 1)<<WDP1 | | |
((prescaler>>0) & 1)<<WDP0; | |
} | |
// system wakes up when watchdog is timed out | |
// obs: make sure you setup the watchdog before calling this! | |
void watchdog_sleep() { | |
ADCSRA = 0; // switch Analog to Digitalconverter OFF | |
set_sleep_mode(SLEEP_MODE_PWR_DOWN); | |
cli(); // timed sequence follows | |
sleep_enable(); | |
// turn off brown-out enable in software | |
// MCUCR = 1<<(BODS) | 1<<(BODSE); // turn on brown-out enable select | |
// MCUCR = 1<<(BODS); // this must be done within 4 clock cycles of above | |
sei(); // guarantees next instruction executed | |
sleep_cpu (); // sleep within 3 clock cycles of above | |
sleep_disable(); // System continues execution here when watchdog timed out | |
ADCSRA = 1; // switch Analog to Digitalconverter ON | |
} | |
void watchdog_powerdown(uint8_t prescaler) { | |
watchdog_init(prescaler); | |
watchdog_sleep(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment