Created
September 24, 2017 19:11
-
-
Save maximeborges/2f3096e7cc9cc2fc0b6c14aaaf85a4b5 to your computer and use it in GitHub Desktop.
Blinking Throwies for ATtiny9
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/interrupt.h> | |
#include <avr/sleep.h> | |
#define LED PB0 | |
#define LED_POWER 100 // From 0 to 255 | |
volatile uint8_t flag = 0; | |
// See Table 9-3. Watchdog Timer Prescale Select in ATtiny9 datasheet for Watchdog Timeout values | |
ISR(WDT_vect) { | |
// Toggle Port B pin 0 output state | |
if(flag) { | |
// Set the LED pin to output | |
DDRB = (1<<LED); | |
// Set the watchdog to wake up in 1 second | |
WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP1); | |
} else { | |
// Set the LED pin to input (disable the LED) | |
DDRB = 0; | |
// Set the watchdog to wake up in 0.5 second | |
WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP0); | |
} | |
flag = !flag; | |
} | |
void main() { | |
// Set up Port B as Input and set the LED pin to output | |
DDRB = 0 | (1<<DDB0); | |
// Setup comparator to work in simple PWM mode | |
TCCR0A |= (1<<WGM00) | (1<<COM0A1); | |
TCCR0B |= (1<<WGM02) | (1<<CS00); | |
// Set comparator value to the requested LED_power value | |
OCR0A = LED_POWER; | |
// Set timer to 0.5s | |
WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP0); | |
// Enable global interrupts | |
sei(); | |
set_sleep_mode(SLEEP_MODE_IDLE); | |
while(1) { | |
// Go to sleep and wait for interrupt... | |
sleep_mode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment