I/O ports can be set to input or output mode in bit units. Port register reading and writing involve the following operations:
- Port in input mode
Read: Read the pin level.
Write: Write to the port latch.
- Port in output mode
Read: Read the port latch or output from peripheral functions
(specifications differ by port).
Write: Write to the port latch (the port latch data is output from the pin.)
Since bit processing instructions are read-modify-write instructions*1, using such an instruction on a port register causes reading and writing to be performed simultaneously on bits other than the one specified by the instruction.
When an unspecified bit is in input mode, its pin level is read and that value is written to the port latch. At this time, if the previous value of the port latch differs from the pin level, the port latch value changes.
When an unspecified bit is in output mode, in general the port latch is read. However, for some ports the peripheral function output is read, and the value is written to the port latch. At this time, if the previous value of the port latch differs from the peripheral function output, the port latch value changes.
*1. Read-modify-write instructions: Instructions that read memory in byte units, modify the value, and then write the result to the same location in memory in byte units.