Position: Index > Unclassified >

AVR watchdog reset timer-practical approach

2017-12-21 15:51  
Declaration:We aim to transmit more information by carrying articles . We will delete it soon, if we are involved in the problems of article content ,copyright or other problems.

This is continuing of thread Why use watchdog variable timer. This post is about how watchdog timer on AVR microcontroller works and how to control it.

As we mentioned earlier, watchdog timer is a distinct timer counter, which generates reset signal when it fill up.


After watchdog timer counts up to maximum, it generates a short pulse duration 1 clock cycle. This pulse triggers internal reset timer counting up to tout.

AVR watchdog timer is distinct clock generator which runs at 1 MHz. Watchdog timer has a prescaler module. So reset interval can be selected by adjusting the prescaler. Generally there are three things you have to do while controlling watchdog timer: enable, disable, and set prescaler.


First of all we need to set watchdog timer prescaler. Prescaler settings can be set in WDTCR(WatchDog Timer Control Register) register(Atmega8).


Prescaler is set by setting three bits (WDP2, WDP1, WDP0) of WDTCR register(see table bellow):


In order to be able to control watchdog timer, there some logic needs to be preserveed.

In order to enable watchdog timer just set WDE bit in WDTCR. Disabling and setting prescalelr is more complicated because some sequence of operations has to be made.

Write a€?1a€? to WDCE and WDE at the same instruction cycle(WDE must be written to a€?1a€? regardless if this was set earlier);

Within next four cycles write logical a€?0a€? to WDE. This disables watchdog timer.

Setting timer prescaller uses the same operation sequence:

Write a€?1a€? to WDCE and WDE at the same instruction cycle(WDE must be written to a€?1a€? regardless if this was set earlier);

Within next four cycles write WDP bits in WDTCR register. This sets new prescaler values of watchdog timer. Dont write 0 to WDE as this will disable timer.

ASM example:

ldi r16, 0×18
out WDTCR, r16 ;set WDTOE and WDE
ldi r16, 0×10
out WDTCR, r16 ; write a 0 to WD

and C example:

void WDT_off(void)


/* reset WDT */


/* Write logical one to WDCE and WDE */

WDTCR |= (1<<

/* Turn off WDT */

WDTCR = 0×00;


Watchdog timer can be enabled during flashing the chip.


Example from PonyProg programming software. Uncheck WDTON bit to enable watchdog timer. Prescaler can be changed (or watchdog timer disabled) the same way as discussed earlier. One thing that you cannot disable Watchdog timer this way (this can be done only by changing fuse settings).

Note: before you enable watchdog timer always reset watchdog timer, because the value might be non zero left. To reset timer use standard AVR assembler instruction WDR.

If you use WinAVR toolset, watchdog timer control can be much easier as there is pre-maid library for controlling watchdog timer.

Include wdt.h header from AVR folder (#include a€?avr/wdt.ha€?)

You may use functions:

#define wdt_reset()

#define wdt_disable()

#define wdt_enable(timeout)

Timeout values are also predefined:

#define WDTO_15MS 0

#define WDTO_30MS 1

#define WDTO_60MS 2

#define WDTO_120MS 3

#define WDTO_250MS 4

#define WDTO_500MS 5

#define WDTO_1S 6

#define WDTO_2S 7

#define WDTO_4S 8

#define WDTO_8S 9

Some timeout values may differ to different MCU types. Refer to AVRlibc documentation.

Good luck.

Reprinted Url Of This Article: