Arduino 使用看门狗

简介

本人使用的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

http://forum.arduino.cc/index.php?topic=128717.0

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值