Author Topic: PIC Watchdog timer question  (Read 2335 times)

0 Members and 1 Guest are viewing this topic.

Offline fubar.grTopic starter

  • Supporter
  • ****
  • Posts: 370
  • Country: gr
    • Fubar.gr
PIC Watchdog timer question
« on: March 07, 2022, 12:19:09 pm »
Hello, I am trying to figure out how the watchdog timer works on a PIC12F1571 in Proteus with XC8 compiler and came across a weird issue.

I am setting WDTCONbits.WDTPS = 0b01110 which should change the WDT period to 16 seconds, but Proteus still says that the processor was reset after (the default) 2 seconds. I tried various values for WDTPS but I was still getting the default 2 second period, as if the WDTPS value is totally ignored.

I tried various things, but what finally worked for me was to call CLRWDT() once, outside the main loop, then the watchdog timer reset would kick in at a time period according to WDTPS and work as intended.

Is this documented somewhere?

My full code is here

Code: [Select]
#include <xc.h>
#pragma config FOSC = INTOSC

#define _XTAL_FREQ 2000000
#pragma config WDTE = SWDTEN

void main(void) {
   OSCCONbits.IRCF = 0b1100;
   WDTCONbits.SWDTEN = 0b1;
   WDTCONbits.WDTPS = 0b01110;
   CLRWDT();
   
    TRISA = 0;
   LATA = 0;
   
    while(1){
    LATA = ~LATA;
    __delay_ms(1000);
     
    LATA = ~LATA;
    __delay_ms(1000);
       
    }
    return;
}

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 14134
Re: PIC Watchdog timer question
« Reply #1 on: March 07, 2022, 01:22:17 pm »
There are no silicon errata for the WDT, nor mention of needing to CLRWDT after changing the WDT prescaler, (though doing so before and after is advisable to prevent a shortened  initial WDT period as there is nothing to indicate the WDT and prescaler are cleared when the prescaler setting is changed), so I would suspect a Proteus bug.  Write test code (e.g. toggle a pin and SLEEP till WDT timeout) and try it on real hardware.
 
The following users thanked this post: fubar.gr

Offline fubar.grTopic starter

  • Supporter
  • ****
  • Posts: 370
  • Country: gr
    • Fubar.gr
Re: PIC Watchdog timer question
« Reply #2 on: April 02, 2022, 11:07:33 am »
Thanks!

I tested it on real hardware and the WDT works as expected, so it was a Proteus bug

Offline MikeK

  • Super Contributor
  • ***
  • Posts: 1364
  • Country: us
Re: PIC Watchdog timer question
« Reply #3 on: April 02, 2022, 07:38:32 pm »
FYI: Your code above defines the xtal frequency as 2MHz...Did you mean 20MHz?
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 14134
Re: PIC Watchdog timer question
« Reply #4 on: April 02, 2022, 07:43:20 pm »
FYI: Your code above defines the xtal frequency as 2MHz...Did you mean 20MHz?
I'd bet *NO* as 20MHz isn't a supported INTOSC frequency, and his OSCCONbits.IRCF bits are correct for 2MHz.  8)
 

Offline MikeK

  • Super Contributor
  • ***
  • Posts: 1364
  • Country: us
Re: PIC Watchdog timer question
« Reply #5 on: April 02, 2022, 07:45:46 pm »
FYI: Your code above defines the xtal frequency as 2MHz...Did you mean 20MHz?
I'd bet *NO* as 20MHz isn't a supported INTOSC frequency, and his OSCCONbits.IRCF bits are correct for 2MHz.  8)

Sorry.  My mistake for not realizing it was INTOSC.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 14134
Re: PIC Watchdog timer question
« Reply #6 on: April 02, 2022, 07:49:29 pm »
The odds would have been in your favor if our O.P. hadn't posted their full config and init code.

For readability, it can be worth defining:
Code: [Select]
#define KHz *1000UL
#define MHz *1000000UL
so wherever frequencies are specified you can use S.I. multipliers, e.g:
Code: [Select]
#define _XTAL_FREQ 2 MHz
« Last Edit: April 02, 2022, 07:55:37 pm by Ian.M »
 
The following users thanked this post: MikeK, woofy


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->