Many sample programs for the MSP430 have their first line as:
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
Why do they do this?
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It only takes a minute to sign up.
Sign up to join this communityThe watchdog timer (WDT) is on by default, it's a useful thing to have in more complex applications, but trips a lot of new people up. They often will not service the WDT in their code or include an interrupt service routine (ISR) to handle the WDT event, so, when their chip keeps resetting they become very frustrated. Additionally, the sample programs are, for the most part, not trying to demonstrate the WDT so it's turned off.
Edit: The watchdog timer could have been named "dead man's switch". Its default behavior is to reset the microcontroller unless the firmware periodically lets it know everything is running fine. This is known as "feeding the dog" or "kicking the dog". This way, if your firmware gets stuck in a loop or otherwise stops operating as expected, the watchdog is not fed and will reset the chip (hopefully to a fresh, working state).
You can also use the WDT as periodic interrupt to perform other tasks, whatever you can imagine. You just have to write the relevant ISR.
In addition to Samuel's point about people accidentally tripping the WDT, there's another important reason why it should be disabled initially.
Even if your application is normally capable of resetting the timer correctly, it may not be able to do so during the initialisation code, for two reasons:
As a result, it's good practice to disable the WDT as the very first thing you do, even if you never enabled it.
If you do want to use it, you can re-enable it immediately before turning on interrupts, as the last step of your initilsation code.
int _system_pre_init(void)
function, which executes before main
\$\endgroup\$
Jul 13, 2014 at 12:58