11
AVR 132 Application Note
2551A–AVR–11/03
The first conditional branch handles the case where no reset flags are set upon startup.
Since the reset flags are always cleared in the initialization routine, this only happens
when runaway code wraps back to address 0 and runs the startup code once again
without a reset. This clearly indicates a bug or fault in software and is handled like a
WDT System Reset. The initialization routine just enters an infinite loop and waits for the
WDT to reset the device properly.
The code then checks the WDT Reset Flag. If it is set, the routine increments the WDT
Reset counter and checks it against a predefined limit. If this limit is exceeded, the appli-
cation assumes that there is a permanent repeating error and indicates this by turning
on the failure indicator LED and halting execution. By entering an infinite loop with a
WDR instruction inside, execution is effectively halted until an external reset occurs.
Power-up or external reset events are considered to be manual intervention and the
WDT Reset counter is cleared. This makes it possible for a human 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 resetting and hoping for things to fix themselves is not a recommended solu-
tion.The rest of the flowchart should be self-explanatory.
Communicate Command
The routine that gets a command is an example of a poorly designed communication
routine. It flashes a LED 10 times and then waits for any button to be pressed. The prob-
lem arises when the user waits too long. A robust design should implement some sort of
timeout check and return with an error code if the communication times out. However,
this routine does not, and the WDT will reset the device if no button is pressed within the
WDT timeout period. The flowchart for the communication routine is shown in Figure 9.
Figure 9. Flash LED and Wait for User to Press Some Buttons
If a command button is pressed in time, the routine sets its health flag and returns the
button press bit pattern.
Any buttons
pressed ?
Communicate()
Flash LED 10 times
No
Yes
Save button bits
Turn off LED and wait
a number of cycles
Set health flag for
this routine
Return button bits
Potential WDT timeout here if
user waits too long before
pressing a command button