Mikroelektronika MIKROE-442 Datenbogen
174
mikoBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
Read Modify Write Problem
The Microchip microcontrollers use a sequence known as
Read-Modify-Write (RMW) when changing an output state
(1 or 0) on a pin. This can cause unexpected behavior under certain circumstances.
When your program changes the state on a specific pin, for example RB0 in PORTB, the microcontroller first
READs
all 8 bits of the PORTB register which represents the states of all 8 pins in PORTB (RB7-RB0).
The microcontroller then stores this data in the MCU. The bit associated with RB that you’ve commanded to
MODIFY
is changed, and then the microcontroller
WRITEs all 8 bits (RB7-RB0) back to the PORTB register.
During the first reading of the PORT register, you will be reading the actual state of the physical pin.
The problem arises when an output pin is loaded in such a way that its logic state is affected by the load. Instances of
such loads are LEDs without current-limiting resistors or loads with high capacitance or inductance.
For example, if a capacitor is attached between pin and ground, it will take a short while to charge when the pin is set
to 1.
On the other hand, if the capacitor is discharged, it acts like a short circuit, forcing the pin to ‘0’ state, and, therefore, a
read of the PORT register will return 0, even though we wrote a 1 to it.
Lets analyze the following example:
PORTB.B0 = 1
PORTB.B1 = 1
Assume that the PORTB is initially set to zero, and that all pins are set to output. Let’s say we connect a discharged
capacitor to RB0 pin.
The first line,
PORTB.B0 = 1
will be decoded like in this way:
READ
PORTB is
read:
STORE
Data is stored inside a temporary internal
register in the MCU: