Action
Configures the watchdog timer.
Syntax
CONFIG WATCHDOG = time
Remarks
Time |
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. |
Note that some new AVR's might have additional reset values such as 4096 and 8192.
When the WD is started, a reset will occur after the specified number of mS.
With 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 with the Fuse Bits.
After 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, providing that the register and bit is 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.
For 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 that you know the cause of the reset, you can read the register R0 before you run other code.
The sample below demonstrates how to store the WDRF bit if you need it, and print it later.
See also
START WATCHDOG , STOP WATCHDOG , RESET WATCHDOG
Example
'-----------------------------------------------------------------------------------------
'name : watchd.bas
'copyright : (c) 1995-2008, 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
Print "Watchdog test"
If Mcusr.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
Next
End
And this shows how to read the register r0:
Dim Breset As Byte
Breset = Peek(0)
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.