2
\$\begingroup\$

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?

\$\endgroup\$
2
\$\begingroup\$

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;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.