PDA

View Full Version : Please help with configs/defines for PIC12F609



keithv
- 23rd September 2014, 20:24
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.





'************************************************* ***************'* 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

keithv
- 23rd September 2014, 20:50
7452

Here's the schematic for the circuit

keithv
- 23rd September 2014, 20:59
http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CCAQFjAA&url=http%3A%2F%2Fww1.microchip.com%2Fdownloads%2Fe n%2FDeviceDoc%2F41302A.pdf&ei=wcIhVIadH4e2ogSoiILoBA&usg=AFQjCNFa65iZekLsmNgLQ5Usgb4NHlrPTw&sig2=yubZQLswYQxiT-yfYcmvGg&bvm=bv.75775273,d.cGU

Here's a link to the PIC12F609 datasheet

keithv
- 23rd September 2014, 21:46
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.

mackrackit
- 24th September 2014, 00:14
Add a capacitor or two across VDD and VSS near the PIC (1uF or so)
And maybe tell us about your power supply.

keithv
- 24th September 2014, 01:01
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.

Dave
- 24th September 2014, 13:31
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.

keithv
- 24th September 2014, 18:27
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.