16 Bit Binary Count using 2 x 74HC595 Chips

Hey Guys,

I've not had my Arduino long and was having trouble getting the data I wanted to a 2nd 74HC595 Shift Register. After a little experimentation and using the http://www.arduino.cc/en/Tutorial/ShiftOut as a guide, same wiring except I stuck to pins 2,3 & 4. My sketch is below, I hope someone finds it useful :slight_smile:

////
// Name : Shift16BitCount, Dual 74HC595 16 Bit Binary Count //
// Author : Mick Stone //
// Date : 9 Feb, 2014 //
// Version : 1.0 //
// Notes : Code for using 2X 74HC595 Shift Registers //
// : to count from 0 to 65536 in binary //
//
//

//Pin connected to ST_CP of 74HC595
int latchPin = 3;
//Pin connected to SH_CP of 74HC595
int clockPin = 4;
////Pin connected to DS of 74HC595
int dataPin = 2;

void setup() {
pinMode(dataPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}

void loop() {
for (int currentValue = 0; currentValue < 65536; currentValue++) {
//Mask counter with 1111111100000000 to select high order byte and divide by 256 to scale to a 8 bit output
int ByteHigh = (currentValue & 65280)/256;
//Mask counter with 0000000011111111 to select low order byte
int ByteLow = currentValue & 255;

// Disable the latch while we clock in data
digitalWrite(latchPin, LOW);

// Send the value as a binary sequence to the module
shiftOut(dataPin, clockPin, MSBFIRST, ByteHigh);
shiftOut(dataPin, clockPin, MSBFIRST, ByteLow);

// Enable the latch again to set the output states
digitalWrite(latchPin, HIGH);

delay(200);
}
}

1 Like

Hi Mick,
I came across your post while trying to figure out how to daisy chain 4 Serial Registers.
I started to build the circuit thinking this shouldn't be too hard. I created an 8 x 32 matrix of LED's
(See my question posted here) Multiple Shift Register 74HC595 Daisy Chained Together - Programming Questions - Arduino Forum

As a starting point I'm trying to scale up your code as an example for how to control a single row of 32 led's.
I'm confused how the values get passed to the second and subsequent shift register chips.

Can you give me a hint on how I would go about doing this?

I think I'd have to add two more lines like the ones below but I'd only be guessing what to make them.

Thanks
David

    int ByteReallyHigh = (currentValue & ???????)/256;
    int ByteVeryHigh = (currentValue & ?????)/256;
    
    //Mask counter with 1111111100000000 to select high order byte and divide by 256 to scale to a 8 bit output
    int ByteHigh = (currentValue & 65280)/256;
    //Mask counter with 0000000011111111 to select low order byte
    int ByteLow = currentValue & 255;

    // Disable the latch while we clock in data
    digitalWrite(latchPin, LOW);
    
    // Send the value as a binary sequence to the module
    shiftOut(dataPin, clockPin, MSBFIRST, ByteReallyHigh);
    shiftOut(dataPin, clockPin, MSBFIRST, ByteVeryHigh);
    shiftOut(dataPin, clockPin, MSBFIRST, ByteHigh);
    shiftOut(dataPin, clockPin, MSBFIRST, ByteLow);

Wow! I have been pulling my hair for this for hours. Thanks man!