Watch Dog Timer (WDT+) of MSP430

Main function of a watch dog timer is to reset the microcontroller when it gets trapped in an infinite loop or experience a software fault .It is basically a hardware timer which counts up and resets the controller after the specified time has passed.During normal operation,the running program has to periodically clear the counter so that it doesnt reset the microcontroller .When the program gets stuck in a software loop,WDT is not cleared at the proper time as a result it counts up and resets the controller.

In MSP430x2xxx devices the watch dog timer is called as WDT+ and is available on all devices (including MSP430G2553) of that series.WDT+ is a 16 bit upcounter with selectable clocks and interrupt capabilities.WDT+ can be configured to act as either a Watch Dog Timer or an Interval timer that can generate periodic interrupts.The clocks for EDT+ can be sourced from either DCOCLK or ACLK.

The Figure below shows the Block Diagram of Watch Dog Timer of MSP430x2xxx microcontroller.

 Block Diagram of Watch Dog Timer of MSP430x2xxx microcontroller WDT+

Default Mode of WDT+ after Reset is Watch Dog Timer mode. The WDT modules sources its clock from DCO at approximately 1 MHz and resets the controller in about 32 milli seconds.So in all  programs, first thing to do is to disable the WDT using the WDTHOLD bit.

WDTCTL = WDTPW + WDTHOLD

Since it is the duty of WDT to protect against software glitches,WDT itself is protected by an internal password .if we tried to read or write to the watch dog control register (WDTCTL) without the password ,the microcontroller will get reseted.The password is 0x5A for writing into WDTCTL.The password is defined in the header file as WDTPW.It is possible to intentionally reset the microcontroller by writing into WDTCTL without WDTPW.

WDTCTL |= WDTHOLD; //Will reset MSP430,WDTIFG flag is set

Registers Required

WDTCTL - Contains WDTHOLD bit for stopping the WDT,WDTSSEL Bits for selecting Clocks ,WDTISx bits for selecting the timer interval,

IE1           - Contains WDTIE used to enable the interrupt in interval timer mode

IFG1        - WDTIFG flag which is set when WDT expires in watch dog mode.

Please note that only the important bits are shown,Please refer the datasheet for more details.

WDT+ as an Interval Timer

WDT+ can operate in two modes,one of the mode is called as interval timer mode .In this mode WDT+ generates an interrupt after a specified amount of time(MSP430 is not reseted in this mode).

The mode selection is done by setting the WDTTMSEL bit in WDTCTL register (WDTTMSEL = 1).You can also select the clocks(ACLK or SMCLK) to be used by WDT in this mode by configuring the WDTSSEL bit.The interval time period can be set by configuring WDTISx bits of WDTCTL.

Instead of going through all these bit fields ,you can use the interval time period constants defined in the header file "msp430g2553.h".

Timer constants for WDT(interval mode ) defined in header file

WDT_MDLY_32 means that time period of the interval is 32 milliseconds and the clock is taken from SMCLK.

WDT_ADLY_1000 means that time period of the interval is 1000 milliseconds (1Second) and the clock is taken from ACLK.

The details are provided next to the constants in comments(which is not shown in the above image ).Now lets write a small program to blink the LED after 1 second interval.The Clocks for WDT+ (interval timer mode ) are taken from ACLK.

#include "msp430g2553.h"
void main(void)
{
    WDTCTL =  WDTPW + WDTHOLD;//Stop the WDT to prevent reset
    P1DIR |=  BIT0;           //P1.0 output
    P1OUT &= ~BIT0;           //P1.0 = 0
    WDTCTL =  WDT_ADLY_1000;  //using ACLK from the LFXT1CLK
    IE1   |=  WDTIE;          //Enable the WDTIE bit  
    _BIS_SR(LPM3_bits + GIE); //Go to LPM3 with interrupts enabled
}

Please make sure that your launchpad has the watch crystal soldered to XIN and XOUT pin of MSP430G2553.You can source the clock from other sources like SMCLK but time interval will be short,also we are going to LPM3 in which SMCLK is disabled. 

In the first line like all programs,we first stop WDT from reseting the MSP430 controller.Then we configure the port pin P1.0 to light up the LED.

We then configure the WDT to go into the interval timer mode by using one of the constants from the header file

WDTCTL =  WDT_ADLY_1000

Here we have decided to make the interval as 1000 milli seconds or 1 second.The clock for WDT interval timer is sourced from ACLK as we are planning to go to sleep in LPM3.

After configuring the WDTCTL register we enable the WDT timer interrupt in IE1 .

IE1   |=  WDTIE;

Then we put the processor in sleep mode LPM3 (only ACLK is active i this mode ) with GIE enabled and wait for the WDT interrupt to come.

#pragma vector = WDT_VECTOR  //Interval timer vector location
__interrupt void IntervalTimer(void)
{
  P1OUT ^= BIT0; // Toggle P1.0
}

Here WDT_VECTOR is the interrupt vector location.The P1.0 is toggled inside the ISR to blink the LED every 1 second.WDTIFG is cleared automatically as interrupt is processed in this mode.

 

WDT+ as Watch Dog Timer

The main job of the Watch dog timer is to reset the microcontroller when it experiences a software fault like getting into an infinite loop.The WatchDog prevents run away code execution and helps to put the controller in a known situation. 

The mode selection is done by clearing the WDTTMSEL bit in WDTCTL register (WDTTMSEL = 0).Like the previous example (interval timer mode), it is much better to use the timer constants defined in the header file.The Timer constants of Watch dog mode are defined separately in the header file.

Timer constants for watch dog defined in msp430g2553.h header file

Like the interval timer mode,clock to the Watch dog timer can be sourced either from  either ACLK or SMCLK.

Here WDT_ARST_1000 means that WDT will reset the microcontroller in 1000 milliseconds and uses the ACLK .while WDT_MRST_32 means that WDT will reset the microcontroller in 32 milliseconds and uses the SMCLK.

Now let's write a small program to illustrate the use of Watch dog timer.In this example a infinite " while(1) " loop is used to simulate a software glitch.

#include "msp430g2553.h"
void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;// Stop the WDT to prevent reset
  int i;                   // Count value for delay
  P1DIR |= BIT0  + BIT6;   // P1.0(Red) and P1.6(Green)output
  P1OUT &= ~BIT0 + BIT6 ;  // P1.0 = 0,P1.6 =0
  for(i =0;i<0xffff;i++);  // A Small delay
  P1OUT |= BIT0;           // P1.0 = 1(Red LED On)  
  WDTCTL = WDT_ARST_1000;  // Put WDT+ in Watch Dog Mode

  //while(1);              // used to simulate glitch 

  WDTCTL = WDT_ARST_1000;  // Reset WDT+ for another 1 second,
  P1OUT  |= BIT6;          // Switch on green LED 
  WDTCTL = WDTPW + WDTHOLD;// Stop WDT
  _BIS_SR(LPM0);           // Put the CPU to sleep
}

In this program at first the WDT is halted using

WDTCTL = WDTPW + WDTHOLD;

so that we could configure the ports properly.The ports P1.0 (Red LED) and P1.6 (Green LED) are configured as outputs to light up the LED's on the Launchpad board. After that a small delay is provided to blink the red LED connected to P1.0.

Then the WDT is started in watch dog mode for a 1 second duration using

WDTCTL = WDT_ARST_1000;

Now to simulate a software glitch ,I am using an infinite while loop.

Under normal circumstances,ie when while(1);  " is commented out as shown in the above  code.The flow of execution proceeds in a linear function and clears the watchdog timer using

WDTCTL = WDT_ARST_1000;

for another 1 second preventing a system reset.The code then lights up the green LED and stops the WDT.

When " while(1);  " is uncommented and compiled into the program to simulate a glitch.The program would enter into an infinite loop after enabling the WDT.The CPU will be trapped in the loop while WDT slowly counts up.When the prescribed time period is up WDT resets the CPU and code execution starts again from the beginning (Red LED will start blinking).

In real world conditions most problems can be solved by a system reset So next time the code will not enter into the infinite loop and the execution proceeds to light up the green LED.

In our case RESETing the controller will not force the controller out of loop since " while(1);  " loop is hard coded by me and not a software glitch.

After the watch dog timer has reseted the processor the WDTIFG flag in IFG1 will be set.Your code can then use that information to notify that system have been reseted by WDT.

 

Back to Main Page