|"Microchip's mid-range PIC Microcontrollers use a sequence of operations : Read, Modify and Write to change output state of a pin. In certain circumstances RMW operations might cause unexpected behavior of outputs. You might have already experienced this issue and struggled with it as you can't find exact reason with microcontrollers unexpected behavior. Mid-Range PIC Microcontroller uses two registers, TRIS and PORT to control the direction and status of an IO pin respectively.|
When you try to change the status of a specific pin, consider 7th bit of PORTB (RB7), the microcontroller reads all 8 bits of PORTB register and stores this data in a temporary register. Then modifies the 7th bit as per the instruction and the modified data is written back to PORTB. This process of modifying the output of a pin can cause unexpected behavior as the PORT Read operation reads the actual state of physical pin.
The actual physical state (voltage level) of a pin may be different from the value written to PORT register in certain instances. For example,
When the output pin is connected to high inductance or capacitance loads.
When the output pin is connected to LEDs without current limiting resistors.
When the output pin drives a transistor without base resistor.
When the output pin is shorted to ground.
If a high value capacitor is connected to a pin, it will take some time to charge the capacitor when the pin is set to HIGH."