»

Jul 07

Using Shift Registers to expand your output

Shift registers are fantastically useful little chips to use when you need a few more I/O pins than your microcontroller offers you.

They essentially allow you to turn 3 output pins into 8. So let’s have a look at a fairly normal shift register chip the ON MC74HC595A:

Logical layout from the datasheet

Inputs

The inputs we’re interested in if we want to use this to expand the output of our microcontroller are:

  • The Serial Data line
  • The Shift Clock
  • The Latch Clock

Now you may be thinking what about the Reset line, what about Output Enable? Well we don’t really need to use these.

  • Reset: Each time we shift data into the shift register we’ll be doing 8-bits at a time “overwriting” the previous values so reset isn’t really very valuable to us (we’d much rather that extra I/O pin on our uC). You’ll notice that the Reset line is an inverted input (see the little circle at the end of the line?) so we want our chip to always be not in the reset state so we just tie that pin high (to 5V in the case of this chip).
  • Output Enable: We want the chip to always have its outputs enabled. I’m guessing you’d rather have an extra uC I/O line than worry about the tiny amount of time at start up when the shift register has not been fed any values and so all its output are on. The line is again an inverted input so it’s really not Enable so we tie it to ground to have our outputs always on.

How do you work this thing?

Well it’s pretty simple, whenever the chip detects a rising edge (i.e. from ground to +5V) on its shift clock input it shifts the bits current stored in the shift register along one (so the highest bit drops off the end to be lost forever) and the lowest bit of the shift register becomes whatever the serial data input line is currently set at. So we fill up our shift register one bit at a time until we have all 8 bits, once all of our new 8-bits are into the shift register we want to send a rising edge to the latch input, this will transfer the bits in the shift register onto our output pins (QA-QH) on the chip.

So in in pseudo code:

ShiftRegisterPort = 8bit value we want on the output of our shift register
for(bit=0;bit<8;bit++){
  uC Shift Clock Pin = 0;
  uC Serial Data Pin = RegisterPort Bit (7-bit);
  uC Shift Clock Pin = 1;
}
uC Shift Clock Pin = 0;
uC Latch Clock Pin = 1;
uC Latch Clock Pin = 0;

The timing diagram looks a bit like this:

Rough timing for writing to the chip

That’s it

And that’s pretty much it. If you are not too fussy about the state of your outputs you can even run this from 2 pins by tying the shift and latch clock lines together, this does give you a ripple effect as the bits are output as they are shifted into the shift register. If you do this you also have to clock 9 times (with the last data bit being ignored) as otherwise the 8th data bit is not latched from the shift register. For slowly changing outputs, ┬áthis could work quite well, however if you want to do PWM, for example, then you don’t really want your outputs rippling all the time as values are shifted along.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>