This has already been discussed previously:
In PIC18 microcontrollers, the effect that occurs when writing to the PORTx registers instead of the LATx registers while using them as outputs is called the "Read-Modify-Write" effect.
Explanation of the Read-Modify-Write Effect:
- Read-Modify-Write Effect: When writing directly to an output pin using the PORTx register, the microcontroller first reads the current state of the port, then modifies the desired bit, and finally writes the value back to the PORTx register.
- Problem: This process can cause errors if any pin on the port changes state during the read-modify-write operation (e.g., due to loads connected to the pin or interference). This can result in an unexpected value being written, as the current state of the port pins is read before modification.
- Solution with LATx:
- The LATx registers are designed to avoid this issue. When writing to LATx, the microcontroller does not perform a read-modify-write operation; instead, it writes directly to the output pins without reading their previous state. This eliminates the risk of unintended changes on the pins during the write operation.
For this reason, it is recommended to use the LATx registers to write to pins configured as outputs instead of PORTx, in order to avoid the read-modify-write effect and ensure stable control of the pins.