Advertisement
petriojk

Interrupts

Feb 16th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.53 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <avr/sleep.h>
  3. #include <avr/wdt.h>
  4.  
  5. #define LED 13
  6.  
  7. // interrupt service routine for when button pressed
  8. void wake ()                            
  9. {
  10.   wdt_disable();  // disable watchdog
  11. }  // end of wake
  12.  
  13. // watchdog interrupt
  14. ISR (WDT_vect)
  15. {
  16.   wake ();
  17. }  // end of WDT_vect
  18.  
  19. void myWatchdogEnable (const byte interval)
  20. {
  21.   MCUSR = 0;                          // reset various flags
  22.   WDTCSR |= 0b00011000;               // see docs, set WDCE, WDE
  23.   WDTCSR =  0b01000000 | interval;    // set WDIE, and appropriate delay
  24.   wdt_reset();
  25.  
  26.   byte adcsra_save = ADCSRA;
  27.   byte prr_save = PRR;
  28.  
  29.   ADCSRA = 0;  // disable ADC
  30.   PRR = 0xFF; // turn off various modules
  31.   set_sleep_mode (SLEEP_MODE_PWR_DOWN);   // sleep mode is set here
  32.   attachInterrupt (0, wake, FALLING);   // allow grounding pin 2 to wake us
  33.   sleep_enable();
  34.     // turn off brown-out enable in software
  35.   MCUCR = bit (BODS) | bit (BODSE);
  36.   MCUCR = bit (BODS);
  37.   sleep_mode ();            // now goes to Sleep and waits for the interrupt
  38.   detachInterrupt (0);      // stop LOW interrupt
  39.   //sleep_disable();
  40.   ADCSRA = adcsra_save; // stop power reduction
  41.   PRR = prr_save;
  42. }  // end of myWatchdogEnable
  43.  
  44. void blink ()
  45. {
  46.   pinMode (LED, OUTPUT);
  47.   digitalWrite (LED, HIGH);
  48.   delay (1000);
  49.   digitalWrite (LED, LOW);
  50. }
  51.  
  52. void setup ()
  53. {
  54.   digitalWrite (2, HIGH);    // pull-up on button
  55. }  // end of setup
  56.  
  57. void loop()
  58. {
  59.  
  60.   blink();
  61.   myWatchdogEnable (0b100001);  // sleep for 8 seconds
  62.  
  63. }  // end of loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement