CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem with PIC16F946

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stoyanoff



Joined: 20 Jul 2011
Posts: 375

View user's profile Send private message

Problem with PIC16F946
PostPosted: Mon Apr 14, 2014 7:35 am     Reply with quote

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: 19436

View user's profile Send private message

PostPosted: Mon Apr 14, 2014 8:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Apr 14, 2014 2:30 pm     Reply with quote

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: 19436

View user's profile Send private message

PostPosted: Tue Apr 15, 2014 12:23 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 15, 2014 7:19 am     Reply with quote

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: 19436

View user's profile Send private message

PostPosted: Tue Apr 15, 2014 7:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Apr 15, 2014 10:11 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Tue Apr 15, 2014 10:18 am     Reply with quote

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: 19436

View user's profile Send private message

PostPosted: Tue Apr 15, 2014 11:06 am     Reply with quote

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....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group