Menu

all about electronic and microcontrollers

Sunday, October 17, 2010

Lesson nr.04-Watch-dog timer

Hardware setup:
This example illustrates how the watch-dog timer should not be used. A command used for resetting this timer is intentionally left out in the main program loop, thus enabling the microcontroller to be reset. As a result, the microcontroller will be reset all the time, which is reflected as PORT RB.0~RB.7 LED blinking.
Button S1 and (pull-up) resistance with value of 4.7 K will provide the manual reset mode to the microcontroller.
ICSP connector provides the connection between the programmer (PicKit 2) and microcontroller (PIC16F628A).



Circuit Diagram:
For those who want to build it on their own breadboard or other platform, here is the electronic scheme built in Eagle Cad, free version:


Software:
Here is the C program written for MikroC PRO for PIC 2010 (version v4.15).
/*
'*******************************************************************************
'  Lesson nr.04:
'          Watch-dog timer
'  Written by:
'          Aureliu Raducu Macovei, 2010.
'  Description:
'          This example illustrates how the watch-dog timer should not be used.
'          A command used for resetting this timer is intentionally left out in
'          the main program loop, thus enabling the microcontroller to be reset.
'          As a result, the microcontroller will be reset all the time, which is
'          reflected as PORT RB.0~RB.7 LED blinking.
'  Test configuration:
'    MCU:                        PIC16F628A
'    Test.Board:                 WB-106 Breadboard 2420 dots
'    SW:                         MikroC PRO for PIC 2010 (version v4.15)
'  Configuration Word
'    Oscillator:                 INTOSC:I/O on RA.6, I/O on RA.7
'    Watchdog Timer:             ON
'    Power up Timer:             Disabled
'    Master Clear Enable:        Enabled
'    Browun Out Detect:          Enabled
'    Low Voltage Program:        Disabled
'    Data EE Read Protect:       Disabled
'    Code Protect:               OFF
'*******************************************************************************
*/
void main()                     // Main;
{
 OPTION_REG = 0x0E;             // Prescaler is assigned to timer wdt
                                // (1:4[0x0A];1:8[0x0B];1:16[0x0C];1:32[0x0D];
                                // 1:64[0x0E];1:128[0x0F]);
 asm CLRWDT;                    // Assembly command to reset WDT timer;
 TRISB = 0x00;                  // All port B pins are configured as output;
 PORTB = 0x0F;                  // Initial value of the PORTB register;
 Delay_ms(600);                 // 600ms delay;
 PORTB = 0xF0;                  // Porta B value different from initial;
 while(1);                      // Endless loop. Program remains here until WDT
                                // timer reset the  microcontroller;
 }                              // End.

4 comments:

  1. Hello wat is the exact application of watch dog timer...if we are using c codings how we know the
    exact time for the instructions to get completed..

    ReplyDelete
    Replies
    1. Setting the prescaler to 1:64, the result in time will be ~1.1 seconds

      Delete
  2. after
    PORTB = 0xF0;
    must be
    Delay_ms(600);

    ReplyDelete
    Replies
    1. It's true what you say only if you don't use WDT.
      But in this example, WDT reset all the game, just in right moment, that's why it's not necessary your code line.

      Delete

If you do not understand something, or if you make some aplication helped by this blog, let me know.