EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Latches: What Are They For?

Started by Fao, Sean October 24, 2005
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,

-- 
Sean
Fao, Sean <enceladus311@yahoo.comi-want-no-spam> wrote:

> 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.
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.
> Why not just set the bits high/low on my MCU and be done with it?
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 (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.
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.

Somewhere on usenet Mon, 24 Oct 2005 08:04:25 -0700, larkmore wrote:

> 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.
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?
Fao, Sean wrote On 10/24/05 05:46,:

> > 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.
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 wrote:
> Fao, Sean wrote On 10/24/05 05:46,: > > >>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. > > > 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. >
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, -- Sean
larkmore@aol.com wrote:
> 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. >
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, -- Sean
Hans-Bernhard Broeker wrote:

> 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.
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?
> 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.
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, -- Sean
"Fao, Sean" <enceladus311@yahoo.comI-WANT-NO-SPAM> wrote in message 
news:AOednS6q2IqYzMDeRVn-hg@adelphia.com...
> Scott Moore wrote: >> Fao, Sean wrote On 10/24/05 05:46,: >> >> >>>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. >> >> >> 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. >> > > 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.
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.
> 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?
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 <howard_hs@yahoo.com> wrote:

> What you originally thought is correct. I am aware of no > microcontrollers which will behave differently.
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 (broeker@physik.rwth-aachen.de) Even if all the snow were burnt, ashes would remain.

Memfault Beyond the Launch