• AVR Freaks

Hot!What is the Latch State of a Port?

Author
Tim Cruise
Junior Member
  • Total Posts : 94
  • Reward points : 0
  • Status: offline
2019/04/22 02:07:56 (permalink)
0

What is the Latch State of a Port?

Dear All,
I learn from Arduino Uno that electric potential level of each port controls the input/output state of this port.
 
In Microchip 8 bit MCU, there is a Latch state of each port besides its electric potential level.
 
Could you explain the meaning of this Latch state?   Smile
#1

5 Replies Related Threads

    Mysil
    Super Member
    • Total Posts : 3326
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: What is the Latch State of a Port? 2019/04/22 03:07:57 (permalink)
    +1 (1)
    Hi,
    All digital I/O pins have output Latches, PC with parallel Port ( Centronics printer interface),
    Atmel microcontroller, PIC microcontroller, or anything else.
    Output Latch is a FlipFlop  that is Set or Cleared, to control and keep the Output state of each Pin.
    Output Latches are organised into a register, with one bit for each pin of a Port. 
    The register will be written when addressed by the peripheral address and data bus.
     
    However, Not all devices are able to Read the state of the output Latch register
    when the Pin is configured as Input.
     
    In Old PIC devices, there is no address for the LATch register in the SFR memory map.
    Writing to the PORT register address will go into the LATCH flip-flops.
     
     
    Reading from the PORT register address will however Read from the digital Input buffers,
    these are different circuits, that are connected to the same pins.
    When a Port have some pins configured as Inputs, and other pins as Output,
    this may have some confusing effects, with some signals changing when not expected.
    You may search in forum messages about RMW effect and problems.
    Also when microcontroller is running faster, with instruction frequency higher than 1 MHz,
    then problems become even more noticable.
     
    Thus, modern PIC microcontrollers, PIC16F1xxx and PIC16F1xxxx, PIC12F1xxx, ...
    have separate SFR memory addresses for LATx register, and for PORTx register.
    Write to LATx register or write to PORTx register actually go into the same place, the register of Port LATches.
    Reading is different:
    Reading from LATx register will read the contents of LATch flip-flops. 
    Reading from PORTx register will read from digital Input circuits.
     
    See PORT chapter in Datasheet , and Diagram of I/O Port Operation.
     
        Mysil
    #2
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: What is the Latch State of a Port? 2019/04/22 06:42:01 (permalink)
    +1 (1)
    HKPhysicist
    I learn from Arduino Uno that electric potential level of each port controls the input/output state of this port.

    Which tells you how an Arduino works.
    That is inappropriate terminology for a PIC which has TRIS and PORT (and maybe LAT) registers.
     

    Nearly there...
    #3
    oliverb
    Super Member
    • Total Posts : 204
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: What is the Latch State of a Port? 2019/04/24 01:16:43 (permalink)
    +1 (1)
    PIC18 parts have a "PORT" register and a "LAT" register.
    Reading "PORT" reads the electrical state of that pin. Using bit operations (read-modify-write) on the PORT register can have unexpected results as it copies the pin state into the output latch.
    Reading "LAT" reads the value of the output latch irrespective of the electrical state of the pin.
     
     
    #4
    Tim Cruise
    Junior Member
    • Total Posts : 94
    • Reward points : 0
    • Status: offline
    Re: What is the Latch State of a Port? 2019/04/24 01:31:04 (permalink)
    0
    Thanks.   grin
     
    And how about Tri State?  How to define the 3 states of a pin in a command?
    High voltage Output;
    Low voltage Output;
    Input.
    #5
    Mysil
    Super Member
    • Total Posts : 3326
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: What is the Latch State of a Port? 2019/04/24 02:04:45 (permalink)
    +2 (2)
    Hi,
    There are 2 instructions needed, on 2 different registers, to fully control the 3 states of a pin:
    High logic level Output:
        LATxbits.pin = 1;   TRISxbits.pin = 0; 

    Low logic level Output: 
        LATxbits.pin = 0;   TRISxbits.pin = 0; 

    Input:    
        __bit  PinState;     /* Define a variable to receive the pin state value.    */
        TRISxbits.pin = 1;
        PinState = PORTxbits.pin;

    If the pin have the possibility to be used for Analog signals,
    then also the Digital Input buffer must be enabled Before it is possible to get correct reading of the pin state:
        __bit  PinState;     /* Define a variable to receive the pin state value.    */
        TRISxbits.pin = 1;
        ANSELxbits.pin = 0;      /* This pin is not connected to a analog input signal. */
        PinState = PORTxbits.pin;

     
        Mysil
    post edited by Mysil - 2019/04/24 02:24:43
    #6
    Jump to:
    © 2019 APG vNext Commercial Version 4.5