简体   繁体   English

即使启用了看门狗,Arduino 也会冻结

[英]Arduino is freezing even with watchdog enabled

I have made a hardware development based on an ATmega328P and programmed with the Arduino IDE.我基于 ATmega328P 进行了硬件开发,并使用 Arduino IDE 进行了编程。

The board has one relay output that switchs an AC load.该板有一个继电器 output 用于切换交流负载。

From time to time, the microcontroller is restarted when the load is switched ON or OFF.有时,当负载打开或关闭时,微控制器会重新启动。 No mystery so far.到目前为止还没有什么神秘之处。 It is probably some EMI interference that is causing the reboot.可能是一些 EMI 干扰导致重新启动。

BUT, sometimes the microcontroller freezes completely.但是,有时微控制器会完全冻结。 I can´t figure out why, as I have enabled the watchdog timer.我不知道为什么,因为我启用了看门狗定时器。 There shouldn't be any freezing.不应该有任何冻结。 As far as I know, the watchdog timer should restart the microcontroller after 2 seconds.据我所知,看门狗定时器应该在 2 秒后重启微控制器。

I would need help understanding WHY I am getting this behaviour and of course, if there could be any software fix.我需要帮助来理解为什么我会出现这种行为,当然,如果可以有任何软件修复。

This is a simple code to show this behaviour.这是显示此行为的简单代码。 I already tried to change some fuses configurations (brownout, wdton, etc) but no luck so far.我已经尝试更改一些保险丝配置(掉电、wdton 等),但到目前为止还没有成功。

Any help would be much appreciated任何帮助将非常感激

Thanks in advance提前致谢

#include <avr/wdt.h>

#define R0 3
#define R1 4
#define R2 5

unsigned int delayTime = 200;
unsigned int counter   = 0;

//--------------------------------------------------------------------

void setup() {
  
  MCUSR = 0; 
  wdt_disable(); 
  
  Serial.begin (9600);
  delay (1000);
  Serial.println ("********************RESTARTING*****************");
  pinMode (R0, OUTPUT); 
  pinMode (R1, OUTPUT);
  pinMode (R2, OUTPUT);
  delay (2000);   
  
  wdt_enable (WDTO_2S);
  
}

//--------------------------------------------------------------------

void loop() {
  
  wdt_reset();
  digitalWrite (R1, HIGH);
  counter++;
  Serial.print ("R1 activated, counter = "); Serial.println (counter);
  delay (delayTime);
  digitalWrite (R1, LOW);
  Serial.print ("R1 deactivated, counter = "); Serial.println (counter);
  delay (delayTime);
  
}

You not have enabled watchdog from reset for three second.您没有在三秒钟内启用看门狗复位。 I recomend use WDR activation by fuse bit WDTON , not by sw after three second.我建议通过保险丝位WDTON使用 WDR 激活,而不是在三秒后通过 sw 激活。

  void setup() {
  
  MCUSR = 0; 
  wdt_disable(); //WDR disabled
  
  Serial.begin (9600);
  delay (1000);   //one second delay
  Serial.println ("********************RESTARTING*****************");
  pinMode (R0, OUTPUT); 
  pinMode (R1, OUTPUT);
  pinMode (R2, OUTPUT);
  delay (2000);   //two second delay 
  
  wdt_enable (WDTO_2S);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM