PDA

View Full Version : Need help with LAT instruction



Alberto
- 14th August 2017, 17:58
I have recently changed mcu for memory space. I moved from 18f2620 to 18f26k20. But the new pic needs the LAT instruction to work properly.

Now I have several alias like:

Bit_Out0. Var portA.6

or

Bit_In0 var portB.0

Can I write:

Bit_Out0 var LATA.6

and

Bit_In0 var LATB.0

I read the manual and searched the forum without any luck!

Thank you for the answer.

Alberto

Dave
- 14th August 2017, 18:30
Why YES you can..

Alberto
- 14th August 2017, 20:09
Thank you Dave for your answer.

The fact is that I am a litlle lost with this LAT register!

I have a blinking led connected to portA.5 and with 18f2620 I used the alias : Led var portA.5, then the instroction

Led = !led made the led blinking.

Now with 18f26k20 this instruction doesn't work anymore but also LATA.5 = !LATA.5 doesn't work

Am I using the instruction the wrong way? Using the alias will it work?

Alberto

Alberto
- 14th August 2017, 22:22
Ok problem solved!

It was the ansel register that was not set properly so portA.5 was set as analog and hence not responding to a digital command.

Thank you again.

Alberto

towlerg
- 15th August 2017, 13:19
Alberto, read about "read modify write" and you'll understand the purpose of LAT. If you write to PORTx and read from LATx you should be squeaky.

mpgmike
- 16th August 2017, 02:38
Alberto, read about "read modify write" and you'll understand the purpose of LAT. If you write to PORTx and read from LATx you should be squeaky.
That's his way of saying don't do it. Read from PORT and write to LAT is the correct message conveyed.

towlerg
- 16th August 2017, 14:51
Mike, I did a little research and came to the conclusion that you should both read and write the LAT register.

My logic is a follows, as you correctly say you must write to LAT (to set the latches and thus remember the correct state of the gpio ) but if you read from the PORT you're still left with the RMW problem. But is you read from LAT that goes away.

Alberto, sorry for the confusion.

Alberto
- 16th August 2017, 19:01
Tank you George for the additional input. Yes read and write to LAT register seems the correct way to go. I did change the original alias from (led var portA.5) to (led var LATA.5) and everything works fine both read and write without changing anything in the original code. (Appart the register setting which are quite different from the 18F2620)

Alberto

towlerg
- 17th August 2017, 12:54
Yes, RMW usually only rears its head with capacitive loads.

mpgmike
- 22nd August 2017, 17:26
More specifically, READ from PORTX.Y where the port is deemed an INPUT. This way you can see where the port is. LAT controls OUTPUT. Never tried reading a LAT register when using the port as an INPUT.

towlerg
- 23rd August 2017, 15:36
Not wishing to be argumentative but it's reading individual bits that is the problem with RMW. Imagine that the bit we want to write to has a capacitive load. To write to a bit (using PORT) the PIC reads all 8 bits, modifies the specified bit and writes back. If we now write to another bit but the load has prevented the original bit from changing, the PIC reads all 8 bits (getting a wrong value from our original bit), modifies and writes back. That why the problem is called Read Modify Write. There are 2 ways to overcome this, either buffer in software or use LAT, both of which "remember" what value was last written.