Novice Question re shift registers

I've seen a lot of information on how to use shift registers to control multiple outputs, but all of the examples I've seen presuppose that you are controlling the maximum number of outputs that a particular shift register can handle. I'm confused as to how to handle a situation where I want to control less than eight outputs.

Specifically, I have an idea for a dice roller. The concept is that after generating a random number, the code would transmute that into a seven-digit binary number to reflect the LED "pips" which should be turned on, based on the physical location of pips on an actual die (i.e., for each digit, 1 would mean that the appropriately numbered pip would be on, 0 would be off). I would then pass that number to the SR, which in turn would control the seven LED pips.

Since the shift register expects eight bits of data, I assume that I actually have to pass it an eight-digit binary number, with either the first or the last digit always 0. I assume that somehow, whether the first or the last digit is always zero is related to whether I use MSBFIRST or LSBFIRST in the shiftout command, and is also related to which data pin (0 or 7) is left unconnected. However, I am totally confused as to how this all fits together (i.e., which digit do I set to zero, which pin do I leave unconnected, and do I use MSBFIRST or LSBFIRST).

Any suggestions which would help me understand this would be appreciated.

Thanks.

Rob Rothman

You can of course control any number of leds by chaining as many register as you need.
When you need to control less than 8, just leave the excess pins unused (any value: 0 or 1)

With a shiftregister, u'll need a resistor pr. led. If you instead use a 'led-driver', u'll need just one resistor to control current to each led. Other advantage is : Voltage supply to the leds (using a led-driver-chip) can be anything from 5V to 15V

one example http://www.ti.com/lit/ds/symlink/tlc5917.pdf

Thanks.

I understand that I need to leave a pin unconnected. However, I assume that my code has to be consistent with which pin is left unconnected, and I don't understand how to make that happen.

Specifically, after generating a random number, I would pass it to a function something like the following:

int figurePips(int diescore)
{
  int pipsLocation[] = {B00001000, B01000001, B00101010, B01100011, B01101011, B0111111}; /* These represent the physical locations of the pips on each side of a standard die.  Note that although there are only six possible numbers, there are seven possible locations for each pip. */
  return pipsLocation[diescore];
}

This function would return an 8-digit binary number, of which seven of the digits represent the pips to be turned on for the particular number. In this snippet, I have used the seven rightmost digits (i.e. the leftmost digit is set to zero regardless of the number rolled).

Then, I would pass this eight-digit binary number to a function something like the following:

void showTheRoll(int pipsOn) 
{
   digitalWrite (latchPin, LOW);
   shiftOut (dataPin, clockPin, MSBFIRST, pipsOn);
   digitalWrite (latchPin, HIGH);
 }

What I don't understand is the relationship between (1) which pin I leave unconnected, (2) whether I leave the leftmost or rightmost digit always set to zero, and (3) whether the shiftOut call should be MSBFirst of LSBFirst.

Am I missing something totally basic?

Rob Rothman

A far as I understand the most significant bit, bit 7, is set to zero in your example. It wouldn't blink a light if a led was attached to outputpin 7 when using MSBFIRST-mode.
Personally I would simply connect 8 leds to experiment / see what happens though.

A dice could be treated as a 4 segment object btw. With the 8 outputpins of a shiftregister it is possible to throw 2 dices if you use 6 pins to each light 2 leds and 2 pins to each drive 1 led..

23
414
3
2

67
858
7
6
(*= no led used)

A typical shift register like the HC595 has output pins. (0 to 7)
Data is clocked in a bit at a time.
Each clock shifts (moves) all the bits up the new bit is clocked into the zero position
and the previous bit 7 data inside the shift register is lost. (or is in the carry position)

Here is a nice video that may also be helpful that goes into great detail:

The shiftOut() function clocks in 8 bits.
While you can specify the MSB vs LSB bit order of how the bits are clocked/handed to the SR,
if you want the data on the 7 output pins to match bit for bit the bits of your 8 bit data,
you will want to shift the data MSBFIRST.
This will clock in bit 7, then bit 6, etc.. down to bit 0.
Since each clock shifts/moves the previous data up, each bit is moved up until
the last bit (bit 0) is clocked in.
At that point the 8 bits will appear on the 8 output pins exactly the same as the
original 8 bit data byte.

How you store the data in your 8 bit bytes is up to you.
i.e. if you only need 7 bits, you can choose to use the lower 7 bits of the upper 7 bits
of the 8 bit byte.

--- bill

Thanks. I think I now understand enough to try it out.

Rob Rothman