Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Watchdog Timer Q

Status
Not open for further replies.

wuchy143

Member
Hi all,

I'm trying to get familiar using a watchdog timer and using the prescalar with it. Background: I'm using the PIC12F675, High Tech C LIte Compiler, and MPSIM which comes with MPLAB IDE. I'm able to get the WDT to go over and halt execution in my simulation. I also see the bit TO in my SFR STATS go to "0" indicating a WDT timeout occured. This event takes the expected 18ms to occur which I verify using the stopwatch in the IDE. Though, when I apply my prescalar to the OPTION_REG it still only does 18ms and not the expected 2.3s(when using a prescalar of 128)(18ms X 128 = 2.3s)

It's as if I'm not assigning the prescalar correctly but in any manuals I read I'm correct. I"m stumped......

Code:

#include <htc.h>


__CONFIG(MCLREN & UNPROTECT & WDTEN & INTIO);

void main()
{

for(;; ) {


CMCON = 0x07; // Digital I/O
ANSEL = 0; // Digital I/O
TRISIO = 0x00000000; // Set Pin I/O
//STATUS = 0x00;
OPTION = 0x00001111; //Enableing Watchdog and setting prescaler to 128 X 18ms = 2.3ms





}



}
 
Do I need to kick the dog?? I have sort of answered my own question here where I found the instruction "CLRWDT()" which enables me to clear the WDT. Though, now the watchdog never goes off. All I'd like to do is have it so the watchdog goes off after 2.3seconds...
 
Last edited:
__CONFIG(MCLREN & UNPROTECT & WDTEN & INTIO);

You can't use WDTEN and use CLRWDT

You have to have watch dog turn off in your config
then turn it on in your main code
 
You can't use WDTEN and use CLRWDT

You have to have watch dog turn off in your config
then turn it on in your main code

HI Burt,

Thanks for the reply. First for my understanding why can't you use WDTEN and use CLRWDT?

Second. I went and turned it on it my main code then I loop forever. Below is my code but it still runs and runs past 2.3 seconds without a reset. What am I missing?

#include <htc.h>


__CONFIG(MCLREN & UNPROTECT & WDTDIS & INTIO);


void main()
{
CMCON = 0x07; // Digital I/O
ANSEL = 0; // Digital I/O
TRISIO = 0x00000000; // Set Pin I/O
//STATUS = 0x00;
OPTION = 0x00001111; //Setting prescaler to 128 X 18ms = 2.3ms
WDTEN;

for(;; )
{
}
}
 
Last edited:
If it's enabled in the config it will keep runing

If you set it on in main code then you can set it to stop at will and then restart it

Look at this code
int i;

main()
{

OPTION = 0b11011111; // Run TMR0 from the Clock Oscillator

i = 0; // Demonstrate Operation of TMR0
while (i < 1000)
i = i + 1;

// Demonstrate a fairly precise 200 us delay

TMR0 = 56; // Initialize TMR0/Breakpoint Here
T0IF = 0; // Turn off Pending Interrupt Requests
T0IE = 1; // Enable TMR0 Overflow to Request Ints
while (!T0IF); // Wait for TMR0 to Overflow
NOP(); // Breakpoint Here - Time to Previous

while (1 == 1); // Loop Forever

} // End cTMR0
 
aha! I think i'm picking up what you're laying down :) Let me work on this a little more and see if I can get it to work how I want.

-mike
 
Burt,

Instead of saying I'm stumped(which I sort of am) I'm going to show you what I understand and ask questions on things I don't understand....lets hope you don't get frustrated with my inch thick head. :)

1. "OPTION = 0b11011111; // Run TMR0 from the Clock Oscillator"
I get that this is making the TMR0 timer to count(increment) on every high to low transition on the GP2/CLK pin.

2. "i = 0; // Demonstrate Operation of TMR0
while (i < 1000)
i = i + 1;

I am taking a stab that this code is only there to get TMR0 counting. And that's the only function of this loop.

3."TMR0 = 56; // Initialize TMR0/Breakpoint Here"
Not sure why we are setting TMRO to 56. Is this a way to get it to a known state?

4."T0IF = 0; // Turn off Pending Interrupt Requests"
Why would you need to turn off pending interrupt requests?

5."T0IE = 1; // Enable TMR0 Overflow to Request Ints"
?????

6."while (!T0IF); // Wait for TMR0 to Overflow"
You are polling the interrupt overflow flag bit to see if TMRO has overflowed.

....so this is where I'm at. I"m going to continue to try and get a better handle on this as you have already gone above and beyond what I'd expect from a forum. Thanks!

-mike
 
There seems to be some confusion about the WDT. The WDT is turned on and off in the config value, there is no way to turn it on and off in software. The CLRWDT instruction resets the WDT to zero. I have no idea why the prescaler isn't working. Try placing a single CLRWDT in the initialisation code and see if that fixes it.

Mike.
 
Last edited:
To use the Timer0 In hi -tech you have to disable the wdt in the config

Then you can use CLRWDT in your main code to reset the timer

The prescaler assignment is fully under software
control (i.e., it can be changed “on the fly” during
program execution). To avoid an unintended device
RESET, the following instruction sequence
(Example 4-1) must be executed when changing the
prescaler assignment from Timer0 to WDT.

I don't have Hi-tech on this computer But give me a min and I will and post what it tell about using clrwdt

I don't mean you can stop the timer per say but you can clear it in software
 
Last edited:
Burt,

The question was about the WDT not Timer0. Why are you posting Timer0 examples?

Mike.
 
I'm sorry I some what changed this to timer0

Here why your code was not working

Code:
#include <htc.h>

#define _XTAL_FREQ  4000000     // oscillator frequency for delay

__CONFIG(MCLREN & UNPROTECT & BORDIS & WDTEN & PWRTEN & INTIO);

void main()
{
 // configure watchdog timer
    OPTION = 0b00001111;            // set WDT timeout:
             //----1---                 prescaler assigned to WDT (PSA = 1)
             //-----111                 prescale = 128 (PS = 111)
                                    //  -> WDT timeout = 2.3 s
 {
        CLRWDT();               //   clear watchdog timer
    }
}

See if this don't work for you
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top