简介
本人使用的Arduino uno r3,avr单片机自带看门狗电路
下文引自单片机文档
ATmega48/88/168/328 has an Enhanced Watchdog Timer (WDT). The WDT is a timer counting
cycles of a separate on-chip 128 kHz oscillator. The WDT gives an interrupt or a system reset
when the counter reaches a given time-out value. In normal operation mode, it is required that
the system uses the WDR - Watchdog Timer Reset - instruction to restart the counter before the
time-out value is reached. If the system doesn't restart the counter, an interrupt or system reset
will be issued.
使用
使用看门狗电路也比较容易,只需要三步
1.引用头文件 #include <avr/wdt.h>
2.初始化时调用看门狗初始化wdt_enable(WDTO_2S); WDTO_2S是定义自头文件的常量,用于设置看门狗触发时间。不同单片机支持常数有所区别。详情请阅读参考链接表格。
3 loop循环时喂狗即可 wdt_reset();
完整代码
#include <avr/wdt.h>
const int ledPin = 13; // 定义LED管脚
void setup() {
//初始化LED为输出
pinMode(ledPin, OUTPUT);
//初始化看门狗
wdt_enable(WDTO_1S);
}
void loop()
{
//LED常亮
digitalWrite(ledPin, HIGH);
//喂狗,注释本句查看有没有重启
wdt_reset();
}
参考资料
http://tushev.org/articles/arduino/item/46-arduino-and-watchdog-timer