74HC595: Getting code for 4 daisy chained 74HC595 to work


I try to getting my code for 4 daisy chained 74HC595 to work. The attached functions is for two 74HC595. I know that I need to shiftout 4 times, but what else do I need to change in the function? How can I split the whichPin integer into 4 bytes.


void registerWrite(int whichPin, int whichState) {
  // the bits you want to send. Use an unsigned int,
  // so you can use all 16 bits:
  unsigned int bitsToSend = 0;    

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // break the bits into two bytes, one for 
  // the first register and one for the second:
  byte registerOne = highByte(bitsToSend);
  byte registerTwo = lowByte(bitsToSend);

  // shift the bytes out:
  shiftOut(dataPin, clockPin, MSBFIRST, registerTwo);
  shiftOut(dataPin, clockPin, MSBFIRST, registerOne);

  // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);

Read this:


There is a little library at the bottom.

You can also use SPI: How to drive 595 shift registers with AVR hardware SPI | Jumptuck

Thanks :slight_smile: