Skip to content

Instantly share code, notes, and snippets.

@monpetit
Created June 21, 2016 03:33
Show Gist options
  • Select an option

  • Save monpetit/308b7475ba4894216c1fce5fae1bb55c to your computer and use it in GitHub Desktop.

Select an option

Save monpetit/308b7475ba4894216c1fce5fae1bb55c to your computer and use it in GitHub Desktop.
#include <avr/io.h>
#include <util/delay.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/power.h>
ISR(WDT_vect)
{
}
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)
{
/* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
/* Disable the peripherals. */
power_all_disable();
/* 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();
}
void setup()
{
DDRA |= 0xff;
PORTA = 0x00;
DDRB |= 0xff;
PORTB = 0x00;
reset_blink(30);
setup_watchdog_timer();
}
void loop()
{
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