PDA

View Full Version : Watchdog Timers



Squibcakes
- 16th December 2003, 07:12
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

Melanie
- 16th December 2003, 08:35
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

Squibcakes
- 17th December 2003, 00:04
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

student
- 27th August 2014, 13:22
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?

Archangel
- 27th August 2014, 19:03
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).