Arduino看门狗的使用方法

本文介绍了如何在Arduino UNO上使用看门狗定时器来防止程序死锁,并提供了具体的代码示例。此外,还讨论了如何利用看门狗定时器实现Arduino在省电模式下的唤醒。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Arduino UNO的看门狗:

    介绍:看门狗定时器用避免电子设备出现故障造成死机,例如死循环。为此,看门狗作为一个物理定时器,在程序运行时不断的增加,并驱动一个中断, 如果达到某一个设定的值,就会复位微处理器。在正常操作下,定时器在程序流程中的定期设置为0, 但如果代码进入死循环,定时器复位不会发生,看门狗触发中断并重新启动。

    使用看门狗防止死机:Arduino为用户提供了方便操作的函数和宏,可以直接引入库:

#include <avr/wdt.h>

    这个函数库提供了三个API函数供我们使用:

wdt_enable(timeout) //看门狗使能并设置超时时间
wdt_disable() //关闭
wdt_reset() //复位

timeout可以使用wdt.h文件中定义的宏常量,也可以自定义时间;

通常情况下,需要在setup()函数中使能看门狗定时器,并在loop()中不断的喂狗防止溢出,官方给出的示例代码如下:

#include <avr/wdt.h>
void setup()
{
   pinMode(13,OUTPUT);
   wdt_enable(WDTO_2S); //开启看门狗,并设置溢出时间为两秒
}
 
void loop()
{
   digitalWrite(13,HIGH);
   delay(100);
   digitalWrite(13,LOW);
   delay(100);
   wdt_reset(); //喂狗操作,使看门狗定时器复位
}

注:看门狗也可以唤醒省电模式下的 Arduino, 详细说明可查阅官方文档,特别指出的是,如果使用POWER_MODE_IDLE模式(UART、SPI、timer等处于工作状态),需在进入休眠状态前关闭看门狗功能,休眠结束之后再次启动,否则几秒之后看门狗会强制重启Aruino, 休眠失败。示例代码:

/* Sleep Demo Serial
 * -----------------
 * Example code to demonstrate the sleep functions in a Arduino. Arduino will wake up
 * when new data is received in the serial port USART
 * Based on Sleep Demo Serial from http://www.arduino.cc/playground/Learning/ArduinoSleepCode
 *
 * Copyright © 2006 MacSimski 2006-12-30
 * Copyright © 2007 D. Cuartielles 2007-07-08 - Mexico DF
 *
 * With modifications from Ruben Laguna 2008-10-15
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <avr/power.h>
#include <avr/sleep.h>

int sleepStatus = 0; // variable to store a request for sleep
int count = 0; // counter

void setup()
{

Serial.begin(9600);
}

void sleepNow()
{
/* Now is the time to set the sleep mode. In the Atmega8 datasheet
 * http://www.atmel.com/dyn/resources/prod_documents/doc2486.pdf on page 35
 * there is a list of sleep modes which explains which clocks and
 * wake up sources are available in which sleep modus.
 *
 * In the avr/sleep.h file, the call names of these sleep modus are to be found:
 *
 * The 5 different modes are:
 * SLEEP_MODE_IDLE -the least power savings
 * SLEEP_MODE_ADC
 * SLEEP_MODE_PWR_SAVE
 * SLEEP_MODE_STANDBY
 * SLEEP_MODE_PWR_DOWN -the most power savings
 *
 * the power reduction management <avr/power.h> is described in
 * http://www.nongnu.org/avr-libc/user-manual/group_avr_power.html
 */

set_sleep_mode(SLEEP_MODE_IDLE); // sleep mode is set here

sleep_enable(); // enables the sleep bit in the mcucr register
// so sleep is possible. just a safety pin

power_adc_disable();
power_spi_disable();
power_timer0_disable();
power_timer1_disable();
power_timer2_disable();
power_twi_disable();
wdt_disable(); //close wdt before Arduino enter sleep mode

sleep_mode(); // here the device is actually put to sleep!!

// THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP
sleep_disable(); // first thing after waking from sleep:
// disable sleep...
wdt_enable(timeout); //open wdt after wakine from sleep
power_all_enable();

}

void loop()
{
// display information about the counter
Serial.print("Awake for ");
Serial.print(count);
Serial.println("sec");
count++;
delay(1000); // waits for a second

// compute the serial input
if (Serial.available()) {
int val = Serial.read();
if (val  'S') {
      Serial.println("Serial: Entering Sleep mode");
      delay(100);     // this delay is needed, the sleep
                      //function will provoke a Serial error otherwise!!
      count = 0;
      sleepNow();     // sleep function called here
    }
    if (val  'A') {
Serial.println("Hola Caracola"); // classic dummy message
}
}

// check if it should go asleep because of time
if (count >= 10) {
Serial.println("Timer: Entering Sleep mode");
delay(100); // this delay is needed, the sleep
//function will provoke a Serial error otherwise!!
count = 0;
sleepNow(); // sleep function called here
}
}


<think>好的,我现在要处理用户关于Arduino看门狗使用教程和实现方法的查询。首先,我需要确认用户的需求是什么。看门狗(Watchdog Timer,WDT)在嵌入式系统中用于检测和恢复系统故障,当系统卡死时自动重启。用户可能希望了解如何在Arduino中配置和使用看门狗,可能涉及库的选择、代码示例以及注意事项。 接下来,我需要回忆Arduino常用的看门狗库。常见的库包括Avr库自带的<avr/wdt.h>和第三方库如Adafruit_SleepyDog。根据用户提供的引用,Adafruit_SleepyDog被提到过,所以应该重点介绍这个库的使用方法,同时也可以提到原生的方法。 然后,我需要考虑步骤分解。通常教程会包括安装库、包含头文件、初始化看门狗、喂狗操作以及示例代码。用户可能需要具体的代码示例来理解如何实现。此外,还要注意不同Arduino板型的兼容性,比如AVR和ARM架构的区别,以及可能的延时时间限制。 另外,用户可能对看门狗的工作原理不太清楚,需要简要解释其机制,比如计数器重置和系统重启的条件。同时,注意事项也很重要,比如喂狗的时间间隔不能超过看门狗的超时时间,否则会导致意外重启。 还要检查是否有相关的引用内容需要整合。用户提供的引用[1]提到了上传ArduinoISP程序,可能与看门狗无关,但引用[2]提到了Adafruit_SleepyDog库,所以需要确保正确引用该库的项目地址和使用方法。 最后,生成相关问题,帮助用户深入理解,比如看门狗的作用、不同库的区别、超时时间设置和调试技巧。确保所有LaTeX格式正确,行内公式用$...$,独立公式用$$...$$,但在这个问题中可能涉及较少数学公式,但代码部分需要用正确的格式展示。 总结步骤: 1. 介绍看门狗的功能和用途。 2. 分点说明使用Adafruit_SleepyDog库的步骤,包括安装、代码结构、初始化和喂狗。 3. 提供原生AVR库的使用方法,指出其复杂性。 4. 注意事项,如时间限制、架构差异。 5. 代码示例展示基本用法和休眠功能。 6. 相关问题生成,引导用户进一步学习。 需要验证Adafruit_SleepyDog的安装方法是否正确,以及代码示例是否有误。同时,确保引用的正确性,特别是引用[2]的项目地址是否正确。可能还需要提醒用户根据板型选择适当的方法,并注意不同板型的超时时间限制。</think>### Arduino看门狗使用教程及实现方法 #### 一、看门狗功能简介 看门狗(Watchdog Timer,WDT)是嵌入式系统的“保险机制”,通过定时器监控程序运行状态。若程序未按时“喂狗”(重置计数器),看门狗会强制重启系统以恢复异常[^2]。 --- #### 二、使用Adafruit_SleepyDog库(推荐) 1. **安装库** - 通过Arduino IDE库管理器搜索`Adafruit SleepyDog`并安装,或手动下载[项目地址](https://gitcode.com/gh_mirrors/ad/Adafruit_SleepyDog)[^2]。 2. **基础用法** ```cpp #include <Adafruit_SleepyDog.h> void setup() { Watchdog.enable(4000); // 启用看门狗,超时4秒 } void loop() { // 喂狗操作 Watchdog.reset(); delay(1000); // 间隔需小于超时时间 } ``` - `Watchdog.enable(ms)`:设置超时时间(AVR芯片上限8秒,ARM芯片可更长)。 - `Watchdog.reset()`:重置看门狗计数器。 3. **低功耗休眠** ```cpp int sleepTime = Watchdog.sleep(2000); // 休眠2秒,自动喂狗 ``` --- #### 三、原生AVR库方法(进阶) ```cpp #include <avr/wdt.h> void setup() { wdt_disable(); // 先关闭看门狗 wdt_enable(WDTO_4S); // 设置4秒超时 } void loop() { wdt_reset(); // 喂狗 delay(1000); } ``` - **注意**:直接操作寄存器需谨慎,错误使用可能导致系统不稳定。 --- #### 四、注意事项 1. **超时限制** - AVR芯片(如Uno):最大8秒(`WDTO_8S`) - ARM芯片(如Due):支持更长超时(如`Watchdog.enable(10000)`) 2. **喂狗间隔** 喂狗操作必须**早于超时时间**,否则触发重启。 3. **调试技巧** - 添加串口输出观察重启次数: ```cpp void setup() { Serial.begin(9600); Serial.println("System rebooted!"); } ``` --- #### 五、完整示例 ```cpp #include <Adafruit_SleepyDog.h> void setup() { Serial.begin(9600); Watchdog.enable(8000); // 8秒超时 } void loop() { Serial.println("Working..."); Watchdog.reset(); // 喂狗 delay(5000); // 5秒循环(需小于8秒) } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值