Advertisement
RuiViana

Sleep_WakeUp_Wdt.ino

Jul 19th, 2018
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1.  
  2. // https://github.com/vancegroup-mirrors/avr-libc/tree/master/avr-libc/include/avr
  3. #include <avr/sleep.h>                              // Biblioteca de sleep
  4. #include <avr/power.h>                              // Biblioteca de controle de power
  5. #include <avr/wdt.h>                                // Biblioteca de watch dog timer
  6. #define ledPin (13)                                 // Port para o LED
  7. volatile int flagWdt = 0;                           // Flag de controle do sleep
  8. int vezes = 3;                                      // Numero de vezes para dormir
  9. //------------------------------------------------------
  10. ISR(WDT_vect)                                       // Rotina de interrupt do watch dog
  11. {
  12.   if (flagWdt < vezes)                              // Se o flag esta habiltado
  13.   {
  14.     flagWdt++;                                      // Desabilita o flag permite entrar em sleep
  15.   }
  16. }
  17. //------------------------------------------------------
  18. void sleepMode(void)                                // Rotina de sleep
  19. {
  20.   set_sleep_mode(SLEEP_MODE_PWR_SAVE);              // Sleep economizando energia
  21.   sleep_enable();                                   // Habilita sleep
  22.   sleep_mode();                                     // Entra em sleep mode
  23.   sleep_disable();                                  // Desabilita sleep
  24.   power_all_enable();                               // Religa perifiericos
  25. }
  26. //------------------------------------------------------
  27. void setup()
  28. {
  29.   Serial.begin(9600);                               // Inicializa a serial
  30.   Serial.println("Inicializando wtd");              // Imprime
  31.   pinMode(ledPin, OUTPUT);                          // Define port como saida
  32.   MCUSR &= ~(1 << WDRF);                            // MCUSR Status Register
  33.   //                                                // WDRF: Watchdog System Reset Flag Limpa o flag
  34.   WDTCSR |= (1 << WDCE) | (1 << WDE);               // WDTCSR – Watchdog Timer Control Register
  35.   //                                                // WDCE: Watchdog Change Enable
  36.   //                                                // WDE: Watchdog System Reset Enable
  37.   WDTCSR = 1 << WDP0 | 1 << WDP3;                   // WDP[3:0]: Watchdog Timer Prescaler 3, 2, 1 and 0  8.0 seconds
  38.   WDTCSR |= _BV(WDIE);                              // WDIE: Watchdog Interrupt Enable
  39.   Serial.println("Inicializacao completa.");        // Imprime
  40. }
  41. //------------------------------------------------------
  42. void loop()
  43. {
  44.   if (flagWdt < vezes)                              // Se o flag estΓ‘ desligado e o arduino esta acordado
  45.   {
  46.     Serial.println("WDT acordou o arduino ");       // Imprime  
  47.     digitalWrite(ledPin, !digitalRead(ledPin));     // Inverte led
  48.     Serial.println("Arduino vai dormir novamente"); // Imprime                                                  
  49.     delay(100);                                     // Aguarda fim da impresao antes de dormir
  50.     sleepMode();                                    // Reentra em sleep
  51.   }
  52.   else
  53.   {
  54.     Serial.println("Arduino acordado ");            // Imprime                            
  55.     digitalWrite(ledPin, !digitalRead(ledPin));     // Inverte led
  56.     delay(100);                                     // Tempo de piscsr o LED
  57.   }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement