1.需要调用
#include <avr/wdt.h>
2.设置看门狗复位时间
wdt_enable(WDTO_2S);
代码时间定义的底层查看
#define WDTO_15MS 0 /** ingroup avr_watchdog See c WDT0_15MS */ #define WDTO_30MS 1 /** ingroup avr_watchdog See c WDT0_15MS */ #define WDTO_60MS 2 /** ingroup avr_watchdog See c WDT0_15MS */ #define WDTO_120MS 3 /** ingroup avr_watchdog See c WDT0_15MS */ #define WDTO_250MS 4 /** ingroup avr_watchdog See c WDT0_15MS */ #define WDTO_500MS 5 /** ingroup avr_watchdog See c WDT0_15MS */ #define WDTO_1S 6 /** ingroup avr_watchdog See c WDT0_15MS */ #define WDTO_2S 7 if defined(__DOXYGEN__) || defined(WDP3) /** ingroup avr_watchdog See c WDT0_15MS Note: This is only available on the ATtiny2313, ATtiny24, ATtiny44, ATtiny84, ATtiny84A, ATtiny25, ATtiny45, ATtiny85, ATtiny261, ATtiny461, ATtiny861, ATmega48, ATmega88, ATmega168, ATmega48P, ATmega88P, ATmega168P, ATmega328P, ATmega164P, ATmega324P, ATmega644P, ATmega644, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega8HVA, ATmega16HVA, ATmega32HVB, ATmega406, ATmega1284P, ATmega256RFR2, ATmega128RFR2, ATmega64RFR2, ATmega2564RFR2, ATmega1284RFR2, ATmega644RFR2, AT90PWM1, AT90PWM2, AT90PWM2B, AT90PWM3, AT90PWM3B, AT90PWM216, AT90PWM316, AT90PWM81, AT90PWM161, AT90USB82, AT90USB162, AT90USB646, AT90USB647, AT90USB1286, AT90USB1287, ATtiny48, ATtiny88. */ #define WDTO_4S 8 /** ingroup avr_watchdog See c WDT0_15MS Note: This is only available on the ATtiny2313, ATtiny24, ATtiny44, ATtiny84, ATtiny84A, ATtiny25, ATtiny45, ATtiny85, ATtiny261, ATtiny461, ATtiny861, ATmega48, ATmega48A, ATmega48PA, ATmega88, ATmega168, ATmega48P, ATmega88P, ATmega168P, ATmega328P, ATmega164P, ATmega324P, ATmega644P, ATmega644, ATmega640, ATmega1280, ATmega1281, ATmega2560, ATmega2561, ATmega8HVA, ATmega16HVA, ATmega32HVB, ATmega406, ATmega1284P, ATmega256RFR2, ATmega128RFR2, ATmega64RFR2, ATmega2564RFR2, ATmega1284RFR2, ATmega644RFR2, AT90PWM1, AT90PWM2, AT90PWM2B, AT90PWM3, AT90PWM3B, AT90PWM216, AT90PWM316, AT90PWM81, AT90PWM161, AT90USB82, AT90USB162, AT90USB646, AT90USB647, AT90USB1286, AT90USB1287, ATtiny48, ATtiny88, ATxmega16a4u, ATxmega32a4u, ATxmega16c4, ATxmega32c4, ATxmega128c3, ATxmega192c3, ATxmega256c3. */ #define WDTO_8S 9
3.喂狗操作
wdt_reset();
4.关闭看门狗
wdt_disable();
测试代码:
#include <avr/wdt.h> int test = 0; void setup() { Serial.begin(9600); Serial.println("in setup"); Serial.print("test="); Serial.println(test); delay(1000); wdt_enable(WDTO_2S); } void loop() { int testLoop; test++; // wdt_reset(); Serial.println("in loop"); Serial.print("test="); Serial.println(test); delay(1000); Serial.print("testLoop="); Serial.println(testLoop++); }