|
|
View previous topic :: View next topic |
Author |
Message |
bcfd36
Joined: 14 Apr 2015 Posts: 28 Location: Boulder Creek, CA
|
Watchdog Timer Issues/Questions |
Posted: Wed Apr 29, 2015 5:29 pm |
|
|
The following small program is supposed to do the following:
At startup, flash the LED 5 times for 1/2 second, then go into a delay (LED is ON) that should trigger the watchdog. When the watchdog is triggered, flash the LED 3 times at 250 ms intervals then leave the light on for 5000 ms before turning it off. Then return back to the main routine again and restart the whole thing..
This is not what is happening. I see the first 5 1/2 second flashes, then the 3 .25 second flashes, followed by what looks to be a slight hesitation, then 4 quick flashes. Followed a hesitation and 4 quick flashes. ad infinitum.
What am I missing? I am using an 18LF45K22 and PCWHD compiler version 5.018.
Code: | #include <18LF45K22.h>
#device ADC=10
#FUSES WDT512 //Watch Dog Timer
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES INTRC_IO //use internal osc, with i/0
#use delay(internal=32000000)
#include <stdlib.h>
#define TEST_LED PIN_C0 //!< Digital output assignment, active high, turn on green test led
int8 restart_reason = 0 ;
//*****************************************************************************
// Determine Restart Mode
//*****************************************************************************
int8 Restart_Mode(void)
{
int cntr = 0 ;
unsigned int8 Cause;
Cause = restart_cause(); //< CCS compiler library function restart_cause() // Determine reason for restart
if(Cause == WDT_TIMEOUT)
{
setup_wdt(WDT_OFF); // disable SW watchdog
while(1)
{
output_low(TEST_LED) ;
delay_ms(250); // !<Do a 1/8 sec delay
output_high(TEST_LED) ;
delay_ms(250); // !<Do a 1/8 sec delay
cntr++ ;
if(cntr > 3)
{
cntr = 0 ;
break ;
}
}
delay_ms(5000) ;
output_low(TEST_LED) ;
}
return Cause;
}
//*****************************************************************************
// Main Routine
//*****************************************************************************
void main()
{
unsigned int32 cntr ;
setup_wdt(WDT_OFF); // disable SW watchdog
restart_reason = Restart_Mode() ;
output_high(TEST_LED); // Turn PCB green LED on to confirm initilaization
// setup_wdt(WDT_ON); // enable SW watchdog
cntr = 0 ;
while(TRUE)
{
restart_wdt() ;
delay_ms(500); // .5 second delay
output_low(TEST_LED) ;
delay_ms(500); // .5 second delay
output_high(TEST_LED) ;
cntr++ ;
if(cntr >= 5)
{
delay_ms(1500) ;
cntr = 0 ;
}
} // End while
} |
Note the commented out setup_wdt(WDT_ON). It looks like the restart_wdt call turns the watchdog back on. Is this the case? The manual doesn't explicitly say.
I never seem to be getting out of the restart routine. Why is it not doing slow flashes followed by quick flashes followed by 5 seconds on, then all that repeated?
Also, the way this is set up, what is the length of time before the watchdog times out? What I thought it would be and what it is, are two different things. _________________ D. Scruggs |
|
|
bcfd36
Joined: 14 Apr 2015 Posts: 28 Location: Boulder Creek, CA
|
|
Posted: Wed Apr 29, 2015 5:54 pm |
|
|
As an addendum, if the delay in the Restart routine is removed, I see the expected behavior (less of course the LED on for 5 seconds in the Restart routine).
I can move the delay in the main routine down to 1170 ms and the watchdog still triggers. If I set it to 1160 the watchdog does NOT trigger. _________________ D. Scruggs |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9246 Location: Greensville,Ontario
|
|
Posted: Wed Apr 29, 2015 5:59 pm |
|
|
Though I never use the 'watchdog', according to the datasheet it's 'time' is NOT very accurate. something like +-100% of value.
Others will know.....
Jay |
|
|
bcfd36
Joined: 14 Apr 2015 Posts: 28 Location: Boulder Creek, CA
|
|
Posted: Wed Apr 29, 2015 6:46 pm |
|
|
Temtronic,
I know the time is extremely inaccurate, but it is close enough for what we are doing. We are just trying to catch if the chip gets lost and goes and starts executing data somewhere. If so, the timeout will allow a restart.
There is probably a better way to do that, but I'm new in this area and am still coming up to speed.
Thanks.
D. Scruggs _________________ D. Scruggs |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Thu Apr 30, 2015 12:00 am |
|
|
You are not re-enabling the watchdog. So your LED will flash 4 times in the main loop and then wait 1.5 seconds and then flash again.
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Thu Apr 30, 2015 12:34 am |
|
|
First thing. You try to use SWDTEN, to control the watchdog. Look carefully at the data sheet. Look at register 24-3. Note that the only setting for the WDTEN bits, that allows software control is '10'. Is this what you have?.
The CCS fuse to give this is WDT_SW. Without this the watchdog hardware will always be on, if it is enabled in the fuses.....
No, restart_wdt, does not turn the watchdog back on. Yours is permanently on....
Then time. The watchdog on your chip is one of the more accurate ones. On some old chips the timings for (on these chips) the 18mSec nominal time, is between 7mSec and 32mSec. So 0.38* to 1.77*. Ouch. Your chip the nominal time is 3.5 to 4.7mSec. So with 512 prescaler, 1792mSec to 2406mSec, with a 'typical' time of 2100mSec.
Now your main routine delay, has 1000mSec of 'flash' in front of it. So your watchdog is restarting if the total loop time is >2160mSec. Right in the middle of the expected time.
The watchdog is a counter. Just like the timers. The watchdog triggers _when the counter overflows at the top_. Just like any other counter, when it overflows, it is back at 0 for the next count. restart_wdt, just resets the counter, nothing else. |
|
|
bcfd36
Joined: 14 Apr 2015 Posts: 28 Location: Boulder Creek, CA
|
|
Posted: Thu Apr 30, 2015 10:18 am |
|
|
Ttlmah,
Thank you very much. This looks like it will be quite helpful. I will get back to you with results.
D. Scruggs _________________ D. Scruggs |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|