Port Initialization
and Direction Control
Semiconductor Group
4 of 7
AP164601 1998-02
Note 2: When a port is in the initial input configuration after reset, and no external source
is driving the pins to defined logic levels, it is not guaranteed that the reset value ’0’ for a
specific bit will still be in the port data register after a read-modify-write operation has
been performed on any bit of this port. This applies to explicit AND/OR masking as well
as to Bit and Bit Field instructions, which are implicit read-modify-write instructions and
always perform a complete word access on a port.
The reason for this effect is that any read access on port pins configured as inputs returns
the logic levels of the individual pins. Therefore, when the value in the port data register is
different from the logic level at the pin, the value in the port data register may
unintentionally be overwritten by a read-modify-write operation. This may also result in
sporadic problems when the level at the input pin is floating to random logic levels.
For ports which are associated with the CAPCOM/CAPCOM2 or PWM unit (P2, P7, P8), a
specific bit protection mechanism has been implemented which prevents single bits
which are modified by the on-chip periperal units from unintentionally being overwritten by
a bit read-modify-write sequence via Bit and Bit Field instructions (see section ’Protected
Bits’ and ’Bit-Handling and Bit-Protection’ in respective User’s Manual, e.g. C167
Derivatives User’s Manual, V2.0, p. 2-17 and p. 4-9).
It is therefore advised to always set the port data register and the port direction register (at
least for all ports without bit protection) as one unit in two consecutive instructions.
In case interrupts could occur after the instruction which writes to the port data register,
and in the interrupt service routine read-modify-write instructions are executed which also
access this port, it is additionally recommended to temporarily disable interrupts, e.g.
; (syntax for 8xC166 microcontrollers)
BFLDH PSW, #0F0h, #0F0h ; set PSW.ILVL to highest priority level
NOP
; Note: NMI# may still interrupt this sequence
BSET P1.7
; set initial ouput value
BSET DP1.7
; set direction to output
BFLDH PSW, #0F0h, #00h
; restore previous priority
; (syntax for other C166 microcontrollers)
ATOMIC #2
; disable interrupts and class A traps for 2 instruction cycles
BSET P1L.7
; set initial ouput value
EXTR#1
BSET DP1L.7
; set direction to output
NOP
; or other instruction which does not access P1
This procedure is also recommended when a port pin is used in a bidirectional manner,
i.e. when its direction is dynamically changed from input to output.