# How to design Watchdog timer that reset the system if Watchdog counter is not cleared at least every 1/4 seconds

This is the code I have for a green LED blinking every 2s with watchdog mode for MSP432.

#include "msp.h"

void main(void)
{
volatile uint32_t i;
WDT_A->CTL = WDT_A_CTL_PW & 0xFF00;
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_IS_5;
P2DIR |= BIT1;
while (1) // continuous loop
{
P2OUT ^=BIT1; // Blink P1.0 LED
for (i =6000000; i> 0; i--); // Delay with 3Mhz clock.
}
}


How to design Watchdog timer that reset the system if Watchdog counter is not cleared at least every 1/4 seconds?

• Make an outer loop around your delay loop. Shorten the delay time. Trigger the watchdog inside the outer loop. – Janka Oct 28 '18 at 18:09
• Thanks for your reply. So Something like this, for(j= 30000; j> 0; j--) { WDT_A->CTL = WDT_A_CTL_PW & 0XFF00; for(I=3000000; I>0; I-- ); } I apologize I am very good at programming. – dhruv Oct 28 '18 at 18:23
• if 6000000 means 2s, and your watchdog needs to be triggered each 1/4s, you have to divide number this by eight, and your outer loop has to count to eight to make it the same delay. – Janka Oct 28 '18 at 18:41
• @Janka Ok, so you answered his question, how will he mark it as accepted? – pipe Oct 28 '18 at 20:40
• @Mike But he did not. He chose to ignore the information when posting his comment, which states "avoid answering questions in comments." The question will now remain listed as unsolved. – pipe Oct 30 '18 at 8:48

The purist/best practice solution is that your program should only kick the watchdog once, at the top of the eternal loop in main(). Thus the best practice is this:

void main (void)
{
...

uint32_t count = 0;

for(;;)
{
kick_dog();

if(count < max)
{
delay(n);
count++;
}
} // for(;;)
}


But of course busy-delay loops is an amateur solution in itself. Professional code would instead utilize on-chip hardware timers. Such as:

void main (void)
{
...
init_timer();
...

uint32_t count = 0;
timer_busy = false;

for(;;)
{
kick_dog();
led();
}
}

static void led (void)
{
if(!timer_busy)
{
timer_busy = true;
start_timer(); // some hw timer that will clear the timer_busy flag from its ISR

count++;
if(count == max)
{
// do something
}
}
}


I am not really familiar with MSP controller, but the answer should be something like this:

#include "msp.h"

void main(void)
{
volatile uint32_t i, j;
WDT_A->CTL = WDT_A_CTL_PW & 0xFF00;
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_IS_5;
P2DIR |= BIT1;
while (1) // continuous loop
{
P2OUT ^=BIT1; // Blink P1.0 LED
for (j = 8; j > 0; j--)
{
WDT_A->CTL = WDT_A_CTL_PW & 0XFF00;
for (i = 750000; i > 0; i--);      // Delay with 3Mhz clock, 0,26s.
}
}
}