View previous topic :: View next topic |
Author |
Message |
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
Watchdog question |
Posted: Mon May 16, 2011 3:55 pm |
|
|
Hi,
I wanna use a wdt in my application. I'm using a 18F87K22 and have configured it like:
#fuses WDT1024
Now the problem is, the WDT is active before I execute setup_wdt(WDT_ON); thus I have put an explicit setup_wdt(WDT_OFF); into my code which doesn't seem to help at all.... :( Why is that?
Thanks for help! |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Mon May 16, 2011 4:25 pm |
|
|
My reason asking is because I wanna disable the WDT while i'm waiting for a user input. I put following in my app:
Code: | setup_wdt(WDT_OFF); //disable WDT while waiting for input
fgets (in_str, PC);
setup_wdt(WDT_ON); //re-enable watchdog | but it doesn't work correctly, while i'm waiting for user input, the watchdog still kicks in and resets my mcu... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 16, 2011 4:28 pm |
|
|
Quote: | I'm using a 18F87K22 and have configured it like:
#fuses WDT1024
|
The P18F87K22 data sheet tells you what you have to do, to enable
software control of the Watchdog:
Quote: |
28.2.1 CONTROL REGISTER
Register 28-16 shows the WDTCON register. This is a
readable and writable register which contains a control
bit that allows software to override the WDT Enable
Configuration bit, but only if the Configuration bit has
disabled the WDT. |
|
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Mon May 16, 2011 4:37 pm |
|
|
Uhm, okay
I tried this:
Code: | #byte WDTCON = getenv("sfr:WDTCON")
#bit SWDTEN = WDTCON.0 |
and then do this to disable:
Code: | setup_wdt(WDT_OFF);
SWDTEN = 0; |
But that doesn't seem to do it either... any clues?
Thank you! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 16, 2011 4:56 pm |
|
|
This program shows how to do it with vs. 4.121. The PIC will start-up
and the LED will be off for 1 second. Then it will go on for 4 seconds,
then the PIC will do a WDT reset, and the process begins again. The
LED will keep blinking: 1 second off, 4 seconds on.
I don't have an 18F87K22, but I have a PIC in same family. I tested this
on a PicDem2-Plus board (non-Rohs version).
Code: |
#include <18F45K22.h>
#fuses INTRC_IO, WDT_SW, WDT1024, NOPLLEN, NOPBADEN
#use delay(clock=4M)
//===================================
void main()
{
output_low(PIN_B0); // LED off
delay_ms(1000); // Wait one second
output_high(PIN_B0); // LED on
setup_wdt(WDT_ON); // Enable WDT. It will reset in 4 seconds.
while(1);
} |
|
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Mon May 16, 2011 5:13 pm |
|
|
Weird... I haven't tried your code yet but just figured something weird out myself:
I have
Code: | #byte WDTCON = getenv("sfr:WDTCON")
#bit SWDTEN = WDTCON.0
#byte CONFIG2H = getenv("sfr:CONFIG2H")
#bit WDTEN0 = CONFIG2H.0
#bit WDTEN1 = CONFIG2H.1
|
and they are all 0 but the WDT still kicks in and resets the mcu... why is that? What am i missing? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 16, 2011 5:42 pm |
|
|
You can't write directly to Config registers in code. The PIC data sheet
explains this in section 28.1 Configuration Bits.
CCS has a function for it:
Quote: |
write_configuration_memory( )
Syntax:
write_configuration_memory (dataptr, count) |
But it's totally, utterly unnecessary, since I showed you how to do it
with the #fuses statement. |
|
|
cerr
Joined: 10 Feb 2011 Posts: 241 Location: Vancouver, BC
|
|
Posted: Tue May 17, 2011 9:53 am |
|
|
Having set the fuses like Code: | #fuses WDT1024,WDT_SW | did it. I assume I had to turn on the Software switch SWDTEN in with the WDT_SW fuses switch in order to be able to switch the WDT on off with setup_wdt() |
|
|
|