Top  Previous  Next


Configures the watchdog timer.









The interval constant in ms the watchdog timer will count to before it will reset your program.



Possible settings :

16 , 32, 64 , 128 , 256 , 512 , 1024 and 2048.

Some newer chips : 4096, 8192.


The XMEGA has a 1 KHz clocked watchdog. For Xmega the following value in millisecond need to be used :

8 ,16,32,64,125,250,500,1000,2000,4000,8000

So 2000 will sets a timeout of 2 seconds.


Note that some new AVR's might have additional reset values such as 4096 and 8192.


When the WatchDog is started, a reset will occur after the specified number of mS.

With a value of 2048, a reset will occur after 2 seconds, so you need to reset the WD in your programs periodically with the RESET WATCHDOG statement.


Some AVR's might have the WD timer enabled by default. You can change this by changing the Fuse Bits.


noticeGlobal Interrupts should be disabled when they are active. The reason is that changing the WD, a special timed sequence is required. An interrupt could extend the time, making the timed sequence fail.


noticeAfter the CONFIG WATCHDOG statement, the watchdog timer is disabled. You can also use CONFIG WATCHDOG to change the time out value. This will stop the watchdog timer and load the new value.

After a CONFIG WATCHDOG, you always need to start the Watchdog with the START WATCHDOG statement.


Most new AVR chips have an MCUSR register that contains some flags. One of the flags is the WDRF bit. This bit is set when the chip was reset by a Watchdog overflow. The CONFIG WATCHDOG will clear this bit, provided that the register and bit are available in the micro.

When it is important to examine at startup if the micro was reset by a Watchdog overflow, you need to examine this MCUSR.WDRF flag before you use CONFIG WATCHDOG, since that will clear the flag.


noticeFor chips that have an enhanced WD timer, the WD timer is cleared as part of the chip initialize procedure. This because otherwise  the WD timer will only work once. If it is important to know the cause of the reset, you can read the register R0 before you run other code.


When the chip resets, the status registers with the reset cause bits is saved into register R0.

This is done because the compiler need to reset these flags since otherwise they can not occur again. And before clearing the bits, the status is saved into register R0.


The sample below demonstrates how to store the WDRF bit if you need it, and print it later.



See also






'name                     : watchd.bas

'copyright                : (c) 1995-2021, MCS Electronics

'purpose                  : demonstrates the watchdog timer

'micro                    : Mega88

'suited for demo          : yes

'commercial addon needed  : no



$regfile = "m88def.dat"                                     ' specify the used micro

$crystal = 8000000                                          ' used crystal frequency

$baud = 19200                                               ' use baud rate

$hwstack = 32                                               ' default use 32 for the hardware stack

$swstack = 32                                               ' default use 32 for the SW stack

$framesize = 40                                             ' default use 40 for the frame space

Dim B As Byte

Dim Wdbit As Bit

Dim bWD As Byte


bWD= R0                                                     ' read the wd flag

Print "Watchdog test" 

If bwd.wdrf = 1 Then                                        ' there was a WD overflow

   Wdbit = 1                                                'store the flag

End If


Config Watchdog = 2048                                      'reset after 2048 mSec

If Wdbit = 1 Then                                           'just print it now since it is important that CONFIG WATCHDOG runs early as possible

    Print "Micro was reset by Watchdog overflow"

End If


Start Watchdog                                              'start the watchdog timer

Dim I As Word

For I = 1 To 1000

  Waitms 100

  Print I                                                   'print value

  B = Inkey()                                               ' get a key from the serial port

  If B = 65 Then                                            'letter A pressed

     Stop Watchdog                                          ' test if the WD will stop

  Elseif B = 66 Then                                        'letter B pressed

     Config Watchdog = 4096                                 'reconfig to 4 sec

     Start Watchdog                                         'CONFIG WATCHDOG will disable the WD so start it

  Elseif B = 67 Then                                        'C pressed

     Config Watchdog = 8192                                 ' some have 8 sec timer

     'observe that the WD timer is OFF

  Elseif B = 68 Then                                        'D pressed

     Start Watchdog                                         ' start it

  End If

  'Reset Watchdog

  'you will notice that the for next doesnt finish because of the reset

  'when you unmark the RESET WATCHDOG statement it will finish because the

  'wd-timer is reset before it reaches 2048 msec

  'When you press 'A' you will see that the WD will stop

  'When you press 'B' you will see that the WD will time out after 4 Sec

  'When you press 'C' you will see the WD will stop

  'When you press 'D' you will see the WD will start again timing out after 8 secs




And this shows how to read the register r0:

Dim Breset As Byte

Breset = R0

When you show this value on an LCD display you will see a value of 7 the first time, and later a value of 15 when the WD reset occured.



Xmega Sample

'                  (c) 1995-2021, MCS
'                      xm128-WD.bas
'  This sample demonstrates the Xmega128A1 Watchdog
$regfile = "xm128a1def.dat"
$crystal = 32000000
$hwstack = 64
$swstack = 64
$framesize = 64
'First Enable The Osc Of Your Choice
Config Osc = Enabled , 32mhzosc = Enabled
'configure the systemclock
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
Config Com1 = 19200 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8
Config Input1 = Cr , Echo = Crlf                           ' CR is used for input, we echo back CR and LF
Open "COM1:" For Binary As #1
'       ^^^^ change from COM1-COM8
Print #1 , "Xmega revision:" ; Mcu_revid                   ' make sure it is 7 or higher !!! lower revs have many flaws
Config Watchdog = 4000                                     'after 4 seconds a reset will occur if the watchdog is enabled
'possible value :  8 ,16,32,64,125,250,500,1000,2000,4000,8000
'these values are clock cycles, based on a 1 KHz clock !!!
Dim W As Word , B As Byte
 W = W + 1
Print W
Waitms 500
 B = Inkey()
If B = "a" Then
    Start Watchdog
    Print "start"
Elseif B = "b" Then
    Stop Watchdog
    Print "stop"
Elseif B = "c" Then
    Config Watchdog = 8000
    Print "8 sec"
Elseif B = "d" Then
  Reset Watchdog
  Print "reset"
End If