Here's a (very rough) sketch of a 74HC595 8-bit register.
So, assuming the shift register is properly connected to the Arduino, each of the outputs from 0-7 (8 total) are treated as bits, which, for those of you who don't know, are the protons and electrons of the information world. Bits are the fundamental building block of all data from the simple byte (8 bits) all the way up to the highest definition video file. They're pretty easy to understand as they can only be 1 or 0, which is handy if you want to store something that only has two possible values; a check box on a form for example (which can be either YES or NO,) or in our case whether a pin has power or not (HIGH or LOW.) Bits have been with us since the dawn of computer logic; since the early Jacquard Looms which took punch cards embedded with rows bits that told the loom what action to perform next.
You'll notice the shift register above is an 8-bit registers, and as almost anyone familiar with computer networking and IP addresses knows, 8 bits make up a byte. A byte can have 256 possible values from 0 to 255. Why 256? Because instead of counting in the decimal we were taught in grade school (which goes in sets from 0 to 10,) you're counting in binary, which goes in sets of 0 or 1.
This is so simple it's deceptively confusing. From the right, the first value in a line is the 1's place. It can be either 0 or 1. The second value is the 2's place. It can be either 0 or 2. It basically doubles from here; 4,8,16,32,64,128 etc. So if you had a value of 10, you'd add 0 in the 1's place to 1 in the 2's place, giving you 2. this would make 11 3, 110 6, and it goes on and on.
Apply this to our shift register above. After passing the clock and latchpin and the way we want it to process the data, we give the shiftout function a number representing the pins we want to toggle. So let's say we wanted to turn output pins 0 and 2 on, but leave 1 off.
Imagine it like this:
Pin 0: 1 (1=ON)
Pin 1: 0 (0=OFF)
Pin 2: 1
101 in decimal would be 5 (1 in the 4+0 in the 2+1 in the 1.) Likewise if we wanted to turn 0-2 on we'd have 111, which would be decimal 7 (4+2+1.) If you're a bit confused as to why we start with pin 0, it's relatively simple; computers start counting at 0 instead of 1 as it is actually the first value in our number system.
So let's say we wanted to hook up another shift register. Do we need to take up another 3 pins on the Arduino? Not at all; we just hook the data pin of Register 2 to the overflow pin of register 1, hook the power and ground of register 2 to the proper rails, and connect the clock and latch pins of Register 2 in parallel with Register 1. It's all diagrammed here.
The wiring seems simple enough, so what about the logic? Before I read up on it, I confess that I assumed it was as simple as passing it a larger number. Each register is an 8-bit address space with 256 possible values, so I assumed that if I passed the register the number 511 it would automatically overflow, lighting up all 8 of Register 1's LED's and the first LED of Register 2.
How naive I was.
Turns out you have to pass the shiftout to the register twice (there's no sense showing the code here, as this blog, again, demonstrates it quite well.) The first shiftout will pass the value to register 1 and the second will overflow to register 2. Each shiftout must contain a value between 0 and 256, indicating which bits you want to turn on or off. I edited the above blog's code (as I only had room for two additional LED's on my breadboard,) and eureka; it worked perfectly.
Hopefully this will provide some basic help understanding both the shiftout function and the broader topic of shift registers in general.
No comments:
Post a Comment