Unknown author:
How to use WDT?
I am no expert on PIC. So, I will show what I did and explain what I understand.
- Set CONFIG word to enable WDT
- In the code I set up 2 functions: FeedDog() and KickDog()
- Placing FeedDog() and KickDog() in the main program
I use mikroC compiler, so I uncheck the default WDT_OFF = $3FFB and check _WDT_ON = $3FFF
void FeedDog(){ //Setting WDT
OPTION_REG = 0x0F; //0b00001111 = Prescalar 1:128 .. reset every 2.3 sec. please consult data sheet for other presacalars
asm{CLRWDT} //Clear WDT
}
void KickDog(){
asm{ CLRWDT } //Clear WDT before the dog resets the PIC
}
I put FeedDog() around the begining of the program to set up WDT and put KickDog() wherever I think the dog will come. If the program is very long, I can put KickDog() in many places as I want if I think the dog will come after that line of code. It is upto me to calculate where to put the KickDog(). In my case, the dog keeps comming every 2.3 sec so I have to kick the dog before 2.3 sec after each kick. My clock update every second, so if it freeze and cannot kick the dog in 2.3 sec the PIC will be reset.
Putting KickDog() in the wrong place can make your program running incorrectly. Please be careful.
void main(){
.. Set up program ..
FeedDog();
.. Set up some more things..
while(1){
...
.. Do tasks ..
KickDog();
...Do tasks..
....
}
}
Post a Comment