在 Arduino 中使用 Watchdog 看门狗 Reset 功能

目录 Content
[hide]

Arduino 采用的  Atmega168 或者 Atmega328 单片机,所以也具有看门狗功能,多是用在程序跑死后自动复位 Reset。但是在Arduino上,受 bootloader 影响的关系,并不是所有板子都可以成功使用的。使用 Atmega168 的 Arduino Duemilanove 目前没有测试成功,会发现不停重启,复位也不行,而使用 Atmega328 芯片的Arduino UNO R3 则完全可用。

一、看门狗复位 Watchdog Reset

下面代码在 Arduino UNO R3 上(Atmega328)测试成功,设置超时为WDTO_8S(即8秒,具体数值参考 avr\wdt.h文件里的代码),在主循环里人为延迟9秒,就会触发重启。重启后,LED灯会点亮一次,然后熄灭并激活看门狗,如此往复。如果将delay修改小于8秒,则能够及时喂狗,程序保持一直运行,LED不会被再次点亮。

代码 ( Arduino UNO R3 + IDE arduino-1.0.6 / 1.5.8 测试通过):

#include <avr/wdt.h>
#define TIMEOUT WDTO_8S      // predefine time, refer avr/wdt.h
const int ledPin =  13;      // the number of the LED pin

void setup(){
  // disable the watchdog
  //wdt_disable();
  pinMode(ledPin,OUTPUT);
  // LED light once after start or if timeout
  digitalWrite(ledPin,HIGH);
  delay(1000);
  // enable the watchdog
  wdt_enable(TIMEOUT); 
}

void loop(){
  // process runing  
  digitalWrite(ledPin,LOW); 
  delay(9000); //if timeout trig the reset
  //feed dog 
  wdt_reset();
}

使用看门狗功能,可以防止芯片死机,对于需要无人看护的应用则是很必要的功能。

watchdog_328

二、软起动 Soft Reset

可以使用汇编指令,使程序从头开始执行。这个方法如果程序死掉就不起作用了。

asm volatile ("jmp 0");

三、看门狗头文件 wdt.h

下面是 avr\wdt.h ( 路径比如 c:\arduino-1.0.6\hardware\tools\avr\avr\include\avr\wdt.h  取决于实际安装的目录 ) 中关于时间的定义:

/**
   \ingroup avr_watchdog
   Symbolic constants for the watchdog timeout.  Since the watchdog
   timer is based on a free-running RC oscillator, the times are
   approximate only and apply to a supply voltage of 5 V.  At lower
   supply voltages, the times will increase.  For older devices, the
   times will be as large as three times when operating at Vcc = 3 V,
   while the newer devices (e. g. ATmega128, ATmega8) only experience
   a negligible change.

   Possible timeout values are: 15 ms, 30 ms, 60 ms, 120 ms, 250 ms,
   500 ms, 1 s, 2 s.  (Some devices also allow for 4 s and 8 s.)
   Symbolic constants are formed by the prefix
   \c WDTO_, followed by the time.

   Example that would select a watchdog timer expiry of approximately
   500 ms:
   \code
   wdt_enable(WDTO_500MS);
   \endcode
*/
#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, 
    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,
    AT90PWM1, AT90PWM2, AT90PWM2B, AT90PWM3, AT90PWM3B, AT90PWM216, AT90PWM316
    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, 
    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,
    AT90PWM1, AT90PWM2, AT90PWM2B, AT90PWM3, AT90PWM3B, AT90PWM216, AT90PWM316
    AT90USB82, AT90USB162,
    AT90USB646, AT90USB647, AT90USB1286, AT90USB1287,
    ATtiny48, ATtiny88.
    */
#define WDTO_8S     9

扩展阅读

关键字:Arduino, 看门狗, Watchdog

5 thoughts on “在 Arduino 中使用 Watchdog 看门狗 Reset 功能

    • LED不是用来触发,是用来标识重启,仅仅是个显示。当然可以调用函数。

    • 试一试就知道了。我不太清楚mini是什么芯片,如果是328,哪应该是可以。

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.