Need help with LAT instruction


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Jun 2016
    Posts
    60

    Default Need help with LAT instruction

    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

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    Why YES you can..
    Dave Purola,
    N8NTA
    EN82fn

  3. #3
    Join Date
    Jun 2016
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    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

  4. #4
    Join Date
    Jun 2016
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    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

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    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.
    George

  6. #6
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    Quote Originally Posted by towlerg View Post
    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.

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    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.
    George

  8. #8
    Join Date
    Jun 2016
    Posts
    60


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    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

  9. #9


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    Yes, RMW usually only rears its head with capacitive loads.
    George

  10. #10
    Join Date
    Apr 2014
    Location
    OK
    Posts
    557


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    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.

  11. #11


    Did you find this post helpful? Yes | No

    Default Re: Need help with LAT instruction

    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.
    George

Similar Threads

  1. Replies: 125
    Last Post: - 24th June 2018, 15:44
  2. Instruction cycle measurement
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 6th August 2012, 08:50
  3. LAT replaces PORT command?
    By markscotford in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd December 2011, 17:37
  4. Instruction times
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th June 2009, 10:16
  5. What does this MCLR instruction mean?
    By bartman in forum General
    Replies: 16
    Last Post: - 30th November 2004, 01:32

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts