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...
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;
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
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?
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!
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.
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.