• AVR Freaks

Hot!what is the difference between PORTA and LATA ?

Author
pubuducg
Starting Member
  • Total Posts : 86
  • Reward points : 0
  • Joined: 2007/04/22 17:18:51
  • Location: 0
  • Status: offline
2007/04/24 12:59:32 (permalink)
0

what is the difference between PORTA and LATA ?

Hi,
 
  I would like to know what is the difference between PORTA and LATA commands (or any other port) . i.e. PORTA = 2; LAT = 2; . For me both seems to be the same when working on a PIC. Can somebody explains it to me about the difference.
 
 
Pubudu.
#1

16 Replies Related Threads

    zardoz1
    Super Member
    • Total Posts : 1852
    • Reward points : 0
    • Joined: 2005/07/09 08:03:28
    • Location: 's-Hertogenbosch, The Netherlands
    • Status: offline
    RE: what is the difference between PORTA and LATA ? 2007/04/24 13:07:21 (permalink)
    +3 (2)
    When writing to an output, actually both do the same but there is a very important difference.
     
    Regularly, when writing to a port, the value is also read back. When writing to a PORT, the value read back very short after writing it is not neceserally the same as the value written. Due to capacative load, when writing a one (1), it can take some time for the pin to go to the level of the logical 1. There is a slope in this signal related to the capacative load on the pin. So when reading the value, immediatelly after writing it, it might happen that these values differ.
     
    Not so when writing to the LATCH register. This is a PIC internal register that stores the value. When writing a one to the LATCH, this value is saved there immediatelly and also written to the PORT. So although in this case the level on the PORT might take some time to reach the intended level, the value in the LATCH register does have the intended level (or better said state). So in this case when reading from LATCH, this definatelly is the value you just wrote to it, even if the read is one cycle later (which is 25 ns on a 40 MIPS dsPIC33F).
    #2
    Kiwi Mike AZ
    Super Member
    • Total Posts : 2056
    • Reward points : 0
    • Status: offline
    RE: what is the difference between PORTA and LATA ? 2007/04/24 13:17:15 (permalink)
    +3 (2)
    ORIGINAL: pubuducg
    I would like to know what is the difference between PORTA and LATA commands (or any other port) . i.e. PORTA = 2; LAT = 2; . For me both seems to be the same when working on a PIC. Can somebody explains it to me about the difference.


    LATx is a Latch - anything written to it is latched into the register and when ever the IO port is an output, its data will appear on the output.
    Not affected by the state of the IO Pins for read-modify-writes

    The PORTx is effectively the direct pin connection - a read-modify-write instruction depends on the actual state of the pin.
    A read from PORTx reads the state of the physical pins
    A write to PORTx is not guaranteed to actually output the data, or can be delayed, and the actual result can vary especially for read-modify-write commands.

    The recommendation I had from the Mocrochip support staff is to use PORTx for reading data in and LATx for writing data out - especially when using single bits for I/O.

    If you are using the whole port as an output is will most likely work fine just using PORTx, but if a PORT is split up into inputs and outputs, or bits used individually then it is recommended to use LATx for any outputs.

    Personally I use LATx for all my outputs now as it works every time without fail.

    Mike
    #3
    Olin Lathrop
    Super Member
    • Total Posts : 7463
    • Reward points : 0
    • Joined: 2004/02/26 17:59:01
    • Location: Littleton Massachusetts
    • Status: offline
    RE: what is the difference between PORTA and LATA ? 2007/04/24 14:28:18 (permalink)
    +1 (1)
    A write to PORTx is not guaranteed to actually output the data

    Your explanation is confusing at best.  LAT and PORT function identically for writing.  Reading from PORT gives you a snapshot of the actual pins state regardless of how, if at all, the PIC is trying to drive them.  Reading from LAT gives you the values last written to the pins, whether thru LAT or PORT.
     
    The distinction in read behaviour becomes important in places you might not think of because of read-modify-write instructions.  For example BSF reads the whole byte, sets the indicated bit to 1, then writes the whole byte back.  If you have a sequence like:
    ;   Port B is set to all outputs and all pins driven low.
    ;
              bsf     portb, 0    ;set RB0 pin high
              bsf     portb, 1    ;set RB1 pin high
    there is a good chance that RB0 will be low at the end.  The first instruction reads 0 from port B, changes that to 1, and writes the 1 to port B.  That works as expected.  However, there is some capacitive loading on RB0 and when the second instruction reads port B the pin hasn't gone far enough high yet and the instruction reads 0.  This gets changed to 2, which is written back to port B.  Now RB0 has been explicitly set to 0 again.
     
    The solution in this case is to perform the BSF operations on LATB instead of PORTB.
    #4
    Kiwi Mike AZ
    Super Member
    • Total Posts : 2056
    • Reward points : 0
    • Status: offline
    RE: what is the difference between PORTA and LATA ? 2007/04/24 15:33:52 (permalink)
    0

    Your explanation is confusing at best.


    True - looking back at it it is confusing.... shouldn't write replies before breakfast when I am not quite awake.

    Mike
    #5
    biozen
    Senior Member
    • Total Posts : 122
    • Reward points : 0
    • Joined: 2004/10/09 09:00:47
    • Location: Agra, India
    • Status: offline
    RE: what is the difference between PORTA and LATA ? 2007/04/24 21:34:59 (permalink)
    +1 (1)
    In short:

     When you want to read a port, use PORTx.
        movff    PORTA,TempStore    ; Store PORTA status


     When you want to write to a port or its pin, use LATx
        bsf    LATA,0       ; Set PORTA pin 0


    --
    Mohit Mahajan.
    #6
    pubuducg
    Starting Member
    • Total Posts : 86
    • Reward points : 0
    • Joined: 2007/04/22 17:18:51
    • Location: 0
    • Status: offline
    RE: what is the difference between PORTA and LATA ? 2007/04/25 09:16:12 (permalink)
    0
    Hi,
     
      Thank you very much to you all . . . You made my life easier . . .
     
    Pubudu
    #7
    RITZ
    Starting Member
    • Total Posts : 52
    • Reward points : 0
    • Joined: 2005/04/03 14:06:52
    • Status: offline
    RE: what is the difference between PORTA and LATA ? 2007/05/05 16:44:23 (permalink)
    0
    Hi, I was told to use LAT instead of PORT in another thread for output and I found this thread explaining why. But I have a question: If I programmatically write to PORTx directly should the write instruction subsequently update the corresponding LATx register? Please note that I am asking if writing to PORTx will also set LATx, not the other way around, I already understand that LAT is a buffer for PORT manipulation.


    One more question: Doesn't the existance of LAT make it pointless to have to configure any port to input or output? Instead every port should be I/O. If you want to use it for output you write to the LAT, and if you use it for input you read the PORT. What kind of circumstances does only input or output protect from when you have seperate registers for both input and output?
    post edited by RITZ - 2007/05/05 16:54:34
    #8
    bob_barr
    Super Member
    • Total Posts : 5428
    • Reward points : 0
    • Joined: 2003/11/07 12:35:23
    • Location: Morgan Hill, CA
    • Status: offline
    RE: what is the difference between PORTA and LATA ? 2007/05/05 17:12:15 (permalink)
    0
    ORIGINAL: RITZ

    Hi, I was told to use LAT instead of PORT in another thread for output and I found this thread explaining why. But I have a question: If I programmatically write to PORTx directly should the write instruction subsequently update the corresponding LATx register? Please note that I am asking if writing to PORTx will also set LATx, not the other way around, I already understand that LAT is a buffer for PORT manipulation.

    Writing to either the LATx or PORTx register will have the same effect. The value is written to the output (for any pins which are set as outouts) and updates the latch register at the same time. By performing bit set, bit clear, and logical operations on LATx, you guarantee that a heavily loaded pin won't cause a read-modify-write problem.
     
    I also find it convenient to search for port reads versus writes being able to search for 'PORTB', for example, to find the reads and 'LATB' to find the writes.


    One more question: Doesn't the existance of LAT make it pointless to have to configure any port to input or output? Instead every port should be I/O. If you want to use it for output you write to the LAT, and if you use it for input you read the PORT. What kind of circumstances does only input or output protect from when you have seperate registers for both input and output?

    No, each pin can be switched from input to output and back but the pins can't be both at the same time. That's what the TRIS register does. You might want to look at the pin block diagrams in the datasheet. Doing that may clarify things. 
     
    Under normal circumstances, if you have a bit set as output, you'll read the last value written to that pin when you read the PORT register.

    While it's always good to learn from one's mistakes, it's much easier to learn from the mistakes of others.
    Please don't PM me with technical questions. I'll be quite happy to help (if I can) on the forums.
    #9
    xaccto
    New Member
    • Total Posts : 18
    • Reward points : 0
    • Joined: 2010/01/13 20:28:17
    • Location: 0
    • Status: offline
    Re: RE: what is the difference between PORTA and LATA ? 2020/01/14 21:57:16 (permalink)
    0
    quick question. If i'm toggling LATx.bit,  but then not seeing the PORTx.bit changing in the simulator, should
    I be concerned ? TRISx.bit is set to 0 (output)
    Monitoring that by both the Logic Analyser window and watch variable on PORTx  & LATx
    mplab 5.30
     
    #10
    ric
    Super Member
    • Total Posts : 27923
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: RE: what is the difference between PORTA and LATA ? 2020/01/14 22:06:10 (permalink)
    0
    You don't mention which PIC or which pin.
    My guess is that it is an analog capable pin that you haven't switched to digital mode.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    oliverb
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: RE: what is the difference between PORTA and LATA ? 2020/01/15 03:33:48 (permalink)
    0
    For clarification look at the logic circuit for a port. LAT is read from the output of the "D" latch, so accessing LAT acts like any other memory location. PORT is read from the pin's input buffer. If you want to read the electrical state of the pin use PORT.
     
    To explain read-modify-write: when writing the individual bits aren't really individually accessable, instead a "bit" operation reads the whole byte, changes the bit you want, then writes it back. If you use PORT then mostly it works, but if you have pins configured as inputs you'll find that the input to the PIC gets "mysteriously" transferred to the output register, which can have odd consequences if the pin is later switched to output.
     
     
     
    #12
    pcbbc
    Super Member
    • Total Posts : 1703
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: RE: what is the difference between PORTA and LATA ? 2020/01/15 05:42:31 (permalink)
    0
    oliverb...which can have odd consequences if the pin is later switched to output.

    ...or even more importantly - if it’s currently an output.
    #13
    markm23
    Starting Member
    • Total Posts : 27
    • Reward points : 0
    • Joined: 2019/11/21 12:51:59
    • Location: 0
    • Status: offline
    Re: RE: what is the difference between PORTA and LATA ? 2020/01/15 10:54:51 (permalink)
    0
    xxacto: "If i'm toggling LATx.bit,  but then not seeing the PORTx.bit changing in the simulator, should
    I be concerned ?"
     
    I have noticed that sometimes. Both the simulator and hardware debuggers (I'm using ICD4 for a PIC16F15356) sometimes don't refresh all the displayed values right away - or at all. E.g., I have a union (the same area of memory taken different ways, such as a 32-bit integer or an array of 4 characters), and changing one representation does not show up as a change in the other representation - but the code that reads this memory does respond to the change. I think I've seen this also with LATA vs. PORTA. So there's no real problem, except that it might confuse you in debugging.
     
    And of course, make sure that there isn't a reason the port didn't change: TRIS has it as an input, or the TRIS output selection is being overriden by a peripheral. Or there's a hardware problem forcing the pin to stay high or low... When you're working with actual hardware and a debugger, when in doubt put a scope probe on the pin and see if it actually changed.
     
    And that's part of the answer to the original question:
     
    TRISA = 0xF0;
    PORTA = 0xFF;
    // If the four inputs (1 bits in TRISA) are low, PORTA should now be 0x0F, but LATA is 0xFF.
    // But if the RA0 pin is overloaded, PORTA might now read 0x0E.
    // Now try to change RA1 using PORTA:
    PORTA &= 0xFD;  // This is equivalent to bit-clear PORTA bit 1
    // LATA is now 0x0C - bits 0 and 4-7 read from PORTA as zeroes and were written back as zeroes.
    // When the short is removed from RA0, it will still be 0.
     
    But if you do all changes to LATA instead, it remembers exactly what you wrote, and there are fewer surprises.
    LATA = 0xFF;     // It's 0xFF no matter the TRIS or hardware conditions
    LATA &= 0xFD;  // It's 0xFD, only bit 1 changed.
     
    Finally, in initializing a device, set the LATx for all outputs, then set TRISx to enable the outputs. If you use the other order, pins that need to be high flickered low, and that may put other devices into a different state. (The hardware designer must make sure the outputs are in a safe state while they are tri-stated (inputs), with pull-up or pull-down resistors unless something else will pull them into the desired state. (E.g., I drive relays with an ULN2803 transistor array; when the PIC output pins are tristated, the NPN transistors pull them down to a TTL logic low, so the relays are off until the firmware initializes them.) It's also not good to have input pins floating at in-between levels while you initialize. I haven't seen this with a PIC that I remember, but many IC's will oscillate and malfunction under these conditions.   
    #14
    crosland
    Super Member
    • Total Posts : 2008
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: RE: what is the difference between PORTA and LATA ? 2020/01/16 04:46:50 (permalink)
    +1 (1)
    markm23
    Finally, in initializing a device, set the LATx for all outputs, then set TRISx to enable the outputs. If you use the other order, pins that need to be high flickered low, and that may put other devices into a different state. 

    Gibberish.
    #15
    nigelwright7557
    Super Member
    • Total Posts : 445
    • Reward points : 0
    • Joined: 2006/11/06 08:15:51
    • Location: 0
    • Status: offline
    Re: RE: what is the difference between PORTA and LATA ? 2020/01/16 05:15:49 (permalink)
    +1 (1)
    Simple answer is always write to lat and read from port.
     
    #16
    1and0
    Access is Denied
    • Total Posts : 10972
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: RE: what is the difference between PORTA and LATA ? 2020/01/16 05:31:01 (permalink)
    +3 (3)
    crosland
    markm23
    Finally, in initializing a device, set the LATx for all outputs, then set TRISx to enable the outputs. If you use the other order, pins that need to be high flickered low, and that may put other devices into a different state. 

    Gibberish.

    I think Mark meant presetting the LATx bits to known desired states before configuring the TRISx bits as outputs, so there won't be any glitches at those output pins.
     
    post edited by 1and0 - 2020/01/16 05:32:21
    #17
    Jump to:
    © 2020 APG vNext Commercial Version 4.5