Electronic – Understanding PIC32 watchdog timer operation

microchipmicrocontrollerpictimerwatchdog

I'm new to uC development and have been fooling around with the PIC series for about 6 months now. I recently started working with the PIC32 and Microchip's XC32 C++ compiler. I don't know if I have a fundamental misunderstanding of how the WDT works on the PIC series, or if there is some caveat of which I am unaware, however I have some code that I think should light an LED on RA0, then turn it off and after 512ms the WDT should reset the uC and the process should start over. What is actually happening is that the LED comes on momentarily, then goes off and never turns back on. I don't know if this means the WDT is incorrectly configured or what. Here is my code:

#include <xc.h>

#define LED _LATA0

/*
 * 
 */
int main(int argc, char** argv) {

    WDTCONbits.WDTPS = 0b1001; //512ms postscaler
    WDTCONbits.WDTCLR = 1; // feed the watchdog

    WDTCONbits.ON = 1; // enable the watchdog


    TRISAbits.TRISA0 = 0; //output on RA0
    ANSELAbits.ANSA0 = 0; // digital only


    LED = 1; //led on
    int i = 100000;
    while(i--);
    LED = 0; // led off
    while(1); // hang up and let WDT reset us

    return 0;
}

What part of successfully using the WDT am I missing?

Best Answer

After digging a little more, I think I found the answer. According to this document, the WDTCONbits.WDTPS register is just a shadow copy of the postscaler bits. To set the postscaler I had to add in my main file #pragma config WDTPS = PS512. My successful code now reads:

#define LED _LATA0
#pragma config WDTPS = PS512 // this is the magic line

/*
 * 
 */
int main(int argc, char** argv) {


    //WDTCONbits.WDTPS = 0b1001; //512ms postscaler

    WDTCONbits.WDTCLR = 1; // feed the watchdog



    WDTCONbits.ON = 1; // enable the watchdog


    TRISAbits.TRISA0 = 0; //output on RA0
    ANSELAbits.ANSA0 = 0; // digital only


    LED = 1; //led on
    int i = 100000;
    while(i--);
    LED = 0; // led off
    while(1); // hang up and let WDT reset us

    return 0;
}