Author Topic: PIC18F4520 watchdog timer  (Read 3985 times)

0 Members and 1 Guest are viewing this topic.

Offline joelbyTopic starter

  • Frequent Contributor
  • **
  • Posts: 634
PIC18F4520 watchdog timer
« on: November 12, 2012, 12:47:42 pm »
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?
 

Offline mrflibble

  • Super Contributor
  • ***
  • Posts: 2051
  • Country: nl
Re: PIC18F4520 watchdog timer
« Reply #1 on: November 12, 2012, 01:46:01 pm »
Code: [Select]
if(RCONbits.NOT_POR)                            // if POR is set
// ...
if(RCONbits.NOT_TO)             // if TO is set

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?
« Last Edit: November 12, 2012, 01:48:49 pm by mrflibble »
 

Offline joelbyTopic starter

  • Frequent Contributor
  • **
  • Posts: 634
Re: PIC18F4520 watchdog timer
« Reply #2 on: November 12, 2012, 01:51:37 pm »
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)

 

Offline PA0PBZ

  • Super Contributor
  • ***
  • Posts: 5185
  • Country: nl
Re: PIC18F4520 watchdog timer
« Reply #3 on: November 12, 2012, 02:08:30 pm »
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: [Select]
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.
Keyboard error: Press F1 to continue.
 

Offline mrflibble

  • Super Contributor
  • ***
  • Posts: 2051
  • Country: nl
Re: PIC18F4520 watchdog timer
« Reply #4 on: November 12, 2012, 02:11:41 pm »
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.
 

Offline joelbyTopic starter

  • Frequent Contributor
  • **
  • Posts: 634
Re: PIC18F4520 watchdog timer
« Reply #5 on: November 12, 2012, 10:35:38 pm »
I agree it's a bit confusing, with double negative names and such but anyway, do you set the NOT_POR bit?

No, 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!
 

Offline joelbyTopic starter

  • Frequent Contributor
  • **
  • Posts: 634
Re: PIC18F4520 watchdog timer
« Reply #6 on: November 13, 2012, 06:11:49 am »
PA0PBZ, I could kiss your mind!

Thanks to you and setting the POR bit, WDT reset detection is now working properly.

Cheers, all!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf