PIC PORTA vs LATA

Status
Not open for further replies.

Oznog

Active Member
I want to be sure I know the difference between PORTx and LATx in the PIC. My C compiler seems to allow them interchangably.

I saw in the PIC18 data sheet that LATx was memory mapped and thus could be used with a Read/Modify/Write option, whereas PORTx only allows a read or write of the whole port and any "Modify" would involve storing the value elsewhere, which takes multiple cycles and thus involves the risk of losing values from pins used as inputs.

Is that correct? Is that the only difference? I think the compiler is selecting the proper assembly instruction for the operation and the PORTx/LATx aren't different, but I'd like to check.
 
I use the HI-TECH C compliler and you have to explicity state which command you want to use. If you use PORTx for read-modify-write you will get erronious results.
 
This prob with PORTx only corrupts the stored output latch value for a pin declared as an input, and only shows up when the pin is changed to an output before reassigning the output latch?

So, is there ever any reason to use PORTx over LATx anymore, or is it just a matter of backwards compatability?
 
I saw the problem using PORTx when assigning the output value using bit wise assignments.

For Example:

PORTA |= 0x04;
PORTA |= 0x01;

This will cause oscillations of the output values.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…