first commit
This commit is contained in:
69
WatchDog/WATCHDOG.ino
Executable file
69
WatchDog/WATCHDOG.ino
Executable file
@@ -0,0 +1,69 @@
|
||||
//Optimisation de la consommation
|
||||
#include <avr/power.h>
|
||||
#include <avr/sleep.h>
|
||||
#include <avr/power.h>
|
||||
#include <avr/wdt.h>
|
||||
volatile int f_wdt=1;
|
||||
|
||||
// Watchdog Interrupt Service est exécité lors d'un timeout du WDT
|
||||
ISR(WDT_vect) {
|
||||
if(f_wdt == 0) {
|
||||
f_wdt = 1; // flag global
|
||||
}
|
||||
}
|
||||
|
||||
// paramètre : 0=16ms, 1=32ms, 2=64ms, 3=128ms, 4=250ms, 5=500ms, 6=1 sec,7=2 sec, 8=4 sec, 9=8 sec
|
||||
void setup_watchdog(int ii) {
|
||||
byte bb;
|
||||
int ww;
|
||||
if (ii > 9 ) ii=9;
|
||||
bb=ii & 7;
|
||||
if (ii > 7) bb|= (1<<5);
|
||||
bb|= (1<<WDCE);
|
||||
ww=bb;
|
||||
// Clear the reset flag
|
||||
MCUSR &= ~(1<<WDRF);
|
||||
// start timed sequence
|
||||
WDTCSR |= (1<<WDCE) | (1<<WDE);
|
||||
// set new watchdog timeout value
|
||||
WDTCSR = bb;
|
||||
WDTCSR |= _BV(WDIE);
|
||||
}
|
||||
|
||||
void enterSleep(void) {
|
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
|
||||
sleep_enable();
|
||||
sleep_mode(); //Entre dans le mode veille choisi
|
||||
|
||||
// Le programme va reprendre ici après le timeout du WDT
|
||||
|
||||
sleep_disable(); // La 1ère chose à faire est de désactiver le mode veille
|
||||
}
|
||||
|
||||
//************* SETUP *************
|
||||
void setup() {
|
||||
//Optimisation de la consommation
|
||||
//power_adc_disable(); // Convertisseur Analog / Digital pour les entrées analogiques
|
||||
|
||||
//power_spi_disable();
|
||||
//power_twi_disable();
|
||||
// Si pas besoin de communiquer par l'usb
|
||||
//power_usart0_disable();
|
||||
//Extinction des timers, attention timer0 utilisé par millis ou delay
|
||||
//power_timer0_disable();
|
||||
//power_timer1_disable();
|
||||
//power_timer2_disable();
|
||||
|
||||
setup_watchdog(9);
|
||||
}
|
||||
|
||||
//************* LOOP *************
|
||||
|
||||
void loop() {
|
||||
if (f_wdt == 1) {
|
||||
// Effectuer les mesures ici f_wdt = 0; // Ne pas oublier d'initialiser le flag
|
||||
enterSleep(); //Revenir en mode veille
|
||||
} else {
|
||||
/* Do nothing. */
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user