View previous topic :: View next topic |
Author |
Message |
flip
Joined: 15 Feb 2005 Posts: 11
|
Using a PORT for input and output in the same program? |
Posted: Tue Feb 22, 2005 3:22 pm |
|
|
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
|
|
Posted: Tue Feb 22, 2005 5:16 pm |
|
|
Quote: | Can I use PORT B as input and output in the same program? |
Yes if you design the hardware for it. |
|
|
Guest
|
|
Posted: Tue Feb 22, 2005 6:02 pm |
|
|
What would you recommend? All I need to have as input are switches and all I need for output are LEDs. Thanks |
|
|
cyril Guest
|
|
Posted: Tue Feb 22, 2005 6:19 pm |
|
|
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
|
|
Posted: Wed Feb 23, 2005 7:27 am |
|
|
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
|
|
Posted: Wed Feb 23, 2005 2:40 pm |
|
|
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
|
|
Posted: Wed Feb 23, 2005 5:24 pm |
|
|
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
|
|
Posted: Wed Feb 23, 2005 8:27 pm |
|
|
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
|
|
Posted: Wed Feb 23, 2005 8:39 pm |
|
|
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
|
|
Posted: Wed Feb 23, 2005 9:12 pm |
|
|
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
|
|
Posted: Thu Feb 24, 2005 10:19 am |
|
|
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
|
|
Posted: Thu Feb 24, 2005 12:26 pm |
|
|
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. |
|
|
[email protected] Guest
|
USB |
Posted: Tue Jul 12, 2005 3:57 pm |
|
|
DEAR SIR I NEED A USB DEVELOPMENT KIT SCHEMATIC DRAWING WITH APPROPRIATE DATASHEETS THANK YOU. |
|
|
|