I/O Pins latching - Page 1
Forum section: electronics / projects

author: ctesla75
author_rank: Contributor
timestamp: June 23, 2015, 01:28:00 pm
content: Hi All,I have been having problems with output pins on a 18f23k20 pic micrcontroller latching. I rewrote the program so as only to turn on and off the relay with a button input,but it still latches. The relay will turn on but will not turn off . i tried using different ports to no avail.when replacing relay circuit with a 1k resistor and led it works.i tried changing timing delays in circuit and still no good.I finally got it to work by adding a diode before the base resistor, but i dont know why this worked, can anyone shed some light on this ,i have never had this problem with other pic micros . this is the first 3.3v micro i have used before.

author: Zman
author_rank: Contributor
timestamp: June 23, 2015, 01:40:56 pm
content: probably you have some spikes on the ground or on reset pin of micro when relay is switching...do you have a pull-up and small cap on reset?proper PCB layout also makes a difference...

author: ctesla75
author_rank: Contributor
timestamp: June 23, 2015, 02:20:55 pm
content: hi zman,i have disabled mclr in config so have not used any cap or resistor maybe i will enable it and add cap and resistor,circuit is built on breadboard, but hasnt been problem in the past

author: macboy
author_rank: Super Contributor
timestamp: June 23, 2015, 03:00:36 pm
content: Does your relay have a reverse connected diode to prevent the reverse voltage transient at turn-off? Try also adding a parallel resistor, equal to or lower than the coil resistance. Put a 10 uF cap between the relay + and the transistor GND to decouple the relay current.Does the behavior change if you write the entire LAT register instead of writing just one bit? Writing just one bit differs in that it uses a read-modify-write.

author: ctesla75
author_rank: Contributor
timestamp: June 24, 2015, 11:30:55 am
content: hi macboy,my relay does have a reverse connected diode . i have tried adding parralel resistor and cap neither worked , I also tried enabling mclr and adding a resistor ,first a 1k then a 10k to mclr and + and a .1uf from mclr to ground it still latches. it latches when i turn on one bit or if i turn on the whole port and triggering from one bit with leds to the other bits the leds flash so only the bit with the relay latches. I also changed the program to turn on a different bit after each button press from 0 to 7 and it doesnt latch in that case,so i am just stumped as to why it is latching , is there a pin i can probe on the scope to look for noise or spikes , im still a newbie but willing to learn

author: lapm
author_rank: Frequent Contributor
timestamp: June 24, 2015, 03:13:04 pm
content: I would get hit scope measurements.. Sounds to me like you have issues with supply rail voltage and that can cause micro to reset even if you disable external mclr.. Chip still has internal reset system.Maybe you can try without relay, does it still latch? If not then its power supply issue and current relay draws causes temporary voltage drop, leading to reset of micro and io pins..Is that 12 volts same rail that gets regulated to 5/3,3 volts for micro?

author: Ian.M
author_rank: Super Contributor
timestamp: June 24, 2015, 03:39:55 pm
content: Can you confirm that you are writing to the LAT bit for the pin, *NOT* the port bit?If not, I suspect you have run afoul of theRMW effect.

author: macboy
author_rank: Super Contributor
timestamp: June 24, 2015, 06:51:24 pm
content: Did you add the decoupling cap at the relay as I suggested? And do you have the PIC decoupled near its supply pins? Decoupling is very important when switching any load that takes any appreciable current, especially inductive ones like a relay coil.

author: slateraptor
author_rank: Frequent Contributor
timestamp: June 25, 2015, 06:20:28 am
content: Quote from: ctesla75 on June 23, 2015, 01:28:00 pmSmells likeCMOS latchupcaused by transient flyback.Some questions:Relay nominal coil resistance?Flyback diode P/N?Validate that flyback diode still forward conducts?Hypothesis: Flyback diode is dead, probably from overcurrent caused by coil discharge. When GPIO goes low, fast transient flyback voltage spike causes GPIO to latchup, which re-energizes relay, all of which happens faster than the relay's switching speed. This would explain why your circuit works with a diode before the base resistor; it absorbs the flyback transient and prevents the GPIO from latchup...it would also explain why everything else apparently works while the relay GPIO alone remains latched without said base diode...it also makes a lot of unvalidated assumptions.

author: picandmix
author_rank: Frequent Contributor
timestamp: June 25, 2015, 09:02:25 am
content: Have got a 45k22 firing on 20+ output transistors and relays without any problem using just a base resistor and flyback diodes. so the K chips do work ok.I does sound like a code problem, have you seen SusyC's similar posts in the Micro forum.One other common mistake, have you turned off the default analogue mode with ANSEL.You don't say what complier you are using, would be helpful to show your code.

author: ctesla75
author_rank: Contributor
timestamp: June 25, 2015, 09:39:23 am
content: hi all,thanks for the replies here is what i have tried so far , i am programming in flowcode a graphical programming systemI have checked the reverse diode it works ok but I replaced it anyway 1n4004.Added 10uf cap between + of relay and gnd no differenceI am not sure if by lat register you mean the entire port but I tried both the entire port and writing only one bit with the same resultsthe 12volt rail is the same that is fed to the 3.3v regulatorcoil resistance is 240 ohmsremoving the relay circuit and powering a led directly through a 1k resistor does not latchhowever trying to turn on led through transistor same circuit as relay but flyback diode removed and 1k resistor and led instead of coil latches led ,even after replacing transistori haven't read susyc's posts i will look it up i am also looking at ansel information now i think flowcode sets to digital by default but i will try to do it manually

author: Kalvin
author_rank: Super Contributor
timestamp: June 25, 2015, 09:58:57 am
content: Quote from: Ian.M on June 24, 2015, 03:39:55 pmAs Ian.M suggested, you might be reading and writing the wrong output register. If Ian.M is right (and I guess he is), you are trying to set or clear the bit in a PORT-register (PORTA, PORTB or PORTC) instead of a bit in a LAT-register (LATA, LATB or LATC).See the data sheethttp://ww1.microchip.com/downloads/en/DeviceDoc/41303G.pdfpage 121 figure 10-1 how the digital port registers are implemented.

author: kolbep
author_rank: Frequent Contributor
timestamp: June 25, 2015, 10:06:56 am
content: Tie your pgm pin to gnd using a 10k resistor.i had it that when pgm was floating, even if lvp was turned off, that when a relay was switched in the near vicinity, that the pic would try to go to programming mode and lock upAlso put a diode and 10k resistor in series from your supply to the mclr pin. The stripe of the diode must point towards the mclr pin. That pulls the mclr high, but blocks programming voltage from coming back to the supplyAlso, if port pins are not used, set them as outputs, stops them from picking up interference.

author: Ian.M
author_rank: Super Contributor
timestamp: June 25, 2015, 11:29:39 am
content: Pics with a PGM pin only need it to be tied low if LVP is not disabled in the CONFIG. On older PICs however, it needed to be held low for reliable HV programming.It certainly wouldn't cause the observed symptoms as a floating enabled PGM pin has a very similar effect to a floating /MCLR reset pin./MCLR doesn't need a series diode unless you are using a really crappy 3rd party ICSP programmer. All Microchip ICSP progremmers will prevent the Vdd supply rail rising during programming. It does however need a 10K pullup, even if its been disabled in the CONFIG.I agree about setting unused pins as outputs - set them as outputs, set them low.

author: ctesla75
author_rank: Contributor
timestamp: June 25, 2015, 01:06:57 pm
content: Have tried to tie pgm to gnd but no difference , i dont know how to choose lat or port in this software i only see option for port either entire port by typint $portb or a bit by typing $portb.1 i found no mention of lat in the help files. my question is if it doesnt latch when only a led is connected only when turning on led or relay through transistor is it still software issue.

author: Kalvin
author_rank: Super Contributor
timestamp: June 25, 2015, 01:29:11 pm
content: Quote from: ctesla75 on June 25, 2015, 01:06:57 pmThe datasheethttp://ww1.microchip.com/downloads/en/DeviceDoc/41303G.pdfpage 78 Table 5-1 lists the register addresses. I have noe experience what so ever with Flowcode, so I cannot help with how to properly access the LAT register, but possibly you can create a C function which would manipulate the LAT register as needed.

author: Ian.M
author_rank: Super Contributor
timestamp: June 25, 2015, 01:43:37 pm
content: You have the LVP bit in CONFIG word 0x300006 (Single Supply ICSP) disabled so as I just said, the PGM pin doesn't need grounding.It looks like you are using a graphical programming environment, maybe flowcode.Whenever you output to a port, *DONT* use the PORTA .... PORTE registers, but instead use LATA .... LATE. However it is absolutely critical that when you want to read an input pin you use the PORTA .... PORTE registers as the LAT ones wont work for input.If you are actually outputting to '$portb.1' try replacing it with '$latb.1' and so on for all other output pins. It looks like your flowchart generates C code for you and C is case sensitive. I know SourceBoost Boost C (commonly bundled with flowcode) uses lower case SFR names, but most other PIC compilers use uppercase SFR names so you need to be careful to type the LAT output pin name in the same case you would type the PORT pin name."Rule of thumb: On PICs with LATx registers, always read inputs from PORTx and write outputs to LATx. If you need to read what you set an output to, read LATx."

author: hlavac
author_rank: Frequent Contributor
timestamp: June 27, 2015, 07:49:13 pm
content: What kind of transistor are you using to switch the load and how is it connected? If you had PNP or P-MOSFET switching the positive rail you could be running into the ESD protection diode to Vcc on the output pin of the micro...

author: ctesla75
author_rank: Contributor
timestamp: June 28, 2015, 08:25:00 am
content: hi hlvac ,i am using bc547 and have tried replacing it with bc337.it is connected as shown in attachment .I have been trying to figure out how to write to lat registers as ian.m suggested and am currently trying to write test program in demo version of mikroc for pic(as soon as i get my head around c). to see if that helps .