0

Watch out for watchdog timers

Posted by Chris on Thursday, April 09, 2009
I learned a very important lesson while getting back into my PIC programming. And that is to never overlook the basics! I'm already onto about my fourth project (but only having one PIC and one breadboard, I put an idea together then as soon as it's working, rip it all apart again). Each time I've been caught out with the fuse settings on the chip - supposedly the very first thing to check when putting an application together.

Most PIC microcontrollers have a clever little thing called a watchdog timer.
It's basically like a time bomb which, when it goes off, resets your PIC.
If you don't know to look out for the watchdog timer, it can make a working project look like a duffer - the PIC looks like it's dead (when in actual fact, it's resetting itself every few hundred microseconds).

The idea behind the watchdog timer is that it counts to a number, using an independent oscillator to your main program. When it gets to that number, it resets your microcontroller and starts counting again!
What's the point in that? Well, if you write some dodgy code, or you're expecting a response from another device that has died, get stuck in an endless loop, or perhaps are just working in a noisy environment* - there are loads of reasons why - you might want the PIC to abort whatever it's doing and start again. Hence the need for a watchdog timer.

The thing to remember is that at frequent intervals in your code (exactly how often depends on the frequency of your main oscillator) you need to reset the watchdog timer. Just every now and again, pepper your code with CLRWDT (clear watchdog) instructions.
Or you could just do what most hobbyist coders do and disable it!

*working in a noisy environment as in putting components onto an electrically noisy board, with lots of power rails and switching power supplies, not you sitting in an office with Radio2 playing too loudly

0 Comments

Post a Comment

whos.amung.us

Copyright © 2009 .Nerd Club All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive Supported by Blogger Templates.