piclist 2001\01\02\091005a >
Thread: Picky I/O
picon face BY : Drew Vassallo email (remove spam text)

Sorry this is so late, I've been enjoying the holidays and staying away from
e-mail :)

>I had assumed that I could flip a pin from input to output and back, by bcf
>and bsf instructions on the TRIS registers.
>Apparently this isn't true.

Well, yes it is.  The only thing you really have to watch out for is the
state of the pin when you switch from input to output.  If the pin is held
high as an input, then switched to an output, the port latch will be loaded
with the input value (high) and remain until it is reloaded with a new

I've done this successfully for I^2C code using pull-ups to drive the I/O
lines.  The problem is that when you do ANY operation on the same port,
REGARDLESS of which pin you access, the port latches are set.  For instance,
if you are switching PORTB, 0 from input to output and it was held high
during the input phase, it will have a '1' loaded into the portb, 0 latch
even after you configure it as an output.  Let's say after you set this pin
as an output and clear it, loading a '0' into the portb, 0 latch.  Now,
let's say you set this pin as an output now, then the pin gets pulled high
by the attached peripheral (even though you aren't READING it at this
point), and during the time that this pin is held high, you do a read or
write on ANY PORTB PIN, then the high value on the 'switchable pin' will be
loaded into the PORTB, 0 latch and stay there until you re-output your
desired value.  This is not obvious and you really have to search your code
to find where you may be accessing other port pins when the input state is
unknown.  If this isn't clear, let me know and I'll try to re-explain it.

I forgot which PIC you're using, but this is from the 16C71 datasheet (same
as all the 16C series I assume, plus 16F84, etc..

Any instruction which writes, operates internally as a
read followed by a write operation. The BCF and BSF
instructions, for example, read the register into the
CPU, execute the bit operation and write the result
back to the register. Caution must be used when these
instructions are applied to a port with both inputs and
outputs defined. For example, a BSF operation on bit5
of PORTB will cause all eight bits of PORTB to be read
into the CPU. Then the BSF operation takes place on
bit5 and PORTB is written to the output latches. If
another bit of PORTB is used as a bi-directional I/O pin
(e.g., bit0) and it is defined as an input at this time, the
input signal present on the pin itself would be read into
the CPU and rewritten to the data latch of this particular
pin, overwriting the previous content. As long as the pin
stays in the input mode, no problem occurs. However,
if bit0 is switched to an output, the content of the data
latch may now be unknown.
Reading the port register, reads the values of the port
pins. Writing to the port register writes the value to the
port latch. When using read-modify-write instructions
(ex. BCF, BSF, etc.) on a port, the value of the port pins
is read, the desired operation is done to this value, and
this value is then written to the port latch.
Example 5-3 shows the effect of two sequential read-modify-
write instructions on an I/O port.
;Initial PORT settings: PORTB<7:4> Inputs
; PORTB<3:0> Outputs
;PORTB<7:6> have external pull-ups and are
;not connected to other circuitry
; PORT latch PORT pins
; ---------- ---------
BCF PORTB, 7 ; 01pp pppp 11pp pppp
BCF PORTB, 6 ; 10pp pppp 11pp pppp
BCF TRISB, 7 ; 10pp pppp 11pp pppp
BCF TRISB, 6 ; 10pp pppp 10pp pppp
;Note that the user may have expected the
;pin values to be 00pp ppp. The 2nd BCF
;caused RB7 to be latched as the pin value

Hope this helps.  I think you'll find it does.

Get your FREE download of MSN Explorer at http://explorer.msn.com

http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


See also: www.piclist.com/techref/microchip/ios.htm?key=i%2Fo
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) Picky I/O

month overview.

new search...