Hi, I've created a sketch that uses a watchdog timer that runs each 8s. I've used a counter to wait 64s (about a minute).
In addition I've used the pin hibernation mode on the xBee.
The xBee communication together with the pin hibernation work properly.
If I add the watchdog timer it stops working.
Is the program restarted from the last row executed before the WatchDog interrupt ?
Here it's my sketch:
#include <avr/sleep.h>
#include <avr/power.h>
#include <avr/wdt.h>
#include <XBee.h>
XBeeAddress64 coordAddr = XBeeAddress64(0x0013a200, 0x4090c5a6);
uint8_t xbeePayload[] = "Hello";
XBee xbee = XBee();
ZBTxRequest zbTx = ZBTxRequest();
ZBTxStatusResponse txStatus = ZBTxStatusResponse();
int XBee_pin = 9; // this pins wake up xbee and put it to sleep
int counter = 0;
void sendData() {
zbTx.setAddress64(coordAddr);
zbTx.setAddress16(0xFFFE);
zbTx.setPayload(xbeePayload);
zbTx.setPayloadLength(sizeof(xbeePayload));
xbee.send(zbTx);
}
ISR(WDT_vect)
{
counter++;
}
void enterSleep(void)
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */
sleep_enable();
/* Now enter sleep mode. */
sleep_mode();
/* The program will continue from here after the WDT timeout*/
sleep_disable(); /* First thing to do is disable sleep. */
/* Re-enable the peripherals. */
power_all_enable();
}
void setup()
{
//To reduce power, setup all pins as inputs with no pullups
/*for(int x = 1 ; x < 18 ; x++){
pinMode(x, INPUT);
digitalWrite(x, LOW);
}*/
pinMode(XBee_pin, OUTPUT);
digitalWrite(XBee_pin, HIGH);
xbee.begin(9600);
delay(5000);
/*** Setup the WDT ***/
/* Clear the reset flag. */
MCUSR &= ~(1<<WDRF);
/* In order to change WDE or the prescaler, we need to
* set WDCE (This will allow updates for 4 clock cycles).
*/
WDTCSR |= (1<<WDCE) | (1<<WDE);
/* set new watchdog timeout prescaler value */
WDTCSR = 1<<WDP0 | 1<<WDP3; /* 8.0 seconds */
/* Enable the WD interrupt (note no reset). */
WDTCSR |= _BV(WDIE);
//Serial.println("Initialisation complete.");
//delay(100); //Allow for serial print to complete.
ADCSRA &= ~(1<<ADEN); //Disable ADC
ACSR = (1<<ACD); //Disable Analog Comparator
DIDR0 = 0x3E; //Disable digital input buffers on ADC1-ADC5 pins
DIDR1 = (1<<AIN1D)|(1<<AIN0D); //Disable digital input buffer on AIN1/0
power_twi_disable();
power_spi_disable();
power_usart0_disable();
//power_timer0_disable(); //Needed for delay_ms
power_timer1_disable();
power_timer2_disable();
}
void loop()
{
if(counter == 8)
{
counter = 0;
pinMode(XBee_pin, OUTPUT);
digitalWrite(XBee_pin, LOW);
delay(5000);
sendData();
delay(2000);
pinMode(XBee_pin, INPUT);
digitalWrite(XBee_pin, HIGH);
/* Re-enter sleep mode. */
enterSleep();
}
}