Author Topic: PIC MCU LAT register  (Read 840 times)

0 Members and 1 Guest are viewing this topic.

Offline sairfan1Topic starter

  • Frequent Contributor
  • **
  • Posts: 348
  • Country: ca
PIC MCU LAT register
« on: September 07, 2023, 06:25:29 pm »
I'm back to PIC MCU after long time.  For PIC MCU we can use TRIS to set direction of port like input/output and we can read or set values directly from port like PORTA
I totally for got what is the purpose of the LAT register here like LATCbits.LATC0 I remember its something related to assigning value to a port bits but with is difference between setting a value to LATx or PORTx
 

Offline FireBird

  • Regular Contributor
  • *
  • Posts: 68
  • Country: at
Re: PIC MCU LAT register
« Reply #1 on: September 07, 2023, 06:49:13 pm »
You use the PORT register to read the state of the pins and the LAT register to set the state of output pins.
 

Offline jpanhalt

  • Super Contributor
  • ***
  • Posts: 3479
  • Country: us
Re: PIC MCU LAT register
« Reply #2 on: September 07, 2023, 08:00:10 pm »
As stated, in theory, you must read a Port and write a LAT.  Unfortunately, they are in different banks, which should require a Bank change, but they have the same offset (I am only familiar with 8-bit MCU's).  Sometimes, I write the port on a LAT-chip as one did with the much older, non-LAT chips and it works fine.  Simulation, both hardware and software, shows the LAT bit being changed. That is not a recommended practice, though.
 

Offline MarkF

  • Super Contributor
  • ***
  • Posts: 2550
  • Country: us
Re: PIC MCU LAT register
« Reply #3 on: September 08, 2023, 04:15:30 pm »
LAT vs PORT really depends on the flavor of PIC you're using.
The best way to understand the difference is by looking at the 'port' section of the datasheet.
Here is a highlighted excerpt from a 32MX PIC:

   

I believe the reason for the incorporation of the LAT register was to allow read-modify-write operations.
 
The following users thanked this post: sairfan1

Offline jpanhalt

  • Super Contributor
  • ***
  • Posts: 3479
  • Country: us
Re: PIC MCU LAT register
« Reply #4 on: September 10, 2023, 07:24:56 pm »
I believe the reason for the incorporation of the LAT register was to allow read-modify-write operations.

RMW operations have always been allowed, and it is my understanding that C did not include work arounds natively.  That is, bit setting was common.

A lot of hullabaloo is made about RMW, but if one paid attention to what was on the port, it was infrequently a real problem.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5913
  • Country: es
Re: PIC MCU LAT register
« Reply #5 on: September 10, 2023, 08:00:21 pm »
It's the opposite, to remove the problems derived from RMW using PORT.
11.2.3 LAT Registers
The LATx register associated with an I/O pin eliminates the problems that could occur with
read-modify-write instructions. A read of the LATx register returns the values held in the port
output latches, instead of the values on the I/O pins. A read-modify-write operation on the LAT
register, associated with an I/O port, avoids the possibility of writing the input pin values into the
port latches. A write to the LATx register has the same effect as a write to the PORTx register

For example, you could write "1" to RA1, then read RA1 again, and get 0 because the output didn't had enough time to toggle.
Or if the output was shorted, you would always read 0 no, matter what.
Reading/writing to the LAT register avoid all this.
The output might be shorted or not have toggled yet, but LATA1 will be read "1" just after setting it.

Another example. Imagine you wanted to toggle some outputs really fast in a PIC18.
This might NOT toggle RA1 4 times!
Code: [Select]
btg    PORTA,1,0
btg    PORTA,1,0
btg    PORTA,1,0
btg    PORTA,1,0

But this will for sure (Or at least try, no matter the output load):
Code: [Select]
btg    LATA,1,0
btg    LATA,1,0
btg    LATA,1,0
btg    LATA,1,0
« Last Edit: September 10, 2023, 08:09:06 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: sairfan1


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf