PIC Watchdog Timers

I’m an admitted newbie to PIC uCs, but I thought I’d share a quick thing I just ran into and it deals with the watchdog timer.

I recently purchased some PIC16F722A-I/SP from Arrow, because unlike the PIC16F877A, these don’t need an external oscillator crystal.

I wanted to play with this and I thought the best way was to just do a little “Hello World” type blinky-led.

So, here was my code (note, I use MPLab and xc8):

* File: main.c
* Author: ceneblock
* Created on November 21, 2017, 12:30 PM

#define _XTAL_FREQ 16000000UL

#pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR Pin Function Select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Selection bits (BOR enabled)
#pragma config BORV = 19 // Brown-out Reset Voltage selection bit (Brown-out Reset Voltage (VBOR) set to 1.9 V nominal)
#pragma config PLLEN = ON // INTOSC PLL Enable bit (INTOSC Frequency is 16MHz (32x))

#pragma config VCAPEN = DIS // Voltage Regulator Capacitor Enable bits (All VCAP pin functions are disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.


void main(void) {
TRISB = 0x00;
RB0 = 1; // LED ON
__delay_ms(1000); // 1 Second Delay
RB0 = 0; // LED OFF
__delay_ms(1000); // 1 Second Delay


The configuration bits I got from the IDE’s configuration bit configuration window (window -> configuration windows -> configuration bits).

So, ready to give it a go, I dropped it into my ZIF programmer, hooked up my PICKit 3 and everything went as it should! No Errors!

However, as I was watching the led flash on and off, I noticed that it wasn’t on for one second and off for another.

I tried adjusting the times in __delay_ms(), but that didn’t help. So I hopped on IRC and asked on freenode’s ##Electronics room. One of the users suggested that my problem was that I wasn’t clearing the watch dog timer before the delays.

So, I added in the following code:


and now everything worked. That said, I could use this method or as the user suggested, I could extend the time before the watchdog timer resets. I might try that later, but this works.

This wasn’t my final solution though, while the inline assembly is just fine, I decided for this simple project I should just disable the watchdog timer all together, so changing the configuration bits:

#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)

Now everything is fine and dandy.

I’ll make a bigger project eventually, but for just right now, this is more than fine.

This entry was posted in Engineering and tagged , , , , . Bookmark the permalink.

Leave a Reply