View previous topic :: View next topic |
Author |
Message |
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
Problem with PIC16F946 |
Posted: Mon Apr 14, 2014 7:35 am |
|
|
Greetings! I'm using MPLab v8.91, CCS v4.134 and PIC16F946. I connected 3 leds to G0,G1 and G2.
I'm controlling them with this program:
Code: |
#include <16F946.h>
#fuses HS,NOWDT
#device adc=10
#use delay(clock=10M)
#define ON_SYGNAL1 PIN_G0
#define ON_SYGNAL2 PIN_G1
#define SP_CHOOSE PIN_G2
void SetupPorts()
{
output_drive(ON_SYGNAL1);
output_drive(ON_SYGNAL2);
output_drive(SP_CHOOSE);
output_high(ON_SYGNAL1);
output_high(ON_SYGNAL2);
output_high(SP_CHOOSE);
}
void main()
{
delay_ms(1000);
SetupPorts();
while(1)
{
output_low(ON_SYGNAL1);
delay_ms(1000);
output_low(ON_SYGNAL2);
delay_ms(1000);
output_high(ON_SYGNAL1);
output_high(ON_SYGNAL2);
output_high(SP_CHOOSE);
delay_ms(1000);
}
}
|
But in this case the signals don't go high state! If I use them 1 by 1 - everything is OK.
Code like this
Code: |
output_high(ON_SYGNAL1);
output_high(ON_SYGNAL2);
|
doesn't work! The pins stay in low state.
What's happening here?!
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19589
|
|
Posted: Mon Apr 14, 2014 8:14 am |
|
|
Start at the beginning. You don't need 'output_drive' anywhere.
Output_high (or low), automatically switches the pin to 'drive'. You only need drive (or float), if you want to switch the drive on/off while leaving the actual output register unchanged. Half of your setup_ports code is wasted.
A question. Do you have suitable current limiting resistors?.
If not, you are probably overloading the pins.
It sounds (though your description is appalling), that you might be hitting the PIC 'read modify write' problem (do a search here). If the load on a PIC pin is too high for it to be 'seen' as 'high' when read, then when you output on another pin on the same port, it'll go off. |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Mon Apr 14, 2014 2:30 pm |
|
|
I the load is not big - 4mA, but I have 100nF filter caps between every pin and GRD. Is it possible the charging current of these caps to cause this?! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19589
|
|
Posted: Tue Apr 15, 2014 12:23 am |
|
|
If these are on the PIC outputs, then yes. You most definately should _not_ have capacitors connected to the PIC outputs. You want good filtering on the supply, but connecting capacitors on outputs would overload the pins every time they change state. It would only be the fact that PIC pins are quite rugged, that would stop them being destroyed by this. You normally want to be minimising output capacitance, not deliberately increasing it... |
|
|
stoyanoff
Joined: 20 Jul 2011 Posts: 375
|
|
Posted: Tue Apr 15, 2014 7:19 am |
|
|
I added 10ms delays between every pin switching and it`s OK for now. I added the caps to prevent unexpected state switching.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19589
|
|
Posted: Tue Apr 15, 2014 7:49 am |
|
|
They won't. They actually cause it.....
Get rid of them before you destroy the chip.
What is happening, is whenever you write to a pin, the chip reads all the pins on the port, changes the one pin, and writes this back.
If you try to change two pins 'one after the other', because the first pin is momentarily massively overloaded, it has not got to the voltage it is meant to reach, so reads back in it's old state.
A few hundred pF, 'maybe' OK, but you are physically overloading the pins, which may well cause them to fail long term. It is bad design, and bad practice.
If the pin does change state, having the capacitors there won't stop them changing eventually (as you see with the delays), so it won't help 'unexpected state switching' in fact it is causing it at the moment.... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 15, 2014 10:11 am |
|
|
Quote: | I have 100nF filter caps between every pin and GRD. |
When I was first starting out, years ago, an older Engineer told me
"one way to kill a gate is to hang a cap on it". He was talking about
LS04 or some other gate (this was back in the days of gates), but it
applies to PIC outputs too.
If you put a 100nF cap on every Vdd pin to ground, we have no problem
with it. But don't put them on i/o pins. |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Apr 15, 2014 10:18 am |
|
|
The concept here is "reactive load" - as a series inductor with a suitably low R value to ground - or better yet a seemingly reverse biased fast diode -to ground-is also quite capable of delivering just as nasty
and destructive a current spike to a protection network in your PIC or any other bit of silicon ..... and hence the extra diodes seen in nearly any MOSFET meant for power switching into a reactive load. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19589
|
|
Posted: Tue Apr 15, 2014 11:06 am |
|
|
There is also another issue, that if all the capacitors are charged to 5v (outputs are high), and then the power is turned off, the odds are the capacitors will try to power the PIC 'back' through the protection diodes. Risks overloading these as well.... |
|
|
|