Implement watchdog timer

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
GTF
Posts: 170
Joined: Sat Dec 10, 2011 7:21 pm
Location: Canada
Has thanked: 20 times
Been thanked: 52 times
Contact:

Implement watchdog timer

Post by GTF »

I would like to implement the watchdog timer of a 18F14K50 (http://ww1.microchip.com/downloads/en/D ... 41350E.pdf) for only the main loop of my code. Config settings in my USB bootloader have the WDT disabled and the postcaler set to 1. My main loop cycles at about 5 kHz. Will adding the code shown in the attached example do the trick to reset the device if the program should hang for greater than 4msec? I have tested the actual program with this added code and it still compiles and runs as expected. I guess I should also check the WD time-out flag (bit 3 of the RCON register) at startup.
Attachments
WDT_example.fcfx
(3.55 KiB) Downloaded 300 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Implement watchdog timer

Post by Benj »

Hello,

Your program looks spot on to me.

If you instead wanted to use C code instead of assembler then you would enable the timer like this.

Code: Select all

wdtcon = 1;
And reset the timer like this.

Code: Select all

MX_CLEAR_WATCHDOG;
Shouldn't really matter though as the output hex file will likely be exactly the same either way.

In the Build -> Project Options menu there is a setting under the General Options tab to allow Flowcode to auto clear the watchdog when using millisecond or second delays, just in case you need any large delays or call any components that use them.

GTF
Posts: 170
Joined: Sat Dec 10, 2011 7:21 pm
Location: Canada
Has thanked: 20 times
Been thanked: 52 times
Contact:

Re: Implement watchdog timer

Post by GTF »

Thanks Benj. Will the code I have added at the beginning of the following example work to set a flag in my program if a WD time-out has occurred? Or is there a better way?
Attachments
WDT_example.fcfx
(3.87 KiB) Downloaded 291 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Implement watchdog timer

Post by Benj »

Hello,

I might do it using C code but yes it looks fine to me.

C code:

Code: Select all

FCV_WDT_FLAG = ((rcon & 0x08) >> 3);

GTF
Posts: 170
Joined: Sat Dec 10, 2011 7:21 pm
Location: Canada
Has thanked: 20 times
Been thanked: 52 times
Contact:

Re: Implement watchdog timer

Post by GTF »

Thanks. Seems I did not have the asm code to test the WDT reset flag quite right to keep BoostC happy. It compiles OK now. Not certain that I need the backslash to separate the asm lines...I think I read it somewhere in the BoostC manual. It seems to compile the same with or without it.
Attachments
WDT_example.fcfx
(3.87 KiB) Downloaded 300 times

Post Reply