Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement