1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

Latches: What Are They For?

Discussion in 'Embedded' started by Fao, Sean, Oct 24, 2005.

  1. Fao, Sean

    Fao, Sean Guest

    I'm just starting to get into embedded systems and I recently stumbled
    across latches (as well as flip-flops). But I don't understand what
    they're used for.

    First, if you would please clarify that I understand how they work, I'd
    greatly appreciate it. It is my understanding that I'd usually set the
    bits (in my case, seven bits for a 7-segment LED display) on my
    microcontroller first, and then set the input enable bit high on the
    latch. At this point, the latch will "store" the values that I
    originally set on my microcontroller. Next, I set the output enable bit
    on the latch high and the values get output to my connected device(s).
    At some point, I'd imagine that I'd have to set both the "input enable"
    and "output enable" pins back to low; but, I'm not sure exactly when to
    do this.

    This is where I start to get confused. What was the point of the latch?
    Why not just set the bits high/low on my MCU and be done with it?
    Also, unless I'm missing something, I'll need two bits in addition to
    the seven that I'm already using for my 7-segment LED display so that I
    can assert the "input enable" and "output enable" pins when I'm ready
    for them.

    In my case, I have a simple Assembly program that I wrote that simply
    counts from 0 to 9 infinitely, setting the output pins high/low as
    necessary to turn on/off the individual LED segments to correctly
    display the current value of my counter.

    I browsed the Digi-Key website for latches and came across one that I
    think will work for me (Digi-Key part # 497-1334-5-ND):

    http://www.digikey.com/scripts/dksearch/dksus.dll?Detail?Ref=205311&Row=156926&Site=US

    Can anybody please give me a hand?

    Thank you in advance,
     
    Fao, Sean, Oct 24, 2005
    #1
    1. Advertisements

  2. A big step towards understanding the reason for latches to exists lies
    in this issue. The purpose of a latch is exactly this: to give you a
    choice when to change those enables back to zero. If you don't need
    the choice, you don't need the latch. Depending on the application,
    you may need only one of the two choices.
    Because, typically, you'll be wanting to use those MCU pins for some
    other job in the meantime. If you don't have a need to reuse the
    pins, you don't need the latch.

    An 8-bit latch is, basically, just one byte of external dual-ported
    static RAM. The MCU can write to it anytime it wants, and a client
    (e.g. your 7-segment display) can read from it any time it wants. In
    the case at hand, you wouldn't have to control the output enable by
    the MCU, though --- you can just tie it to active state.

    Or you could use a 7-segment display with an integrated latch. Or
    even one with an integrated BCD-to-7-segment decode and latch.
     
    Hans-Bernhard Broeker, Oct 24, 2005
    #2
    1. Advertisements

  3. Fao, Sean

    larkmore Guest

    Sean,
    There a two big reasons to use latches in
    microcontroller/microprocessor systems. The first is if you are short
    on IO pins on your device. The second is if you are expecting input
    that changes faster than you are able to read it. More specificly this
    happens with pulse based interrupts.

    A latch is a way of saving a value for later without having to dedicate
    resources to it. This will make more sense with some examples based on
    the above scenarios.

    In the first case, let's say you have a microcontroller with 16 IO pins
    free. At first glance it looks like you could connect at most 16
    signals to the device. But if you get several 8 bit latches and put
    their data lines on the same pins, and their enable lines on separate
    pins, you can quickly multiply the available outputs. Using the chip
    you describe that has two enables per latch chip, you could have four
    latches of eight bits each. The data lines remain the same, but you
    would choose one chip's enable line at a time to tell it you want to
    talk to it. It is very common for systems to use another chip called a
    selector/decoder/demultiplexor which further expands the number of
    latches you can support. These work by taking a binary input on the
    address pins and setting exactly one of its otuputs high or low based
    on the binary value. With one of these chips you could then have eight
    bits of addressable space or 256 latches with a single enable signle
    for each. This binary addressing is actually how most microcontrollers
    internal memory mapping is accomplished. For eight bits of data you
    can have 256 "locations".

    The second use of latches is when you need to save an incoming signal
    that might not last long. For example, if you are monitoring a circuit
    that sends you a brief pulse to let you know something occured, like a
    motion sensor perhaps, your software may be busy doing something else.
    If the pulse has vanished by the time your software checks that pin
    again you will miss that information. A latch keeps the data after the
    pulse ends and lets the microcontroller check it when its ready. In
    this case the latch chose usually has a reset signal that allows it to
    be cleared after servicing. Think of it as a mousetrap that catches
    and holds the signal until the user comes around and deals with it.
     
    larkmore, Oct 24, 2005
    #3
  4. Fao, Sean

    sonos Guest

    is the 'pulse' digital or analog, or both? how is your description
    similar/different from a capacitor's sample and hold for an ~ac signal?
     
    sonos, Oct 24, 2005
    #4
  5. Fao, Sean

    Scott Moore Guest

    Fao, Sean wrote On 10/24/05 05:46,:
    If you have a MCU which can set that line to high or low and keep it there,
    then the MCU already has a latch, inside the chip, that drives that line,
    so you would be correct in thinking you can do without it.
     
    Scott Moore, Oct 24, 2005
    #5
  6. Fao, Sean

    Fao, Sean Guest

    Quote honestly, I'm not sure; but, I'm guessing that unless it's
    internal to the processor (Atmega128) that I my board (MAVRIC-IIB from
    http://www.bdmicro.com) does not have one. I'll have to read the datasheet.

    Maybe I'm assuming too much because I thought that if I set a pin
    high/low, it would just stay there until I said otherwise. I guess this
    not true on some MCU's?

    Thank you for the response,
     
    Fao, Sean, Oct 24, 2005
    #6
  7. Fao, Sean

    Fao, Sean Guest

    Your examples have cleared things up a LOT! I have a much better
    understanding now. Also, another bad assumption of mine was that I was
    limited by the # of IO pins I had on my board as to how many external
    devices I could use. And to be honest, I had never ever considered
    second example. I learned a lot from your post.

    Thank you very much for your reply,
     
    Fao, Sean, Oct 24, 2005
    #7
  8. Fao, Sean

    Fao, Sean Guest

    I was with you up until the last sentence. By "active state", do you
    mean that I wire the output enable pin to be at a constant high?
    Another good suggestion and I'll keep this in mind for the future.
    Although, for this particular project, I think I'll stay with an
    external latch so I can --hopefully-- use it for other projects. Also,
    I already have the 7-segment LED displays ;-).

    Thank you very much for your reply,
     
    Fao, Sean, Oct 24, 2005
    #8
  9. There are indeed output latches internal to the Atmega128 on all I/O ports.
    If you have control of a pin/port, and you write to it, the value you write
    will stay there indefiniately until you write a new value.
    What you originally thought is correct. I am aware of no microcontrollers
    which will behave differently. An output port which is free to change value
    after you write to it would be completely useless in most applications. If
    such a microcontroller did exist, it would be clearly documented how to
    externally latch the output values. The idea behind a microcontroller is to
    be highly integrated with minimal external parts count to keep final device
    costs low.
     
    Howard Henry Schlunder, Oct 25, 2005
    #9
  10. Then, as the song goes, you ain't seen nothin' yet. A round of
    applause, please, gentleman, as the all-time classic 8051
    bi-directional port mounts the stage, which at least in some
    devices/configurations will:

    1) drive a logic zero written to the port's register hard enough
    to make it stay zero (or die trying)
    2a) drive a logic one just as hard, but only for a very short moment,
    and after that
    2b) drive it high by only a pull-up resistor roughly in the 100 kOhm
    range, so a connected device gets a chance to drive it to logic zero
    by overpowering the pull-up
    3) report back not the bit that was last written into the output latch,
    but rather the actual state of the pin, but yet
    4) refer to the most recently written value for read-modify-write style
    instructions in order to have those behave as expected
     
    Hans-Bernhard Broeker, Oct 25, 2005
    #10
  11. Yes. If you have no particular reason for the latch output ever *not*
    to provide data to the 7-segment display, you don't need the output
    enable signal controlled by the MCU --- so tie it to the 'enabled'
    state, which in your case is equivalent to one.

    Figuratively speaking: if you have no intention ever to use the brake
    on your car, you might as well get rid of the brake pedal to save some
    precious legroom. ;-)
     
    Hans-Bernhard Broeker, Oct 25, 2005
    #11
  12. Fao, Sean

    Fao, Sean Guest

    Well that's almost too simple! And as long as there are no adverse
    affects, I don't believe I currently have any reason not to have the
    signal sent as soon as I set the input pin high. Although, I must admit
    that I can think of at least one example where it would come in handy to
    have the choice.
    With gas prices the way they are, I might as well take off the brake
    pads, too...it'll cut back on friction and improve fuel economy. Got
    anything else I can take off? ;-)

    Thanks again for the help,
     
    Fao, Sean, Oct 26, 2005
    #12
  13. Fao, Sean

    larkmore Guest

    Be very careful with this one! Check the data sheet for your latch and
    make sure the output enable is level sensitive and not edge sensitive.
    This is one of the major differences between a "latch" and a
    "flip-flop" and people often use the terms interchangably when they
    shouldn't. A D type flip-flop will not propogate the input signal to
    the output signal without seeing a clock edge! So if you are using a D
    flip-flop to latch your data you will need to strobe (send a pulse with
    a high and a low edge) the clock pin. Simply tying the line high or
    low will NOT work. If you are unsure what kind of hardware you're
    dealing with, post the part number or a link to the datasheet and we'll
    check it over.
    -Will
     
    larkmore, Oct 26, 2005
    #13
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.