Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <avr/interrupt.h>
  2. #include <avr/sleep.h>
  3.  
  4. #define LED PB0
  5. #define LED_POWER 100 // From 0 to 255
  6.  
  7. volatile uint8_t flag = 0;
  8.  
  9. // See Table 9-3. Watchdog Timer Prescale Select in ATtiny9 datasheet for Watchdog Timeout values
  10. ISR(WDT_vect) {
  11. // Toggle Port B pin 0 output state
  12. if(flag) {
  13. // Set the LED pin to output
  14. DDRB = (1<<LED);
  15. // Set the watchdog to wake up in 1 second
  16. WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP1);
  17. } else {
  18. // Set the LED pin to input (disable the LED)
  19. DDRB = 0;
  20. // Set the watchdog to wake up in 0.5 second
  21. WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP0);
  22. }
  23. flag = !flag;
  24. }
  25.  
  26. void main() {
  27. // Set up Port B as Input and set the LED pin to output
  28. DDRB = 0 | (1<<DDB0);
  29.  
  30. // Setup comparator to work in simple PWM mode
  31. TCCR0A |= (1<<WGM00) | (1<<COM0A1);
  32. TCCR0B |= (1<<WGM02) | (1<<CS00);
  33. // Set comparator value to the requested LED_power value
  34. OCR0A = LED_POWER;
  35.  
  36. // Set timer to 0.5s
  37. WDTCSR = (1<<WDIE) | (1<<WDP2) | (1<<WDP0);
  38.  
  39. // Enable global interrupts
  40. sei();
  41.  
  42. set_sleep_mode(SLEEP_MODE_IDLE);
  43.  
  44. while(1) {
  45. // Go to sleep and wait for interrupt...
  46. sleep_mode();
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement