Skip to content

Instantly share code, notes, and snippets.

@hexagon5un
Last active October 23, 2017 14:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hexagon5un/87f6a0be0b67e269d889522de4386122 to your computer and use it in GitHub Desktop.
Save hexagon5un/87f6a0be0b67e269d889522de4386122 to your computer and use it in GitHub Desktop.
Low-power ATtiny45 code for short infrequent pulses
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define LED PB4
#define LED_DDR DDRB
#define LED_PORT PORTB
#define setBit(sfr, bit) (_SFR_BYTE(sfr) |= (1 << bit))
#define clearBit(sfr, bit) (_SFR_BYTE(sfr) &= ~(1 << bit))
ISR(WDT_vect){
setBit(LED_PORT, LED);
// can use _delay_us() here to make brighter
/* _delay_us(1); */
clearBit(LED_PORT, LED);
}
int main(void) {
// Just in case watchdog gets used as reset source, turn it off
wdt_disable();
// init WDT for interrupts, not reset
setBit(WDTCR, WDIE); // interrupt enable
sei();
// Init
setBit(LED_DDR, LED); /* set LED pin for output */
// Turn off all peripherals
power_all_disable();
// Max sleep mode for max power savings
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
// Mainloop
while (1) {
sleep_mode();
}
return 0; /* end mainloop */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment