我创建了一个sketch,它使用一个每8秒运行一次的看门狗定时器。我用了一个计数器等了64秒(大约一分钟)。
另外,我在XBee上使用了引脚休眠模式。 XBee通信与引脚休眠一起正常工作。如果我添加watchdog timer,它就会停止工作。程序是否从看门狗中断之前执行的最后一行重新启动?
#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 pin wakes up the XBee and puts 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);
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 without any 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();
}
}
答案 0 :(得分:0)
enterSleep()需要在if语句之外。您当前的实现在循环()中旋转,等待8个看门狗超时,执行一次代码,休眠,唤醒并执行相同的旋转。我认为代码看起来应该是这样的。
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();
}
另外,请注意,在发生延迟时,看门狗仍在运行。它可能会在你的if语句中再次触发,你应该在以下几个地方发出一个wdt_reset():
void loop()
{
if(counter == 8)
{
counter = 0;
pinMode(XBee_pin, OUTPUT);
digitalWrite(XBee_pin, LOW);
delay(5000);
wdt_reset();
sendData();
wdt_reset();
delay(2000);
pinMode(XBee_pin, INPUT);
digitalWrite(XBee_pin, HIGH);
}
/* Re-enter sleep mode. */
enterSleep();
}