There are sometimes when your program is executing and for whatever reason it fails to operate as intended and causes the program to behave erratically or the CPU may get stuck in an infinite loop for example. In such cases it may be necessary to reset the microcontroller. However there are situations where this is not possible for example on the Mars rover or a pacemaker, where human intervention is not possible or improper operation can cause loss of life or death. In such cases it is necessary to have some way of resetting the microcontroller autonomously. That is the purpose of the watchdog timer and in this post we use the timer on board the PIC32MX270F256D microcontroller.

Watchdog Timer Module

The Watchdog Timer  or WDT on the PIC32 microcontroller operates from the internal LPRC oscillator so it consumes very little power. The watchdog timer is simple, all it does is ensure that if some malfunction occurs in software then resetting the hardware may fix the problem.

It is important when using the WDT to ensure that the timer is periodically cleared in software. If the timer is not cleared within the specified timeout period then the device will reset and sometimes it is unintentional. For this reason it is important to ensure the timer is turned off when not in use.

The timeout of the watchdog timer is set in the configuration bits and to use the watchdog timer of the PIC32 you simply set the WDT configuration register “ON” bit to “1” in software.

The timeout period of the watchdog timer on the PIC32MX270F256D ranges from 1 ms to 1048.576 seconds.


Connections

The hardware simply consists of a CP2104 USB-UART bridge connected to the microcontroller.


C Program

/*
 * File: Main.c
 * Author: Armstrong Subero
 * Processor: PIC32MX270F256D w/Ext OSC @ 4MHz, PLL to 48 MHz, 3.3v
 * Program: 14_WDT
 * Compiler: XC32 (v1.44, MPLAX X v4.00)
 * Program Version: 1.0
 *                             
 * Program Description: This Program Allows PIC32MX270F256D to demonstrate the
 *                      use of the Watchdog Timer which is used to break the
 *                      microcontroller out of an infinite loop.
 * 
 * Hardware Description: An CP2104 USB-UART converter is connected to PINC5
 * 
 * Change History:
 *
 * Author             Rev     Date          Description
 * Armstrong Subero   1.0     03/04/2018    Initial Release.
 * 
 *                      
 * Created April 3rd, 2018, 9:40 PM
 */

/*******************************************************************************
 Includes and defines
*******************************************************************************/
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "PIC32MX270_STD.h"
#include "Delay.h"
#include "uart1.h"

#define LED LATBbits.LATB14


/*******************************************************************************
* Function Name: void initMain (void)
*
* Specification: initializes main
*
*******************************************************************************/
void initMain(void)
{  
    uc_init();              // initialize microcontroller
    TRISBbits.TRISB14 = 0;  // set PINB1 as output
    
    // unlock PPS
    systemReg_Unlock();
    CFGCONbits.IOLOCK = 0;

    RPC5Rbits.RPC5R = 0x0001;   //RC5->UART1 TX

    // lock PPS
    CFGCONbits.IOLOCK = 1; 
    systemReg_Lock();
    
    UART1_Initialize();
    
    
    WDTCONbits.ON = 1;          // turn watchdog timer on
}


/*******************************************************************************
* Function Name: void main (void)
*
* Specification: main function
*
*******************************************************************************/
void main(void) {
    
    initMain();         // initializes main function
    
        
    // configure printf to use UART1
    __XC_UART = 1;
      
    while(1)
    {
  
      printf("Running\n");
      delay_ms(1000);
      
        for (;;)
        {
            printf("Stuck in infinite loop!\n");
            delay_ms(1000);
        }
      
    }
}


The program demonstrates the use of the watchdog timer to break the microcontroller out of a software malfunction, in this case an infinite loop. Since while within the loop the microcontroller is not cleared, after 8.192 seconds the microcontroller will be reset. The information of the state of the microcontroller is sent via UART.


Link to Project

You can download the entire project here!

Watchdog Timer Project

 

Leave a Reply

Your email address will not be published. Required fields are marked *