AntonioB

Arduino low power

Jul 22nd, 2014
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include <avr/sleep.h>
  2. const byte LED = 9;
  3. /*Riattiva ARDUINO*/  
  4. void wake ()
  5. {
  6.   // cancello lo sleep mode
  7.   sleep_disable();
  8.   //disabilito l agestione dell'interapt
  9.   detachInterrupt (0);
  10. }  
  11. /*Setup*/
  12. void setup ()
  13.   {
  14.   digitalWrite (2, HIGH);  // enable pull-up
  15.   }  
  16. /*Loop*/
  17. void loop ()
  18. {
  19.  /*al primo ciclo accendo il led*/
  20.   pinMode (LED, OUTPUT);
  21.   digitalWrite (LED, HIGH);
  22.   delay (550);
  23.   digitalWrite (LED, LOW);
  24.   delay (50);
  25.   pinMode (LED, INPUT);
  26.   // disabilito ADC
  27.   ADCSRA = 0;  
  28.   set_sleep_mode (SLEEP_MODE_PWR_DOWN);
  29.   sleep_enable();
  30.   // Do not interrupt before we go to sleep, or the
  31.   // ISR will detach interrupts and we won't wake.
  32.   noInterrupts ();
  33.   // prendo l'eveno, interapt quando D2 diventa LOWW  
  34.   attachInterrupt (0, wake, LOW);
  35.   // turn off brown-out enable in software
  36.   // BODS must be set to one and BODSE must be set to zero within four clock cycles
  37.   MCUCR = bit (BODS) | bit (BODSE);
  38.   // The BODS bit is automatically cleared after three clock cycles
  39.   MCUCR = bit (BODS);
  40.   /*si vuole garantire che la cpu venga spenta solo
  41.   la successiva istruzione dell'attivazione di arduino*/
  42.   /*Fà PARTIRE DI NUOVO IL LOOP*/
  43.   interrupts ();  // one cycle
  44.   sleep_cpu ();   // one cycle
  45.  
  46.   } // end of loop
Advertisement
Add Comment
Please, Sign In to add comment