PIC18F4520 watchdog timer - Page 1
Forum section: electronics / general

author: joelby
author_rank: Frequent Contributor
timestamp: November 12, 2012, 12:47:42 pm
content: Hi all,I've been struggling with a watchdog problem on this PIC18F for a couple of days and was hoping that someone else might have some insight.I've enabled the watchdog timer and it works great, but for some reason I'm unable to detect a timeout event after a reset using the following C18 code:void main() {char foo;foo = isWDTTO();}This function is provided in the Microchip library, but the implementation boils down to:char isWDTTO(void){if(RCONbits.NOT_POR) // if POR is set{if(RCONbits.NOT_PD) // if NOT_PD is set{if(RCONbits.NOT_TO) // if TO is setreturn 0;return 1;}return 0;}return 0;}Unfortunately I don't have any 18F dev boards to double check that this should work, though something similar on a 16F works perfectly well.I literally am checking and saving the result of isWDTTO() in the first line: definitely before re-enabling SWDTEN and before calling ClrWdt.There's nothing in the errata about this not workingHas anyone successfully got WDT working on an 18F?

author: mrflibble
author_rank: Super Contributor
timestamp: November 12, 2012, 01:46:01 pm
content: Code:if(RCONbits.NOT_POR) // if POR is set// ...if(RCONbits.NOT_TO) // if TO is setThose have code that doesn't match the comment. I am not encumbered by any RCONbits related information so that's just a random shot in the dark here.Maybe you have a code versus intent mismatch somewhere?

author: joelby
author_rank: Frequent Contributor
timestamp: November 12, 2012, 01:51:37 pm
content: I've looked at the datasheet carefully and I think the function's doing the right thing, though tomorrow I'll check the actual value of RCON at startup. Unfortunately the device doesn't have an ICSP header so debugging is a slow and annoying process.The library source code is pretty awful, reallye.g.// If BOR is enabled#if defined(BOR_ENABLED)

author: PA0PBZ
author_rank: Super Contributor
timestamp: November 12, 2012, 02:08:30 pm
content: I agree it's a bit confusing, with double negative names and such but anyway, do you set the NOT_POR bit?Looks like you have to do that in the code:Code:bit 1 NOT_POR: Power-on Reset Status bit1 = A Power-on Reset has not occurred (set by firmware only)0 = A Power-on Reset occurred (must be set in software after a Power-on Reset occurs)It is recommended that the POR bit be set after a Power-on Reset has been detected so that subsequentPower-on Resets may be detected.

author: mrflibble
author_rank: Super Contributor
timestamp: November 12, 2012, 02:11:41 pm
content: Alrighty. I just thought I'd mention it since you did say it was from library code. And I've seen all sorts of crappy mistakes in library code. But since you checked the datasheets, that option is out. Bummer.Yeah, those comments that repeat the code almost verbatim are so awesomely useless. XDSTM32F4 libraries have the same thing. pfffffrt. I wonder if ST has "lines of code comments written" as a KPI for programmers or something.

author: joelby
author_rank: Frequent Contributor
timestamp: November 12, 2012, 10:35:38 pm
content: Quote from: PA0PBZ on November 12, 2012, 02:08:30 pmNo, but that's a very interesting observation! I suspect that reading the exact value of RCON might be informative here.A bit further down: "POR events are captured by the POR bit (RCON<1>). The state of the bit is set to ‘0’ whenever a POR occurs; it does not change for any other Reset event. POR is not reset to ‘1’ by any hardware event. To capture multiple events, the user manually resets the bit to ‘1’ in software following any POR."I wish that the documentation summarised all of these requirements in the "watchdog timer" section. No wonder people don't use the peripheral much.Anyway, will report back when I get a chance to test it today. Thanks for everyone's help so far!

author: joelby
author_rank: Frequent Contributor
timestamp: November 13, 2012, 06:11:49 am
content: PA0PBZ, I could kiss your mind!Thanks to you and setting the POR bit, WDT reset detection is now working properly.Cheers, all!