On-Chip Peripheral Components
C513AO
User’s Manual
6-14
05.99
6.1.6 Read-Modify-Write Feature of Ports 2 and 3
Some port-reading instructions read the latch and others read the pin. The instructions reading the
latch (rather than the pin) read a value, possibly change it, and then rewrite it to the latch. These
are called “read-modify-write” instructions, and are listed in Table 6-2. If the destination is a port or
a port pin, these instructions read the latch rather than the pin. Note that all other instructions which
can be used to read a port, exclusively read the port pin rather than the latch. In all cases, whether
from the latch or the pin, the instruction is performed by reading the SFR P0, P2 and P3 as in the
op-code; for example, “MOV A, P3” reads the value from Port 3 pins, while “ANL P3, #0AAH” reads
from the latch, modifies the value, and writes it back to the latch.
Although it is not obvious, the last three instructions in Table 6-2 are “read-modify-write”
instructions. They are such because they read all 8 bits of the port byte, modify the addressed bit,
then write the complete byte back to the latch.
“Read-modify-write” instructions are directed to the latch rather than the pin to avoid a possible
misinterpretation of the voltage level at the pin. For example, a port bit might be used to drive the
base of a transistor. When a “1” is written to the bit, the transistor is turned on. If the CPU then reads
the same port bit at the pin rather than the latch, it will read the base voltage of the transistor
(approx. 0.7 V; that is, a logic low level) and interpret it as “0”. For example, when modifying a port
bit by a SETB or CLR instruction, another bit in this port with the above mentioned configuration
might be changed if the value read from the pin were written back to the latch. However, reading
the latch rate than the pin will return the correct value of “1”.
Table 6-2
“Read-Modify-Write” Instructions
Instruction
Function
ANL
Logic AND; for example, ANL P1, A
ORL
Logic OR; for example, ORL P2, A
XRL
Logic exclusive OR; for example, XRL P3, A
JBC
Jump if bit is set and clear bit; for example, JBC P1.1, LABEL
CPL
Complement bit; for example, CPL P3.0
INC
Increment byte; for example, INC P1
DEC
Decrement byte; for example, DEC P1
DJNZ
Decrement and jump if not zero; for example, DJNZ P3, EL
MOV Px.y,C
Move carry bit to bit y of Port x
CLR Px.y
Clear bit y of Port x
SETB Px.y
Set bit y of Port x