Monday, May 25, 2020

Using the watch dog timer in PIC16F887

Watch dog timer (WDT) is an internal digital circuitry exists in most of microcontrollers. It's designed to reset its main MCU due to many causes. The reset may caused by hardware fails or software stuck reasons.
For example, in PIC16F887 the WDT reset may caused by oscillator fail, software stuck or external communication time out.

In PIC16F887 a WDT is an 8-bit counter, clocked by its independent internal 31 kHz oscillator (LFINTOSC). It has two prescaler to reduce the reset timeout. Those two prescaler are 16-bit WDT prescaler and software programmable prescaler of the OPTION register. Using these two prescalers, we can adjust the WDT time out between 1 milli second to 268 seconds.

Using the watch dog timer in PIC16F887 
There are registers involve with WDT control:
  1. The configuration register
  2. The OPTION_REG
  3. watch dog timer control register (WDTCON)
The two registers mentioned above have already listed in the previous posts. Now let see a new register WDTCON.
  • The watch dog timer control register (WDTCON)
 BIT 7





 BIT 0
 N/AN/A N/A WDPTS3  WDPTS2  WDPTS1  WDPTS0 SWDTEN

Watch dog timer period selects bits (WDPTS[3:0]) is the prescaler rate selection. This 4-bit group could make a 16-bit prescaler value. Using the WDPTS we can select a prescaler between 1:32 to 1:65536. For more detail about this prescaler, we can read the datasheet.
By default, the value of this prescaler after reset is 0x04, making a time out of 17 milli second.

The software enable or disable the watch dog timer (SWDTEN) turn on or turn off the watch dog timer control. If we have already turn on the watch dog timer in the configuration WDTE=1, this bit has no effect in the program. But if we did not set WDTE=1, then we can enable the watch dog timer in the running program by assigning SWDTEN to '1'.

In the OPTION_REG, the prescaler assignment (PSA) is assigned to WDT and the prescaler select bits PS[2:0] is 0x07 which equal to 1:128, by default. 

With these default settings, we can find the total time out of WDT to make a reset as follow:

Using the watch dog timer in PIC16F887

As mention above we can adjust the time out between 1 milli second to 268 seconds, by adjusting the two prescaler and calculate for the time out period.

In the following example, I use the WDT default time out as calculated above. An input switch is debouncing from unwanted counting using the software delay loop, the while loop. It could make a software delay loop, or it could stuck in this loop we don't release the switch pressing. The WDT non-stop counts the time. If the pressing takes longer than 2.176 seconds, the time out occurs causing the reset.

Using the watch dog timer in PIC16F887
Schematic diagram. RC0 connects to an input switch counting the presses.
PORTC outputs an the 8-bit presses counting. Each press must take no
longer than 2.176 seconds.

XC8 source code is here:

#include "xc.h"
// PIC16F887 Configuration Bit Settings
// CONFIG1
#pragma config FOSC = XT
#pragma config WDTE = ON
#pragma config PWRTE = OFF
#pragma config MCLRE = ON
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = ON
#pragma config IESO = ON
#pragma config FCMEN = ON
#pragma config LVP = ON
// CONFIG2
#pragma config BOR4V = BOR40V
#pragma config WRT = OFF

/*_XTAL_FREQ use for __delay*/
#define _XTAL_FREQ 4000000

#define SW0 RC0

void main(){
    char i=0;
    //Clear PortD
    PORTD=0x00;
    //Set PortD To Output
    TRISD=0x00;
    //Clear PortC
    PORTC=0x00;
    //RC0 input
    TRISC0=1;
    //Clear WDT time
    CLRWDT();
    while(1){
        if(SW0==0){
            while(SW0==0);
            i++;
        }
        PORTD=i;
        /*Clear the previous
         counting in the while loop*/
        CLRWDT();
    }
}








No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (56) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (47) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (71) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (3) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (31) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)