PORTx or LATx

Status
Not open for further replies.
When LATx is read it returns the value that was last written to the port.
When PORTx is read it returns the value that is on the pins, even if the pin is set to output.

So, when setting or clearing an output pin use LATx. When testing an input pin use PORTx.

The difference is subtle but can cause problems.
For example,
If you have a capacitive load on bit 3 of a port and do the following sequence,
bsf PORTx,3
bcf PORTx,4
then the capacitive load on bit 3 may not have charged yet and when the processor reads the port during the bcf, it will read bit 3 as low and will subsequently write it back as 0. By using LATx this problem doesn't occur.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…