Datasheet

1996 Microchip Technology Inc. Preliminary DS40122B-page 35
PIC14000
If the CMBOE bit (CMCON<5>) is set, the RD3/REFB
pin becomes the programmable reference B output
and pin RD2/CMPB becomes the comparator B output.
PORTD<1:0> also serve multiple functions. These pins
act as the I
2
C data and clock lines when the I
2
C module
is enabled.
The TRISD register controls the direction of the Port D
pins. A ‘1’ in each location configures the
corresponding port pin as an input. Upon reset, this
register sets to FFh, meaning all PORTD pins are ini-
tially inputs. The data register should be initialized prior
to configuring the port as outputs.
Unused inputs should not be left floating to avoid
leakage currents. All pins have input protection diodes
to V
DD and VSS.
EXAMPLE 5-3: INITIALIZING PORTD
Note: Setting CMBOE changes the definition of
RD3/REFB and RD2/CMPB, bypassing
the PORTD data and TRISD register set-
tings.
CLRF PORTD ; Initialize PORTD data
; latches before setting
; the data direction
; register
BSF STATUS, RP0 ; Select Bank1
MOVLW 0xFF ; Value used to initialize
; data direction
MOVWF TRISD ; Set RD<7:0> as inputs
5.4 I/O Programming Considerations
5.4.1 BI-DIRECTIONAL I/O PORTS
Reading the port register reads the values of the port
pins. Writing to the port register writes the value to the
port latch. Some instructions operate internally as
read-modify-write. 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 PORTC will
cause all eight bits of PORTC to be read into the CPU.
Then the BSF operation takes place on bit5 and
PORTC is written to the output latches. If another bit of
PORTC is used as a bi-directional I/O pin (say 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 re-written 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 into output mode later on, the content
of the data latch may now be unknown.
A pin actively outputting a LOW or HIGH should not be
driven from external devices at the same time in order
to change the level on this pin (“wire-or”, “wire-and”).
The resulting high output currents may damage the
chip.
Example 5-4 shows the effect of two sequential read
modify write instructions (ex. BCF, BSF, etc.) on an I/O
Port.
EXAMPLE 5-4: READ MODIFY WRITE
INSTRUCTIONS ON AN
I/O PORT
;
;
Initial PORT settings: PORTC<7:4> Inputs
; PORTC<3:0> Outputs
;
;
PORTC<7:6> have external pull-up and are not
connected to other circuitry
;
; PORT latch PORT pins
; ---------- ----------
BCF PORTC, 7 ; 01pp pppp 11pp pppp
BCF PORTC, 6 ; 10pp pppp 11pp pppp
BSF STATUS,RP0 ;
BCF TRISC, 7 ; 10pp pppp 11pp pppp
BCF TRISC, 6 ; 10pp pppp 10pp pppp
;
; Note that the user may have expected the pin
; values to be 00pp pppp. The 2nd BCF caused
; RC7 to be latched as the pin value (High).