After set a watchdog,the mcu died.I can't reset it though the Reset pin

here is my code

void setup()
{   Serial.begin(9600);
	wdt_disable();
	wdt_reset();
	Serial.println("Begin");
}
void loop()
{
		wdt_enable(WDTO_4S);
		wdt_reset();
		cli();
		while (1)
		{
		}
}

I upload it.But it only show once "Begin" then nothing happen.I want to reupload the program.But it won't reset even I put reset pin to ground.The chip seems die.Only use ASP to erase the chip then the chip can reprogram again.What's the matter?Is the wdt library broken?
I have test 3 chips but it happen the same thing...(ATMEGA328P)

Yes that is what happens because the time out is less than the time it takes for the boot loader to finish loading some new code. It will happen every time.

Ehh,I have set it to 8S,the code won't start either.

I found the problem.Bootloader is the problem!(You are right).(I am using 3.3V 8Mhz bootloader).

Right now I have to use 8Mhz bootloader.And I need MCUSR,too.I can't use optiboot because they use 115200bps which is too fast.
Any advice?thx

I think you can do it if you take the right steps.

  • Power off the chip
  • Hold down the reset button
  • Power the chip back on, still holding reset down
  • Start uploading the sketch
  • Release the reset button

Doing it this way stops the sketch from running on power-up, and thus whatever-it-does doesn't matter. You go straight to the bootloader.

If you are running out of enough hands to do this you might use a wire with an alligator clip to keep reset shorted to ground, while you do the other steps, and then release it at the right moment.

I can burn the program in right now.Bootloader makes watchdog fail.So right now I need a bootloader which is support 8mhz,also make watchdog usable and the MCUSR need to work

Rather than trying to reset the processor, generate an interrupt:

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

#define LED 13

// watchdog interrupt
ISR (WDT_vect) 
{
  wdt_disable();  // disable watchdog
}  // end of WDT_vect

void setup ()
{
  pinMode (LED, OUTPUT);
  set_sleep_mode (SLEEP_MODE_PWR_SAVE);
}  // end of setup

void loop()
{
  // sleep bit patterns:
  //  1 second:  0b000110
  //  2 seconds: 0b000111
  //  4 seconds: 0b100000
  //  8 seconds: 0b100001

  // sleep for 4 seconds
  const byte interval = 0b100000;
  
  MCUSR = 0;                          // reset various flags
  WDTCSR |= 0b00011000;               // see docs, set WDCE, WDE
  WDTCSR =  0b01000000 | interval;    // set WDIE, and appropriate delay
  wdt_reset();                        // pat the dog
  sleep_mode ();                      // now goes to sleep and waits for the watchdog

  // now do stuff
  for (byte i = 0; i < 5; i++)
    {
    digitalWrite (LED, HIGH);
    delay (250);
    digitalWrite (LED, LOW);
    delay (250);
    }

}  // end of loop

The above code flashes the LED on pin 13 every four seconds.

This version prints to serial every four seconds:

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

// watchdog interrupt
ISR (WDT_vect) 
{
  wdt_disable();  // disable watchdog
}  // end of WDT_vect

void setup ()
{
  set_sleep_mode (SLEEP_MODE_PWR_SAVE);
  Serial.begin (115200);
}  // end of setup

long counter = 0;

void loop()
{
  // sleep bit patterns:
  //  1 second:  0b000110
  //  2 seconds: 0b000111
  //  4 seconds: 0b100000
  //  8 seconds: 0b100001

  // sleep for 4 seconds
  const byte interval = 0b100000;
  
  MCUSR = 0;                          // reset various flags
  WDTCSR |= 0b00011000;               // see docs, set WDCE, WDE
  WDTCSR =  0b01000000 | interval;    // set WDIE, and appropriate delay
  wdt_reset();                        // pat the dog
  sleep_mode ();                      // now goes to sleep and waits for the watchdog

  Serial.print (F("Hello, world! Iteration: "));
  Serial.println (++counter);
  Serial.flush ();  // wait for output to complete
}  // end of loop
Serial.println("Begin");

...
cli();

One problem with your original code is that turning interrupts off stops serial output. That is why I had Serial.flush() in my code.

Nick, you might want to add another w to your link in reply #5

Thanks, I've been noticing my Arduino Leonardo "message generator" has been less reliable now I moved to Ubuntu. I'll have to add ... gasp! ... delay().

Halry:
here is my code

void setup()

{   Serial.begin(9600);
wdt_disable();
wdt_reset();
Serial.println("Begin");
}

One problem with this code is that you need to clear the WDT reset flag before you can disable the watchdog timer. I ran into this exact same issue with an ATtiny84V. Add MCUSR = 0; before wdt_disable();.

And put the watchdog disabling code right at the beginning of the setup().

In case anyone is wondering how to do this (it is not totally trivial) here is what worked. I needed to take a line like this:

Keyboard.println (F("http://www.gammon.com.au/interrupts"));

And have it delay for (say) 5 milliseconds between each character. The easiest way I could find was to derive a class from Print, and use that. eg.

class my_Keyboard_class : public Print
{
public:
	virtual size_t  write(uint8_t c);
};

size_t my_Keyboard_class::write (uint8_t character)
{
  Keyboard.write (character);
  delay (5);
  return 1;
}

my_Keyboard_class myKeyboard;

Now just change Keyboard.print (or println) to myKeyboard.print throughout the code and you are done. eg.

myKeyboard.println (F("http://www.gammon.com.au/interrupts"));

Your experience is showing.
Thank you, I was wondering.

Jiggy-Ninja:
One problem with this code is that you need to clear the WDT reset flag before you can disable the watchdog timer. I ran into this exact same issue with an ATtiny84V. Add MCUSR = 0; before wdt_disable();.

And put the watchdog disabling code right at the beginning of the setup().

I tried that and sadly it didn't work, eg.

#include <avr/wdt.h>

void setup()
{   
  MCUSR = 0; 
  wdt_disable();
  wdt_enable(WDTO_4S);
  Serial.begin(115200);
  wdt_reset();
  Serial.println("Begin");
  Serial.flush ();
}

void loop()
{
  wdt_reset();
  cli();
  while (1)
  {
  }
}

According to a page I found ( avr-libc: <avr/wdt.h>: Watchdog timer handling ) if the watchdog was enabled it still is on reset with a fast prescaler (15 mS). So if the bootloader doesn't turn it off you are in trouble. I tested with the Lilypad bootloader and it didn't work. With the Optiboot loader, it does work. So the problem is indeed in the bootloader. Personally I don't know where to find a fixed 8 MHz bootloader.

Yes,I am going to make a bootloader,but after I burning my bootloader,everytime I want to upload program.avrdude will give me "verification error",and program won't work.
I builded it with ATMELStudio.Here is the bootloader for test(no wdt modification,just want to test it.use -Os optimization,4K on disk,Fuse:0xFEDCE2,Lock:0xCF.)
Here you go:
http://pastebin.com/j5a0nr1Y(the forum only allow 9500 words.So I put it to pastebin)
Thanks for your replies

You can attach code to posts.

How to use this forum

I have to use 8Mhz bootloader.And I need MCUSR,too.I can't use optiboot because they use 115200bps which is too fast.

Take the default (16MHz) optiboot bootloader and put it on your 8MHz chips. Then configure your boards.txt so that the upload speed is 57600 instead of 115200 (half the clockrate means half the upload speed.)

I know.But the bootloader is too long,the forum said it is not allow over 9500 words........I can't post it.So I put it to pastebin

No it didn't , that was for posts in the body of the thread not attachments.

As explained here: How to use this forum

Scroll down to:

  1. You can add attachments