Using Watchdog Timer on Nano - resetting before it times out

I am teaching an Arduino class using the Nano and am for the WDT (Watchdog timer) section, I am teaching how to program the WDTCSR register for the prescaler control and mode control.

I understand that the avr/wdt.h library does not work with the Nano unless you burn a new bootloader and I want to just keep the existing bootloader and have the students write to the registers directly.

Setting up the registers works fine and the WDT interrupts just like it should. Now I want to show them how to reset the WDT so that as long as they keep resetting it, it will not interrupt.

My question is how to do that. What register, what bit do I need to set/reset in order to keep the WDT from interrupting?

The ATmega328 manual talks about the “Watchdog Timer Reset (WDR) Instruction” and has an assembly example using “wdr;” and a C programming example using “_watchdog_reset()” but if I try using either of those in my code I get an error that they are not defined.

Any ideas?

#define wdt_reset() __asm__ __volatile__ ("wdr")

This is how it is defined in wdt.h. It is just one assembly instruction.
Are you sure the nano is not supported by avr/wdt? It is a 328-based board, it should be IIRC

Thanks, that works!

The wdt.h library puts the Nano into an infinite wdt reset loop and requires that you re-flash the bootstrap loader.

The solution is to use a different bootstrap loader (opt.boot) but that turns the Nano into a UNO and I did not want to do that.


Well, it is not the use of the library that puts the nano into the reset loop. Using library functions to change watchdog settings is no different from using direct register manipulation as you do.
If you really have students that are instructed to fiddle with WDT registers, you should not use a platform that goes into an unusable state when using a wrong command

The easiest way to fix that issue on a nano is to connect it to ISP programmer, tell the IDE that it's an Uno, and burn bootloader. Then always tell the IDE it's an Uno when uploading to it.

For everything other than wdt_reset() I would recommend setting the registers directly. Those builtin avr libraries are the spawn of the devil - they don't do very much, just set a few registers, but it is so easy to get screwed because you don't realize exactly what it's doing to which register.

I have 60 students and 60 Nano's - so burning a new bootloader for each one is not my first choice. :slight_smile: