PDA

View Full Version : Input and Output on same Port



Frozen001
- 13th November 2008, 17:59
Ok this may seem like a silly question, but I want to use one port to input data and output data, PortE actually on an 18f8722. Now I know that TRISE $FF will make PortE and Input and TRISE=$00 Will make it an output, but what do I use for both??

Melanie
- 13th November 2008, 18:35
Remember that a 1 in a TRIS setting will make the pin an INPUT (remember 1=I=Input), and a zero will make it an Output (remember 0=O=Output).

So, TRISE=$FF is the same as TRISE=%11111111 (all input)

and TRISE=$00 is the same as TRISE=%00000000 (all output)

so to make bits 0 thru 3 INPUT and bits 4 thru 7 OUTPUT you can write...

TRISE=$0F or TRISE=%00001111

and to make all even bits INPUT and all odd bits OUTPUT...

TRISE=$55 or TRISE=%01010101

and to make pin E.2 and E.7 INPUT, and all the rest OUTPUT...

TRISE=$84 or TRISE=%10000100

etc etc.

Frozen001
- 13th November 2008, 18:41
Thanks for the reply. I understand the 1 and 0 of each port, but in a portion I want it all an input and later on I want it all and output.

Do I have to have a TRISE $00 before I use it as an output, and then have another TRISE $FF to use it as an input??

Melanie
- 13th November 2008, 18:46
Precisely.

If you wish to use a pin (or indeed an entire Port) for Input and Output, all you do is simply change the TRIS for that pin or that whole Port before you perform any I/O.

Once you set TRIS, it stays that way until you change it. Note that some PBP commands change TRIS on pins as required by that command, but for clarity, you should always set TRIS for whatever you happen to need it for.

There is no restriction as to how many times you can change the TRIS status within your program.