Using shiftOut

May I please request some help using shiftOut() with the TPIC6B595 shift register?

My goal is to simply light the LEDs, but the code that I am using does not work. (Once this is working I intend to scale up with more shift registers and LEDs)

Board is a Mega 2560.

Schematic is attached along with the pin layout of the TPIC6B595.

const int latchPin = 53; // SS RCK
const int clockPin = 52; // SCK SRCK
const int dataPin = 51; // MOSI SERIN

byte Data = 0;

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

void loop()
{
  shiftWrite (0,1);
  shiftWrite (1,1);
  shiftWrite (2,1);
  shiftWrite (3,1);
  delay(1000);
  shiftWrite (0,0);
  shiftWrite (1,0);
  shiftWrite (2,0);
  shiftWrite (3,0);
  delay(1000);
}

void shiftWrite(int Pin, boolean State)
{
  bitWrite (Data, Pin, State);
  digitalWrite (latchPin, LOW);
  shiftOut (dataPin, clockPin, MSBFIRST, Data);
  digitalWrite (latchPin, HIGH);
}

shiftOut Writes the 8 bits of “Data” to the shift register. There’s therefore no need for...

bitWrite (Data, Pin, State);

Or 4 copies of...

 shiftWrite (0,1);
  shiftWrite (1,1);
  shiftWrite (2,1);
  shiftWrite (3,1);

To write the lower 4 bits all ON use...

shiftOut (dataPin, clockPin, MSBFIRST, 0x0F);

And to write the lower 4 bits all OFF use...

shiftOut (dataPin, clockPin, MSBFIRST, 0x00);

Note these also shift out the upper 4 bits (I left them as all zeros).

 const int latchPin = 53; // SS RCK
const int clockPin = 52; // SCK SRCK
const int dataPin = 51; // MOSI SERIN

byte Data = 0;

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

void loop()
{
  bitWrite (Data,0,1);
  bitWrite (Data,1,1);
  bitWrite (Data,2,1);
  bitWrite (Data,3,1);
  shiftWrite();
  delay(1000);
  bitWrite (Data,0,0);
  bitWrite (Data,1,0);
  bitWrite (Data,2,0);
  bitWrite (Data,3,0);
  shiftWrite();
  delay(1000);
}

void shiftWrite()
{
  digitalWrite (latchPin, LOW);
  shiftOut (dataPin, clockPin, MSBFIRST, Data);
  digitalWrite (latchPin, HIGH);
}

Thank you for taking the time to reply, and posting the revised code along with the detailed explanation.

I've uploaded this sketch to my board, but the LEDs still won't light.

|439x500

|431x500

Show us a good image of your ‘actual’ wiring.

Posting images: https://forum.arduino.cc/index.php?topic=519037.0

From the timing diagrams like it latches on a falling edge of RCK (latch pin) as well.

 void shiftWrite()
{
  shiftOut (dataPin, clockPin, MSBFIRST, Data);
  digitalWrite (latchPin, HIGH);
  digitalWrite (latchPin, LOW);
}

Otherwise I’d probably be looking at a wiring issue.

It works.

I had neglected to link the 4 power bus quadrants, leaving my circuit un-powered.

Completely user error.

All good now then?
I still think you need to look at your latch edge, as the code earlier look like it would be latching at the start of the next shift as opposed to the end of the current shift. That means the data appearing at your output will be delayed until the next write.

Yes, thank you so much!

I revised shiftWrite as you suggested, ending with latch on LOW. Running nicely on my board.

“ It works.”

You are an expert now !