Author Topic: I/O Pins latching  (Read 5298 times)

0 Members and 1 Guest are viewing this topic.

Offline ctesla75Topic starter

  • Contributor
  • Posts: 22
I/O Pins latching
« on: June 23, 2015, 01:28:00 pm »
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.
 

Offline Zman

  • Contributor
  • Posts: 25
  • Country: au
Re: I/O Pins latching
« Reply #1 on: June 23, 2015, 01:40:56 pm »
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...
Information wants to be FREE!
 

Offline ctesla75Topic starter

  • Contributor
  • Posts: 22
Re: I/O Pins latching
« Reply #2 on: June 23, 2015, 02:20:55 pm »
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
 

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2254
  • Country: ca
Re: I/O Pins latching
« Reply #3 on: June 23, 2015, 03:00:36 pm »
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.
 

Offline ctesla75Topic starter

  • Contributor
  • Posts: 22
Re: I/O Pins latching
« Reply #4 on: June 24, 2015, 11:30:55 am »
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
 

Offline lapm

  • Frequent Contributor
  • **
  • Posts: 564
  • Country: fi
Re: I/O Pins latching
« Reply #5 on: June 24, 2015, 03:13:04 pm »
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?
« Last Edit: June 24, 2015, 03:15:29 pm by lapm »
Electronics, Linux, Programming, Science... im interested all of it...
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: I/O Pins latching
« Reply #6 on: June 24, 2015, 03:39:55 pm »
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 the RMW effect.
 

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2254
  • Country: ca
Re: I/O Pins latching
« Reply #7 on: June 24, 2015, 06:51:24 pm »
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.
 

Offline slateraptor

  • Frequent Contributor
  • **
  • Posts: 833
  • Country: us
Re: I/O Pins latching
« Reply #8 on: June 25, 2015, 06:20:28 am »
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.

Smells like CMOS latchup caused 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.
 

Offline picandmix

  • Frequent Contributor
  • **
  • Posts: 395
  • Country: gb
Re: I/O Pins latching
« Reply #9 on: June 25, 2015, 09:02:25 am »
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.
 

Offline ctesla75Topic starter

  • Contributor
  • Posts: 22
Re: I/O Pins latching
« Reply #10 on: June 25, 2015, 09:39:23 am »
hi all,
         thanks for the replies here is what i have tried so far , i am programming in flowcode a graphical programming system
I have checked the reverse diode it works ok but I replaced it anyway 1n4004.

Added 10uf cap between + of relay and gnd no difference

I 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 results

the 12volt rail is the same that is fed to the 3.3v regulator

coil resistance is 240 ohms

removing the relay circuit and powering a led directly through a 1k resistor does not latch
however 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 transistor

i 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


 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: I/O Pins latching
« Reply #11 on: June 25, 2015, 09:58:57 am »
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 the RMW effect.

As 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 sheet http://ww1.microchip.com/downloads/en/DeviceDoc/41303G.pdf page 121 figure 10-1 how the digital port registers are implemented.
 

Offline kolbep

  • Frequent Contributor
  • **
  • Posts: 598
  • Country: za
    • ShoutingElectronics.com
Re: I/O Pins latching
« Reply #12 on: June 25, 2015, 10:06:56 am »
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 up

Also 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 supply

Also, if port pins are not used, set them as outputs, stops them from picking up interference. 
====================================
www.ShoutingElectronics.com Don't just talk about Electronics, SHOUT ABOUT IT! Electronics Blog Site and Youtube Channel
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: I/O Pins latching
« Reply #13 on: June 25, 2015, 11:29:39 am »
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.
 

Offline ctesla75Topic starter

  • Contributor
  • Posts: 22
Re: I/O Pins latching
« Reply #14 on: June 25, 2015, 01:06:57 pm »
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.
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: I/O Pins latching
« Reply #15 on: June 25, 2015, 01:29:11 pm »
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.

The datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/41303G.pdf page 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.
« Last Edit: June 25, 2015, 01:31:01 pm by Kalvin »
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12852
Re: I/O Pins latching
« Reply #16 on: June 25, 2015, 01:43:37 pm »
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."
 

Offline hlavac

  • Frequent Contributor
  • **
  • Posts: 536
  • Country: cz
Re: I/O Pins latching
« Reply #17 on: June 27, 2015, 07:49:13 pm »
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...
Good enough is the enemy of the best.
 

Offline ctesla75Topic starter

  • Contributor
  • Posts: 22
Re: I/O Pins latching
« Reply #18 on: June 28, 2015, 08:25:00 am »
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 .
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf