Electronics > Microcontrollers

PIC18F4520 watchdog timer

(1/2) > >>

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 set
                                return 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 working :( Has anyone successfully got WDT working on an 18F?


--- Code: ---if(RCONbits.NOT_POR)                            // if POR is set
// ...
if(RCONbits.NOT_TO)             // if TO is set

--- End code ---

Those 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. :P Maybe you have a code versus intent mismatch somewhere?

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, really :) e.g.

// If BOR is enabled
#if defined(BOR_ENABLED)

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 bit
1 = 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 subsequent
Power-on Resets may be detected.
--- End code ---

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. XD

STM32F4 libraries have the same thing. pfffffrt. I wonder if ST has "lines of code comments written" as a KPI for programmers or something.


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod