Watchdog Timers


Closed Thread
Results 1 to 5 of 5

Thread: Watchdog Timers

  1. #1
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    257

    Post Watchdog Timers

    Hello all,

    Still learning all there is to know about microcontrollers...
    I assume that it is good practice to use a WDT in Picbasic programs, ie to reset the PIC if things go pear shaped.

    I'm using a PIC 16F628, and IC-Prog 1.05C to set the Code protect registers.

    Few questions,
    1. Generally , how often should the WDT be cleared in the program?

    2. Is the Pause instruction effected by the WDT?

    3. Is it possible that a PIC can get caught up in a WDT loop that causes it to stop executing code?

    I have written a simple program to test the WDT and it is hard to tell if it is working properly... maybe answers to the above will help...

    Cheers

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    WDT is handled automatically for you by PBP in background - you don't have to do anything. If you look at the Assembler listing of any of your compiled programs, you will see a liberal sprinkling of CLRWDT instructions throughout. PBP puts them there to keep resetting the WDT so you don't have to.

    The Pause instruction compiles to have a CLRWDT within it, otherwise something like Pause 10000 would cause the WDT to restart the PIC.

    You would have to try real hard to get the WDT to crash a PBP program through normal code execution... here is a program that blinks a LED only on Start-Up...

    '
    ' PIC Defines
    ' -----------
    @ DEVICE pic16F628, INTRC_OSC_NOCLKOUT
    ' System Clock Options
    @ DEVICE pic16F628, WDT_ON
    ' Watchdog Timer
    @ DEVICE pic16F628, PWRT_ON
    ' Power-On Timer
    @ DEVICE pic16F628, MCLR_OFF
    ' Master Clear Options (Internal)
    @ DEVICE pic16F628, BOD_ON
    ' Brown-Out Detect
    @ DEVICE pic16F628, LVP_OFF
    ' Low-Voltage Programming
    @ DEVICE pic16F628, CPD_OFF
    ' Data Memory Code Protect
    @ DEVICE pic16F628, PROTECT_OFF
    ' Program Code Protection

    MyLED var PortB.1
    ' LED Connected between PIC pin and Vdd via Resistor
    Start:
    Low MyLED
    Pause 1000
    High MyLED
    EndlessLoop:
    Pause 1000
    Goto EndlessLoop
    End

    Now the above program will blink the LED only once when you first power-on thereafter it'll sit there till doomsday doing nothing except executing the endless loop.

    You can add the following define to stop PBP from automatically resetting the WDT (see PICBasic Pro Manual under CLEARWDT command)...

    DEFINE NO_CLRWDT 1

    Now we don't have to try so hard to get the WDT to kick us into Reset (using our original code example)...

    '
    ' PIC Defines
    ' -----------
    @ DEVICE pic16F628, INTRC_OSC_NOCLKOUT
    ' System Clock Options
    @ DEVICE pic16F628, WDT_ON
    ' Watchdog Timer
    @ DEVICE pic16F628, PWRT_ON
    ' Power-On Timer
    @ DEVICE pic16F628, MCLR_OFF
    ' Master Clear Options (Internal)
    @ DEVICE pic16F628, BOD_ON
    ' Brown-Out Detect
    @ DEVICE pic16F628, LVP_OFF
    ' Low-Voltage Programming
    @ DEVICE pic16F628, CPD_OFF
    ' Data Memory Code Protect
    @ DEVICE pic16F628, PROTECT_OFF
    ' Program Code Protection

    DEFINE NO_CLRWDT 1 ' Don't automatically insert CLRWDT's

    MyLED var PortB.1
    ' LED Connected between PIC pin and Vdd via Resistor
    Start:
    Low MyLED
    Pause 1000
    High MyLED
    EndlessLoop:
    Pause 1000
    Goto EndlessLoop
    End

    So unless you start putting CLEARWDT instructions within your code, you should see that LED blink about once every couple of seconds or so...

    We can also manually force the WDT to execute a restart by rewriting our program to perform the world's tightest loop (ie a 'goto' onto itself) like so...

    '
    ' PIC Defines
    ' -----------
    @ DEVICE pic16F628, INTRC_OSC_NOCLKOUT
    ' System Clock Options
    @ DEVICE pic16F628, WDT_ON
    ' Watchdog Timer
    @ DEVICE pic16F628, PWRT_ON
    ' Power-On Timer
    @ DEVICE pic16F628, MCLR_OFF
    ' Master Clear Options (Internal)
    @ DEVICE pic16F628, BOD_ON
    ' Brown-Out Detect
    @ DEVICE pic16F628, LVP_OFF
    ' Low-Voltage Programming
    @ DEVICE pic16F628, CPD_OFF
    ' Data Memory Code Protect
    @ DEVICE pic16F628, PROTECT_OFF
    ' Program Code Protection

    MyLED var PortB.1
    ' LED Connected between PIC pin and Vdd via Resistor
    Start:
    Low MyLED
    Pause 1000
    High MyLED
    EndlessLoop:
    @ goto $
    End

    So although here PBP has sprinkled CLRWDT throughout our code, there is however no CLRWDT instruction in that 'goto' loop at the end so the WDT will kick-in after a couple of seconds or so.

    Remember finally that the WDT shares the prescaler with TMR0. By default the WDT has the prescaler. Assign the prescaler to TMR0 and the WDT timeout suddenly becomes about 18mS.

    Melanie

  3. #3
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    257


    Did you find this post helpful? Yes | No

    Default

    Wow! You told me everything that I need to know... and the code worked just as you said it would...

    Thanks and Merry Christmas!
    Jared

  4. #4
    Join Date
    Aug 2014
    Posts
    2


    Did you find this post helpful? Yes | No

    Thumbs up Re: Watchdog Timers

    I don't know how to thank you I had no idea wdt resets by default and I wanted to use it to start over my program but it wouldn't work and now i know why i have a question i want to clear every thing when my micro resets by wdt but i don't know how I've tried clear at first line of my program but it doesn't do it
    is there a way?
    Last edited by Archangel; - 27th August 2014 at 18:57.

  5. #5
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default Re: Watchdog Timers

    WDT timer "resets" PIC to code beginning. Put clear right after you declare your variables so they exist as a ram location, and they should clear, better you should simply zero them in code specifically as opposed to using the shotgun approach (clear).

Similar Threads

  1. Timers and Interrupts
    By senior project1 in forum Off Topic
    Replies: 2
    Last Post: - 25th November 2008, 15:17
  2. Timers
    By mitchf14 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th November 2008, 21:08
  3. Reading Timers
    By kevj in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th August 2007, 09:19
  4. hardware timers
    By Adam in forum General
    Replies: 3
    Last Post: - 7th March 2007, 01:10
  5. Availability of PIC timers from PBP
    By coda64 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 8th February 2006, 08:18

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts