![Atmel AVR132 Application Note Download Page 13](http://html1.mh-extra.com/html/atmel/avr132/avr132_application-note_3003535013.webp)
13
AVR 132 Application Note
2551A–AVR–11/03
Command 3 gives an example of runaway code. This example just calls a function at an
unused address. The program counter runs to the end of program memory and wraps
back to address 0. No reset flags will be set and the fault is caught safely in the initializa-
tion routine.
T o s i m u l a t e t h e b a d f u n c t i o n c a l l , t h e f o l l o w i n g c o d e f r a g m e n t i s u s e d :
“((void(*)()) 0x1FF)();“
The integer
0x1FF
is converted to a pointer-to-a-function, and the
function is called. Refer to the ANSI C standard for more details on function pointers and
type conversions.
Using the WDT as a Wakeup
Timer
This example only uses the WDT Interrupt Mode, and the initialization routine is thus
quite reduced. As described earlier it is important to disable the WDT System Reset
Mode upon startup even if the WDT System Reset Mode is never used. The initialization
routine is shown in Figure 11.
Figure 11. Initialization Routine When Using the WDT as Wakeup Timer
The main loop of this example flashes the LED connected to PB0 10 times to show that
it is awake. It then resets the WDT, enables the WDT Interrupt Mode and enters sleep
mode. When the WDT times out, it wakes up the CPU again. The interrupt handler dis-
ables WDT Interrupt Mode, so that no unneccessary interrupts are generated if the main
loop runs long before entering sleep mode once again.
Combined Operation
The third example shows how to use the WDT both as a Wakeup Timer and system
reset source with parameter backup. It is an extended version of the first code example,
now using Command 1 to enter sleep mode.
In this example, the initialization routine includes the parts shown in dashed frames in
the flowchart. This means that the WDT Interrupt Mode is enabled and backed up
parameters are restored if the Write Complete flag is set upon startup.
The parameter to be backed up is the value of the Timer/Counter1. It has no particular
function in this application, but serves as an example of a parameter that is cleared on
reset and needs to be restored.
Initialization
Disable WDT
Sy stem Reset Mode
Set WDT timeout
period
Set Sleep Mode
Return
Enable WDT
Interrupt Mode