我遇到了一个问题,即使在计时器上还有足够的时间,在AVR ATtiny84A上执行禁用看门狗序列实际上也会复位芯片。当在许多物理部件上运行相同的代码时,这会不一致地发生。有些每次都会重置,有些有时会重置,有些则永远不会重置。
为了演示该问题,我编写了一个简单的程序,该程序可以...
- 使看门狗超时1秒
- 重置看门狗
- 使白色LED闪烁0.1秒
- 闪烁白色LED熄灭0.1秒
- 禁用看门狗
看门狗使能和禁用之间的总时间少于0.3秒,但是有时在执行禁用序列时会发生看门狗复位。
这是代码:
#define F_CPU 1000000 // Name used by delay.h. We are running 1Mhz (default fuses)
#include <avr/io.h>
#include <util/delay.h>
#include <avr/wdt.h>
// White LED connected to pin 8 - PA5
#define WHITE_LED_PORT PORTA
#define WHITE_LED_DDR DDRA
#define WHITE_LED_BIT 5
// Red LED connected to pin 7 - PA6
#define RED_LED_PORT PORTA
#define RED_LED_DDR DDRA
#define RED_LED_BIT 6
int main(void)
{
// Set LED pins to output mode
RED_LED_DDR |= _BV(RED_LED_BIT);
WHITE_LED_DDR |= _BV(WHITE_LED_BIT);
// Are we coming out of a watchdog reset?
// WDRF: Watchdog Reset Flag
// This bit is set if a watchdog reset occurs. The bit is reset by a Power-on Reset, or by writing a
// logic zero to the flag
if (MCUSR & _BV(WDRF) ) {
// We should never get here!
// Light the RED led to show it happened
RED_LED_PORT |= _BV(RED_LED_BIT);
MCUCR = 0; // Clear the flag for next time
}
while(1)
{
// Enable a 1 second watchdog
wdt_enable( WDTO_1S );
wdt_reset(); // Not necessary since the enable macro does it, but just to be 100% sure
// Flash white LED for 0.1 second just so we know it is running
WHITE_LED_PORT |= _BV(WHITE_LED_BIT);
_delay_ms(100);
WHITE_LED_PORT &= ~_BV(WHITE_LED_BIT);
_delay_ms(100);
// Ok, when we get here, it has only been about 0.2 seconds since we reset the watchdog.
wdt_disable(); // Turn off the watchdog with plenty of time to spare.
}
}
在启动时,程序将检查先前的重置是否是由看门狗超时引起的,如果是,它将点亮红色LED并清除看门狗重置标志以指示看门狗重置发生。我相信永远不要执行此代码,永远不要点亮红色LED,但是经常这样做。
这里发生了什么?