When performinging READ-MODIFY-WRITE action, some target board setup may encounter problems. Don't know what is this? Simple. Let's look at the following example:
PORTB=~PORTB;
The statement required the PORTB to be read first then invert then assign back to PORTB, when executed. This is called READ-MODIFY-WRITE.
Some students (not all) encounter problem whereby the execution does not lead to the toggling port PORTB.
The remedy is to change the statement to: PORTB=~LATB.
Why? All the I/O ports in the PIC18F are buffered when they are configured as output ports. In this context, all the 8 bits of PORTB are directed from the output sides of the buffer. The output devices (e.g. LEDs) may have loaded heavily and causing the voltage to drop far below the VDD value. When the READ action takes place the logic states might have been read wrongly.
Consider that the PORTB LEDs are currently lighted up by logic 1s. If the READ-MODIFY-WRIRE statement mentioned above is used to toggle the LEDs. The dropping of VDD by heavy loading may cause the READ action to read logic 0's instead. After toggling and assigning back the PORTB, i.e. 0's become 1's and assign these 1's back to PORTB. The LED's will still be lighted up hence toggling does not take place.
On the other hand the 8 bits in LATB are directed from the input sides of the buffer. What ever loading occurs on the output does not affect the logic level of the buffer inputs. Hence, it works.
Moral of the story (Rule of thumb): when performing READ-MODIFY-WRITE, use LATx instead of PORTx.
PORTB=~PORTB;
The statement required the PORTB to be read first then invert then assign back to PORTB, when executed. This is called READ-MODIFY-WRITE.
Some students (not all) encounter problem whereby the execution does not lead to the toggling port PORTB.
The remedy is to change the statement to: PORTB=~LATB.
Why? All the I/O ports in the PIC18F are buffered when they are configured as output ports. In this context, all the 8 bits of PORTB are directed from the output sides of the buffer. The output devices (e.g. LEDs) may have loaded heavily and causing the voltage to drop far below the VDD value. When the READ action takes place the logic states might have been read wrongly.
Consider that the PORTB LEDs are currently lighted up by logic 1s. If the READ-MODIFY-WRIRE statement mentioned above is used to toggle the LEDs. The dropping of VDD by heavy loading may cause the READ action to read logic 0's instead. After toggling and assigning back the PORTB, i.e. 0's become 1's and assign these 1's back to PORTB. The LED's will still be lighted up hence toggling does not take place.
On the other hand the 8 bits in LATB are directed from the input sides of the buffer. What ever loading occurs on the output does not affect the logic level of the buffer inputs. Hence, it works.
Moral of the story (Rule of thumb): when performing READ-MODIFY-WRITE, use LATx instead of PORTx.
Comments