The magic of the 74LS259
In the previous note I made a pitch for learning Boolean logic so you can solve problems with chips instead of code and suggested browsing parts catalogs to find things you’d never look for.
Now, a demo of a chip I discovered by accident that solves a kerbillion problems I’ve had in the past, the 74LS259. There’s two types of chip you need to know about to understand what makes the 74LS259 great for Arduino projects.
The multiplexer
Multiplexers are pretty common, and if you’ve spent a fair amount of time doing Arduino projects, you’ve probably seen them in demos, if not used them yourself. They are handy for things like routing signals to/from your Arduino when you don’t have enough pins for everything you want to connect to your Arduino. (There’s a short tutorial on the Arduino site that you should go read if you’re unfamiliar with how to use a multiplexer. Read the code, look at the diagram, it’ll make sense.) On the one hand it’s very convenient being able to use 4 pins to drive 8 outputs, however they are real time and don’t have any concept of state or memory. If I have 8 LEDs on a multiplexer, and I want to turn the first one on and off, the others will all go off. Then when I go to turn a different one one on and off, the first one goes off. (There’s a ton of tricks to deal with this, most of them involve some form of persistence of vision.)
The latch
The second component to know about is the “latch”. (Say it in the Monty Python narrator voice for extra fun.) If you didn’t study EE/CS at the college level, odds are you’ve never heard of a latch. I spent 1.5 semesters doing digital logic and I’d pretty much forgotten about latches other than to remember what a pain they were to use in a circuit. The latch is one of those core functions of computers that we tend to forget about because there’s not really a software version that we use while writing code the way we use OR, AND, NOT, and so on.
Simply put, a latch is a way to store 1 bit of data, and that bit’s value is either “on” or “off”. You have an input wire, an output wire, and some sort of “control” wire that lets you change the value stored in the latch. You enable the latch and capture the value on the input wire, then disable the latch and the output wire will continue to have the same value as the input wire did, even if the input wire changes value. Latches can be combined with many other chips to create memory to store bytes of data, move it around on buses, and do all sorts of things that modern computers are based on.
The 74LS259
I’d kinda forgotten about latches until I was killing time flipping through a databook and stumbled upon the 74LS259:
“The SN74LS259 is a high-speed 8-Bit Addressable Latch designed for general purpose storage applications in digital systems. It is a multifunctional device capable of storing single line data in eight addressable latches, and also a 1-of-8 decoder and demultiplexer with active HIGH outputs.[…]”
See the connection? It’s a multiplexer with a latch on every output. To put it another way, it’s a multiplexer with memory. If you want to run 8 outputs and be able to turn them on and off independently, this is the chip you’ve been looking for. Granted, it takes 5 pins to run the LS259: 3 to generate the address, 1 to generate the on/off signal, and one to enable/disable the latch, but you gain the ability to turn things on and off and have them stay the way you left them. Another thing in the LS259’s favor is that it works just like a multiplexer, so there’s nothing new to learn and it will work with your old code with only a slight modification. You use three pins to select the address of the output pin, set your signal pin high or low, then enable and disable the latch with a fifth pin. After you toggle the latch, you can move on and set the value in the next pin.
It’s just this easy: LS259.pde
[tags]arduino,ls259[/tags]