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 CCS Technical Support

Using a PORT for input and output in the same program?

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



Joined: 15 Feb 2005
Posts: 11

View user's profile Send private message

Using a PORT for input and output in the same program?
PostPosted: Tue Feb 22, 2005 3:22 pm     Reply with quote

Can I use PORT B as input and output in the same program? I'm using the 18f458. My program works like this:

In one subroutine, I initialize PORT B as output and output B to LEDs. A control bit makes the program jump to this subroutine and this control bit also is hooked up to 8 AND gates, one for each bit from PORT B. I pull down all of PORT with 1k resistors, then AND PORT B with the control bit.
The output of the ANDs go to 8 LEDs.

In the other subroutine, PORT B is iniatialized as input. The next control bit is hooked up to 8 more AND gates. This control bit is ANDed with 8 input switches (which are also pulled down using 1k resistors). The output of these AND gates are then hooked up to PORT B.

Will this work? I tried straight hooking up the LEDs and the switches to PORT B but the program wouldn't work.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 22, 2005 5:16 pm     Reply with quote

Quote:
Can I use PORT B as input and output in the same program?

Yes if you design the hardware for it.
Guest








PostPosted: Tue Feb 22, 2005 6:02 pm     Reply with quote

What would you recommend? All I need to have as input are switches and all I need for output are LEDs. Thanks
cyril
Guest







PostPosted: Tue Feb 22, 2005 6:19 pm     Reply with quote

I'd think you would want to use a gate with open collector (or drain) outputs. And of course use pull up resistors
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Feb 23, 2005 7:27 am     Reply with quote

One of our designs has switches with LED indicators that are driven in the manner that you desire. That is a single pin is both an input and an output. For the output, the pic supplies the current to the LED. So the LED is connected to the PIC and the other end of the LED is connected to a resistor to ground (680ohms). The PIC is also connected to 2 series resistors to ground. The first a 220 ohm and the second a 47K. The junction between these two resistors is connected to a switch. The other side of the switch goes to 5V.



Last edited by Mark on Wed Feb 23, 2005 9:26 pm; edited 2 times in total
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Feb 23, 2005 2:40 pm     Reply with quote

Mark,
I think there is an error in your schematic drawing; your text and the drawing don't match. Shouldn't the switch be connected between the 47k and 220 Ohm resitors? Nice idea!
Just something I don't understand is why you need the 47k resistor at all?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Feb 23, 2005 5:24 pm     Reply with quote

Yep, text is right and drawing is wrong. In a hurry this morning. The 220 ohm resistor will prevent you from attaching VDD to the pin on the PIC. Could cause damage to the PIC if the switch were pressed and the PIC was set to output a 0. The 47K pulls the input to ground when the switch is not closed.

Last edited by Mark on Wed Feb 23, 2005 9:09 pm; edited 1 time in total
flip



Joined: 15 Feb 2005
Posts: 11

View user's profile Send private message

PostPosted: Wed Feb 23, 2005 8:27 pm     Reply with quote

Let me get this straight. I use the same idea as in your diagram, but put the switch in between the 220ohm and 47k?
flip



Joined: 15 Feb 2005
Posts: 11

View user's profile Send private message

PostPosted: Wed Feb 23, 2005 8:39 pm     Reply with quote

I'm also wondering will the LED light up when the switch is on? Can the output to the LED work when the switch is on?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Feb 23, 2005 9:12 pm     Reply with quote

flip wrote:
I'm also wondering will the LED light up when the switch is on? Can the output to the LED work when the switch is on?


The LED would actually light up when the switch is pressed or "on" but would be a bit dimmer since there is a 220 ohm resistor in the path now. Note that this is a method that we use in our design. It might not be the solution for you. Our LEDs our indicators for the switches (which are momentary). It all depends on your design as what you need to do.
Guest








PostPosted: Thu Feb 24, 2005 10:19 am     Reply with quote

I'm just looking for a design that will allow me to use PORTB to output to LEDs in one subroutine, and have switches as input in another subroutine, using the same PORT B line without affecting eachother. Do you think I should use AND gate logic?
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Thu Feb 24, 2005 12:26 pm     Reply with quote

I'd stick a 74HC245 to read the switches. The LEDs could be driven off the PIC or you could even stick a 74HC373 to drive them. Phillips also makes I2C driver chips. They can be configured to blink LEDs and such.
s11002761@usp.ac.fj
Guest







USB
PostPosted: Tue Jul 12, 2005 3:57 pm     Reply with quote

DEAR SIR I NEED A USB DEVELOPMENT KIT SCHEMATIC DRAWING WITH APPROPRIATE DATASHEETS THANK YOU.
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