I have made a hardware development based on an ATmega328P and programmed with the Arduino IDE.
The board has one relay output that switchs an AC load.
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.
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.
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.
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.
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.