| View previous topic :: View next topic   | 
	
	
	
		| Author | 
		Message | 
	
	
		
			soonc
 
 
  Joined: 03 Dec 2013 Posts: 215
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| PIC18F47K42 and WDT | 
			 
			
				 Posted: Sat Mar 21, 2020 11:28 am     | 
				     | 
			 
			
				
  | 
			 
			
				Using the PIC18F47K42 I would like to use the WDT selectively.
 
 
So I figured I need to set the FUSE
 
and the first thing in main() switch off the WDT as example below. 
 
 	  | Code: | 	 		  
 
#include <18F47K42.h>
 
#FUSES WDT
 
 
void main()
 
{
 
  setup_wdt( WDT_OFF );  // does not stop the WDT it resets the cpu
 
 
  rest of code here....
 
}
 
 | 	  
 
 
However the code just does a reset about once per second. 
 
 
How to selectively enable / disable the WDT ? 
 
 
Here is code I would like to use: 
 
 
 	  | Code: | 	 		  
 
#include <18F47K42.h>
 
 
#fuse WDT
 
 
#use delay(clock = 12000000, crystal = 12000000) 
 
 
void main()
 
{
 
     int32 g_n32WDTCounter=0;
 
     
 
     setup_wdt( WDT_OFF );    
 
 
         
 
 
     enable_interrupts(GLOBAL);
 
 
     /////////////////////////////////////////////////////////////////////
 
     // eventually using DS32KHz as SOSC as a more accurate clk for the WDT
 
     setup_wdt( WDT_ON | WDT_1S | WDT_WINDOW_100_PERCENT | WDT_CLK_31000);
 
     
 
    // at some point in future code call this....
 
     do{
 
 
        restart_wdt();
 
                   
 
        sleep( SLEEP_FULL );
 
        delay_cycles(1);// nop after sleep
 
        
 
        output_toggle( PIN_B3 ); // for scope monitoring        
 
        
 
        g_n32WDTCounter++;
 
     
 
     }while( g_n32WDTCounter < 30 ); // about 30 seconds    
 
        
 
     setup_wdt( WDT_OFF );    
 
     reset_cpu();         
 
}
 
 | 	  
 
 
What is it I'm not understanding about the WDT ! | 
			 
		  | 
	
	
		  | 
	
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat Mar 21, 2020 12:52 pm     | 
				     | 
			 
			
				
  | 
			 
			
				You have to turn off the hardware enable, to let the software 
 
enable/disable work.  Example:
 
 | 
			 
		  | 
	
	
		  | 
	
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat Mar 21, 2020 1:02 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Key thing is the line in the data sheet (registers 5-5), where the options 
 
that enable the watchdog in the fuses all have the note: 'SWDTEN is 
 
ignored'.
 
So as PCM_Programmer says if you want software control of the WDT,
 
you can't turn it on in tghe fuses.... | 
			 
		  | 
	
	
		  | 
	
	
		
			soonc
 
 
  Joined: 03 Dec 2013 Posts: 215
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| Thanks for the help | 
			 
			
				 Posted: Sat Mar 21, 2020 4:54 pm     | 
				     | 
			 
			
				
  | 
			 
			
				Thanks for the help. 
 
I've tried several combinations of FUSES but still can make it work. | 
			 
		  | 
	
	
		  | 
	
	
		
			PCM programmer
 
 
  Joined: 06 Sep 2003 Posts: 21708
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat Mar 21, 2020 5:35 pm     | 
				     | 
			 
			
				
  | 
			 
			
				I assume you mean "still can not" make it work ?
 
 
Post your current test program. | 
			 
		  | 
	
	
		  | 
	
	
		
			temtronic
 
 
  Joined: 01 Jul 2010 Posts: 9589 Location: Greensville,Ontario 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat Mar 21, 2020 6:30 pm     | 
				     | 
			 
			
				
  | 
			 
			
				just a comment...
 
 
you should not enable interrupts without 'handlers' ( ISRs)...
 
 
your program enables the 'global', so if the 'wizard' has enabled specific interrupts, unknown to you... they WILL cause you random problems....I don't know if you use the 'wizard' or other 'helper' butone programmer's idea of defaults won't ever be what you KNOW the 'defaults' need to be.... | 
			 
		  | 
	
	
		  | 
	
	
		
			soonc
 
 
  Joined: 03 Dec 2013 Posts: 215
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sat Mar 21, 2020 7:50 pm     | 
				     | 
			 
			
				
  | 
			 
			
				 	  | PCM programmer wrote: | 	 		  I assume you mean "still can not" make it work ?
 
 
Post your current test program. | 	  
 
 
Thanks PCM P I meant cannot make it work... But I did and it's going ok now...
 
Thanks for the help. | 
			 
		  | 
	
	
		  | 
	
	
		
			temtronic
 
 
  Joined: 01 Jul 2010 Posts: 9589 Location: Greensville,Ontario 
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Sun Mar 22, 2020 2:38 pm     | 
				     | 
			 
			
				
  | 
			 
			
				I have to wonder why an 'accurate' WDT is needed ?
 
WDTis only enabled after final program is made and then 'timeout' is usual computed as 1.2 - 2 x  a 'failsafe' value.
 
Say main() does everything and updates a 'flag' about 5 second interval. WDT could be set to 7-10 seconds.
 
There's probably no need for subsecond accuracy. | 
			 
		  | 
	
	
		  | 
	
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Mon Mar 23, 2020 2:26 am     | 
				     | 
			 
			
				
  | 
			 
			
				What he is trying to do, is reasonable. The way to use the WDT, with 
 
SOSC, is:
 
 	  | Code: | 	 		  
 
 
#define WDT_SOSC 0x2000
 
 
 
   setup_wdt( WDT_ON | WDT_1S | WDT_WINDOW_100_PERCENT | WDT_SOSC); 
 
 | 	  
 
By default the standard settings only support operation off the 31K, or 
 
31K25 internal clock. 
 
 
This correctly puts 0x27 into the 0x395C register, to select SOSC.
 
 
There are always individual settings that get 'missed' like this, and
 
you just have to create the extra settings when this happens. | 
			 
		  | 
	
	
		  | 
	
	
		 |