In anticipation for the 8x8x8 LED Cube Instructable (I have a 3x3x3 version here), I am going to explain a key component in operating the cube that I do not feel is explained as nicely as it could be (but please feel free to correct me if you feel otherwise). This is the practical operation of a shift register, as opposed to the theory of a shift register.
Step 1: Background
The vast majority of LED Cubes tend to be operated through some type of a shift register, such as the popular Texas Instruments SN74HC595 serial in, parallel out shift register. It is fairly easy to find out that these shift registers work by receiving an input that represents a '0' or a '1', and that a clock pulse internally shifts the inputs along the shift register, and then another signal allows the received input signals to be displayed. There are many timing diagrams available to illustrate this phenomenon.
Step 2: Away From the Theory!
But while this sounds great, knowing that something should theoretically work does not help much in actually making the shift register on your desk in front of you work.
But no more! Today, I will explain how to figure out what a shift register needs and then show what that looks like in code and on the breadboard.
Step 3: Materials
There are only a few things that we will need to use the shift register. What I will use to demonstrate the SN74HC595 shift register is:
- SN74HC595 shift register (the datasheet is provided on the next step)
- 8 LEDs
- jumper wires
- Digilent's chipKIT Uno32
- the free MPIDE software
If you are not familiar with how to use the MPIDE software, check out Digilent's great tutorial on how to use it.
Step 4: Wiring the Shift Register
If we take a look at the datasheet provided a the bottom of this step, we can see the pinout diagram conveniently on the very first page (also provided just above this step). After we place the shift register on the breadboard, we can then wire the Vcc power to the 3.3V power pin on the Uno32 and the GND to the ground pin on the Uno32 to get them out of the way.
The 3.3V pin was chosen to ensure that the LEDs would not immediately encounter any issues, although it does say on the first page of the datasheet that this particular shift register can operate anywhere from 2V to 6V.
While this is nice, presuming we want our shift register to work, there are still 14 pins left, including our input and clocks.
Step 5: Determining Pin Functions
To figure out what the remaining pins do, there are two main things that I recommend to do.
The first, especially in the case of the SN74HC595 shift register, is to read the opening description paragraph for the device. Here, we find out that 'SER' is the serial input, the 'SRCLR' (with the over-bar) is the overriding clear, 'OE' (with the over-bar) is an output enabling pin, and 'SRCLK' and 'RCLK' are the serial clock and register clock, respectively.
If you do not see this information in the opening paragraph for your shift register, the second thing I recommend is looking for a table in the datasheet that lists all of the pins and what they do. This may be called "pin functions" or "terminal functions" or something similar to that.
Step 6: Great, But What Do These Actually Mean?
The serial input is, well, the serial input which we will feed a '0' or '1' (this will be explained later). The overriding clear is a convenient pin that will put '0's in all of the outputs, which is useful to do at the beginning of a code segment to ensure that there is not any weird data that we don't want in the outputs. The over-bar means that when we write a '0' to that pin (also called driving the pin low), that is when the pin does its designated function, as opposed to applying power to a pin to turn it "on". We will attach both of these pins to digital pins on the Uno32.
The output enabling pin with its over-bar means that this pin must be driven low in order for any of the outputs to work, so let's attach this pin to ground.
The two clocks, the serial clock and the register clock (sometimes known as a latch pin) both shift the inputs in the shift register. Both of these, according to the description, are "positive-edge triggered". This means that for these pins to work, we have to write a '1' to these pins (driving the pin high). For a long time, I thought that these were like real clocks that always needed to "tick" at a constant pace; luckily this is not true, they are more like buttons (in a sense) that we press whenever we want the pin to do its job.
Consequently, this also means that if we want to shift our inputs or in the case of the register clock allow our inputs to be sent on the output pins, that we must drive these pins low, before being able to drive them high to get that "positive-edge". We will attach these pins to digital pins on the Uno32.
Step 7: You Still Have 9 Pins Left...
The remaining pins, which unfortunately aren't super-clearly labeled, are the 8 outputs of our 8 bit shift register. The 9th pin, Qh', is the serial output pin. This pin is where the data gets shifted to once it has been shifted all the way through the shift register. In practice, you could use this pin to "daisy-chain" multiple shift registers together so that they could all use these same clock wires, and only have one input port from the microcontroller.
But for us, we can leave this pin a "floating pin" and not connect it to anything in particular and let all of its outputs go into void, so-to-speak.
The 8 output pins, Qa-Qh, are where our inputs will be displayed, with the first input we tell the shift register on Qh, the second input on Qg, and all the way down to the last input displayed on Qa. We will attach each of these individual outputs to an anode leg of an LED, and the cathode leg of the LED to ground.
Step 8: The Code in Practice- Setup
Now we are at the part which I personally had to figure out by trial and error; coding the shift register.
If you are using a different microcontroller than the Uno32, you may have to adjust the code to conform to its standards, but here is the basic idea.
We want all of the necessary pins to be primed to receive the appropriate input, such as a positive-edge for the two clocks. So, we will digitally write (using the digitalWrite() function) all of our pins to be "LOW". Then to make sure that all of our register input slots are clear, we will update our outputs with the register clock by digitally writing the register clock pin "HIGH", and then bring it back down to low. We will also bring the overriding clear, SRCLR with the over-bar, HIGH so that we can have other inputs besides all zero's. An example of what this looks like is provided below this step.
Step 9: The Code in Practice- Running the Leds
To actually light up the LEDs, we will need to provide 8 different input signals as well as 8 different serial clock pulses to correctly fill the shift register.
We will do this by first digitally writing the register clock LOW to make sure that the outputs do not display their changing data until the correct moment. Then we will digitally write the serial input pin either HIGH or LOW to representing a '1' (turning on the LED) or a '0' (leaving the LED off). Next, we will digitally write the serial clock HIGH to shift the data from the input to the first output slot as well as shifting the data in the other outputs one "place" over. Finally, we'll digitally write the serial clock LOW so that we can get a "positive-edge" for the next bit of data.
After doing this 8 times, we will digitally write the register clock HIGH to display all of the outputs. An example of how to do this is provided just below this text.
Step 10: Refinement and Beyond
You probably realize that lighting up the LEDs in different patterns can result in huge amounts of code really quickly. To fix this, I definitely recommend writing functions to do the patterns for you. An example function that lights up just one LED is provided below.
With this knowledge, you can now use shift registers to power individual (or certain multiples) of objects out of a set. But with respect to the LED cube, you could also use a shift register to choose which LED's get grounded so that electricity can flow through them, as opposed to just having a shift register provide power.
Feel free to ask me any questions you may have!
But I do have one of my own: what other ideas can you come up with for shift register uses?