0

我创建了一个草图,它使用每八秒运行一次的看门狗定时器。我用计数器等待了 64 秒(大约一分钟)。

此外,我在 XBee 上使用了 pin 休眠模式。XBee 通信与 pin 休眠一起正常工作。如果我添加看门狗计时器,它将停止工作。程序是否从看门狗中断前执行的最后一行重新开始?

#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();
    }
}
4

1 回答 1

0

enterSleep() needs to be outside the if statement. Your current implementation spins in the loop(), waits for 8 watchdog timeouts, executes the code once, sleeps, wakes up and does the same spin. The code should look like this I believe.

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();
}

Also, be aware that the watchdog is still running while your delays occur. It may trigger again during your if statement, you should issue a wdt_reset() in a few places like:

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();
}
于 2013-06-28T20:45:38.757 回答