Please help with configs/defines for PIC12F609
	
	
		I'm having trouble with this project.  I'm using a PIC12F609 and a pushbutton to turn a dual latching 2formC relay on and off.  An LED comes on when the relay is on.  This is being used to route an audio signal.  The "mute" part of the code uses a MOSFET to short the signal path to ground so that you cannot hear the relay contacts  when it switches.
Everything functions exactly the way I want it to.  The problem I'm having is that after 30 minutes or so of sitting idle, the circuit will spontaneously change from the "on" state to the "off" state, or vice versa.  And eventually, the program will lock up after sitting idle for about an hour.
I guess I'm doing something wrong in the configuration/define, but I can't figure out what I'm doing wrong.  Any help would be greatly appreciated.
	Code:
	
'****************************************************************'*  Name    : relayswitch2.BAS                                                                       *
'*  Author  : Keith Vonderhulls                                                                       *
'*  Notice  : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS]                     *
'*          : All Rights Reserved                                                                         *
'*  Date    : 9/23/2014                                                                                  *
'*  Version : 1.0                                                                                            *
'*  Notes   :                                                                                                  *
'*          :                                                                                                     *
'****************************************************************
#config
    __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _IOSCFS_4MHZ & _CP_OFF
#endconfig
DEFINE OSCCAL_1k 1
    ANSEL = 0 ' Set all digital
    CMCON0 = 7 ' Analog comparators off
    CMCON1 = 7 ' Analog comparators off
    TRISIO = %10001000
    
LATCH0 var GPIO.2       'ON latch coil of relay
LATCH1 var GPIO.1       'OFF latch coil of relay
LED var GPIO.4
MUTE var GPIO.0
PB Var GPIO.3           ' Alias GPIO.3 to push button
    
    LED = 0
    MUTE = 0
    LATCH1 = 1              'initiate program with LED, MUTE and relay off
    
    pause 10                'allow 10ms pulse to relay coil to turn off 
    LATCH1 = 0              'end pulse to relay coil to conserve power
    
main:
    
    if PB = 0 then
        MUTE = 1                     'mute while switching occurs
        TOGGLE LED                 'change LED status  
        pause 30                    'mute & debounce
            gosub activaterelay
    
        pause 30                        'allow another 30ms to mute
        MUTE = 0
            gosub switchrelease
        pause 50                        'debounce
    endif
    
goto main
End
activaterelay:                          'change state of relay base upon state of LED
    
    if LED = 1 then
        LATCH0 = 1
    elseif LED = 0 then
        LATCH1 = 1      
    endif
    
    pause 10                 ' send 10ms pulse to relay coil
    
    if LED = 1 then        '  end pulse to relay coil to conserve power
        LATCH0 = 0
    elseif LED = 0 then
        LATCH1 = 0      
    endif
    
return
switchrelease:                  'wait for footswitch to be released
    do until PB = 1
        pause 10
    loop
return
 
	 
	
	
		1 Attachment(s)
	
	
		Re: Please help with configs/defines for PIC12F609
	
	
		Attachment 7452
Here's the schematic for the circuit
	 
	
	
	
		Re: Please help with configs/defines for PIC12F609
	
	
	
	
	
		Re: Please help with configs/defines for PIC12F609
	
	
		Page 98 (sect. 11.3.6) of the PIC12F609 datasheet discusses TIME-OUT SEQUENCE.  I think this may be where I'm having problems, but it's going over my head.
	 
	
	
	
		Re: Please help with configs/defines for PIC12F609
	
	
		Add a capacitor or two across VDD and VSS near the PIC (1uF or so)
And maybe tell us about your power supply.
	 
	
	
		1 Attachment(s)
	
	
		Re: Please help with configs/defines for PIC12F609
	
	
		Attachment 7453
Here's an updated schematic.  It shows the power supply.  I also forgot to include a 0.1uF cap off of GPIO.3 to help with debounce.
	 
	
	
	
		Re: Please help with configs/defines for PIC12F609
	
	
		Well I for one would NEVER operate a relay directly from the processor. looking at the data sheet for the relay, It draws 22 ma. Its bad enough for a pin that is sinking but it looks as if you are also sourcing the current to the coil. Use a transistor to switch the coil and you also get an extra pin to use. Also, I see NO diode to prevent backemf form the relay.
	 
	
	
	
		Re: Please help with configs/defines for PIC12F609
	
	
		
	Quote:
	
		
		
			
				Originally Posted by 
Dave
				
			 
			Well I for one would NEVER operate a relay directly from the processor. looking at the data sheet for the relay, It draws 22 ma. Its bad enough for a pin that is sinking but it looks as if you are also sourcing the current to the coil. Use a transistor to switch the coil and you also get an extra pin to use. Also, I see NO diode to prevent backemf form the relay.
			
		
	 
 Thanks, Dave.  I actually am using a 2N3904 to supply the power to the coils.  the MCU connects to the base of each transistor.  The collector of each transistor connects to VDD.  And the emitter of each transistor connects to its respective coil.  I don't know why I didn't draw them on the schematic.  I guess I didn't think that part was relevant to my specific problem.
Anyhow!
I resolved the issue.  It was a hardware/physical design problem.  The .1uF capacitor off of GPIO.3 was causing the problem.  I added it to help make the switch less bouncy, but this isn't a good idea when using the MCLR pin when MCLR is turned off.  I removed that capacitor and everything works perfectly now.