Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem using PIC16F886 - Outputs dropping for no reason.

Status
Not open for further replies.

foglie0p

New Member
Hi guys,

This problem has got me stuck. Dont know where to look anymore.

To explain the project, it is interfacing 2 temp sensors with their minimum and maximum settings driving 2 relays. The lot displayed on a 16x2 LCD.

I have 5 buttons:

2 buttons for the first relay temperature (+ or -)
2 buttons for the 2nd relay temperature (+ or -)
1 button to switch the display between 1st and second sensors and relays infos.

To explain, both relays are ON, as temp is lower than the minimum set, PORTC3 and 4 are high (relay1 and 2 outputs pin)

As soon as I press the switch to change the display PORTC1, both relay outputs drops for a brief moment.

This is where I dont understand, because there is nothing in the program telling these 2 outputs to do so at this time.

Hopefully my code makes a bit of sense to you guys. Not the best to arrange things like a pro.

Thanks in advance for any infos.

Cheers!
 
I'm doing a bit of reorganizing to your code so it's easier to read. By chance, can you also post up your circuit schematic so I can tell a bit more what's going on in the human sense as well?
 
If you are loading the relay pins too heavily then the bsf enable in the LCD routines will clear the relay outputs. Try using a shadow register for port C.

As already mentioned, a schematic would help.

Mike.
 
I am pretty sure this is why the 18 series are now latched.. I had a similar problem with a pic16f877a.. I had several alarm outputs, if I set them individually they didn't work as expected... I ended up doing exactly what Pommie suggested.... Create a register (output buffer) and write only once to the port.. Incidentally... I was using port C as well..

It must have something to do with the amount of peripherals on this port....
 
Yes...on the 16F you cannot read the output latch directly. Reading an output pin just returns the high/low state of the pin itself. The latch on 16F PICs can only be written, not read. Hence the need for a shadow register.
 
Last edited:
I worked without drawing, just made one up quickly. Hope it is complete enough to help a bit.

Will give a try to the shadow register and see what happens.

Thanks for your answers, sounds good for the reorganisation, will probably teach me better how to structure my code.
 
Another thing I will note is that the ST7066U controller chip on the display module DOES NOT use ASCII format. You must can the character map as a look up table. In my revised code, I will have 3 look up tables - one for letters, one for digits and one for punctuation marks. Still working on the code as we speak.

Now...question about your hardware...why are you direct driving relays from the PIC? This is bad practice IMHO and should be avoided at all costs. Best to use a transistor to drive the relays (and you MUST have a flyback diode across the relay coils in conjunction with this). This way the PIC only has to source the current to fire the transistor on, which is quite small and won't stress the PIC nearly as much. Then you won't need the shadow register and will be able to read the PIC pins as the pins will not be loaded hardly at all by the transistor.
 
Last edited:
Hey mate,

Didnt know about ASCII for the display, it displays characters and numbers fine tho.. Strange.

Yes I know for the fly back diode, I didnt put it on the drawing, my bad. These relays draw 7mA, but I might throw in two 2n2222 transistor you are right.

So to be sure for next time, what is it for the PIC, 20mA for each pin or for each port?

Thanks for taking the time to look at the code, really appreciate it.

Cheers!
 
My mistake...apparently the ST7066U does in fact use standard ASCII codes. Just researched it.

Now...in Microchip assembly, the proper way to type out ASCII values is -

a'<character>'

Example, sending the character B would be -

a'B'

While true that the PIC CAN source/sink 25mA per port pin, what's not being taken into account is the fact that relays are an inductive load, and it's never a good idea to drive inductive loads from a microcontroller pin. LEDs are about the extent of the loads I will direct drive from a PIC pin. Microcontrollers were primarily designed to generate signals only...not direct drive the load itself. It would be like your brain trying to directly pump your blood rather than it sending a signal to the heart that tells the heart when to do it.

However, if you insist on direct driving the relays though, I would use the PIC pin as a current SINK rather than a SOURCE (i.e. tie one side of the relay to +5V and the other side to the PIC pin). The logic to turn the relay on/off would be reversed, but you would at least be able to directly read the relay pins without any concerns for loading...which means no shadow register needed.
 
Last edited:
Ah ok, didn't know about the exact way to write the characters, noted with thanks.

I havent built the proper circuit yet, I am using a breadboard and I simulate the relays by using LED, if I remove one of them it looks like the other one is not dropping anymore so your saying about the current proves right I think.

As well for the LCD backlight I will be using NPN transistor to lower the load on the controller and change the buttons to drive the inputs through the ground and not the 5V.
 
I hope you're using a 330R resistor (or something close to that value) in series with the relay sim LEDs.

No need to change the buttons. When set up as inputs, the pins draw current, but not very much at all. It doesn't matter which way you do the switches so your switch schematic is correct.

It's only when pins are used as outputs that certain things of this nature need to be considered.
 
Last edited:
Yes I use resistors. Ok will let the buttons as they are, I have seen schematics with buttons reversed it is why I thought it might have been different.
 
Yes the more common way to connect buttons to a microcontroller input is with the pull up resistor/switch to Vss scheme (I think this carried over from non-CMOS TTL devices since they required a current limiting resistor to Vcc at the input to supply turn on current), but pull down resistor/switch to Vdd also works. The high input impedance of the PIC input keeps the input from drawing much current if any at all. The only difference is that the logic reverses.
 
Last edited:
Oh ok, I don't know that much on this old generation electronic. I started learning programming on my own for a hobby and it has been a trail and error learning process but I think I have done not too bad.

After tweaking the code and rearranging things on the breadboard it looks like everything is working so far. Will probably learn some more in seeing how you structure the code as I think my way only makes sense to me ;-) haha
 
Status
Not open for further replies.

Latest threads

Back
Top