Atmel AVR132 Application Note Download Page 13

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

Summary of Contents for AVR132

Page 1: ...ous damage to its surroundings Automatic handling and recovery of such cases is the job of a watchdog timer The Enhanced Watchdog Timer WDT runs independent of the rest of the system causing system re...

Page 2: ...age 3 shows what happens when a WDT timeout occurs The dotted boxes describe actions performed by the system The solid lined boxes describe actions to be performed by the application When using the En...

Page 3: ...tion path through the main loop of your application This includes expected interrupt handlers as well If your main loop is very large several checkpoints could be inserted inside the loop to allow a s...

Page 4: ...period The Everything ok check at the end of the loop is the part of the loop deciding whether the application is operating correctly or not One solution is to use flags that are set in different part...

Page 5: ...to WDT System Reset The Write Complete Flag could be a byte in EEPROM indicating whether the backup operation was finished before the system reset This flag is checked in the startup code if the WDT...

Page 6: ...3V supply voltage An example on how to use the WDT as a wakeup source is shown in Figure 4 Figure 4 Using the WDT as a Wakeup Timer If periodic wakeups are preferred the disabling of the WDT Interrupt...

Page 7: ...pt or a timeout warning interrupt An example interrupt handler is shown in Figure 5 Figure 5 Dual purpose WDT Interrupt Handler Note that the wakeup flag must be set manually prior to entering sleep m...

Page 8: ...safe state or at least be able to supply some debugging information to the operator Changing the WDT Configuration To prevent accidental changes to the WDT configuration special timed sequences are n...

Page 9: ...igure 7 Figure 7 Circuit Diagram for Application Example The examples demonstrate the following concepts Using the WDT as a system reset source Using the WDT as a Wakeup Timer Using the WDT as a combi...

Page 10: ...dashed frames are only used in the Combined Operation code example and are described later Figure 8 Initialization Routine When Using the WDT as a System Reset Source Any res et f lags s et Initializa...

Page 11: ...uman operator to manually reset an application that has been halted by too many WDT System Resets The opera tor must of course try to find the source of the WDT System Resets before resetting Blindly...

Page 12: ...in the switch block Execute Command In this routine the command code decides which action to perform Command 1 has no particular action but it keeps the main loop running healthy by being a valid comm...

Page 13: ...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 t...

Page 14: ...decides what action to take If it is already cleared an error has occurred and the failure LED is lit The rest of the interrupt handler implementation complies with the flowchart The rest of the code...

Page 15: ...9F Tonetsu Shinkawa Bldg 1 24 8 Shinkawa Chuo ku Tokyo 104 0033 Japan Tel 81 3 3523 3551 Fax 81 3 3523 7581 Memory 2325 Orchard Parkway San Jose CA 95131 USA Tel 1 408 441 0311 Fax 1 408 436 4314 Mic...

Reviews: