Simple Watch Dog with 8 bit PIC

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.
Post Reply
jay_dee
Posts: 230
http://meble-kuchenne.info.pl
Joined: Sun Dec 20, 2020 6:06 pm
Has thanked: 92 times
Been thanked: 61 times

Simple Watch Dog with 8 bit PIC

Post by jay_dee »

I recently had a problematic project that would lock up from random excessive noise.
Whilst we sorted out the root problem, I wanted to enable a Hardware Watchdog in the embedded project.
If the PIC crashed, the PIC would automatically reset and probalby run fine again.

I thought implmenting a WatchDog would be quite involved but it turned out to be really simple.

I have put together a little FC program for a PIC18F that flashed an LED and after 200 cycles, it fakes a Lock Up.
The WatchDog is triggered and reset the PIC, starting the process all over again.

The WatchDog is Enabled in the FC Configuration setting, same place you set the PIC version your using.
File > Project Options > For my PIC is was in section 0x2
The WatchDog Time-Out is 4mS x the PreScaler Value, which is also set in the Configuration settings.

You just need to consistantly run a single C command to stop the Watch Dog Triggering.

Code: Select all

CLRWDT();
I used a C component and placed it in a the main loop. Also made sure the main loop would never take longer than the WatchDog timer to cycle.

J.
Attachments
PIC18F_WatchDog_V1_WIP.fcfx
(24.5 KiB) Downloaded 2 times

chipfryer27
Valued Contributor
Posts: 1703
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 374 times
Been thanked: 588 times

Re: Simple Watch Dog with 8 bit PIC

Post by chipfryer27 »

Hi

There is an interesting story involving NASA and WDT. Basically years ago someone decided to save a few cents by not implementing. You can guess the rest... how much did that part we didn't fit cost?????

In the options screen you also have a check box you can enable to auto clear watchdog, so FC does it for you in delays etc.

If implementing, whilst testing it is a good idea to have some form of visual indication your code is starting, such as an LED (or a line in a display) perhaps disabling it further along. If strange things are happening and your LED is on you know WDT has kicked in.

It can also be used to wake up from sleep, which doesn't cause a reset, it just continues again from where it went to sleep (good idea to throw in a NOP after sleep too).

Really good feature.

Regards

Post Reply