2

I am able to control one shift register with this shift function:

void shift (int pin) {
  shiftOut(ds,shcp,MSBFIRST,pin);  
  digitalWrite(stcp,HIGH);        
  delay(100);
  digitalWrite(stcp,LOW);         
}

but when I move to 2 shift registers it does not add up.. how do I need to adapt this method to make it work with 2 shift registers when I am going from 8 leds to 16 leds?

My second register now just copies the first register.. I want the second register to continue instead of repeat

4 Answers 4

5

It sounds like the primary issue is how you've connected the two shift registers. You just need the bit shifting out of the first one to shift into the second one so they act, together, as one longer shift register.

Two serial shift registers

The example pictured is described in full here, under the heading "Example 2": http://arduino.cc/en/tutorial/ShiftOut

As Klaus mentioned, you want to first ShiftOut the byte you intend to wind up in the second resister, then ShiftOut the byte for the first register. This will push your first byte through to the second register.

1

split the data into 2 bytes, and then send those, one after the other

void shift (int pin) {
  byte data1 = 0;//shift register 1
  byte data2 = 0;//shift register 2
  if( pin<8 )
    data1 = (1<<pin);
  else
    data2 = (1<<(pin-8));
  shiftOut(ds,shcp,MSBFIRST,data2);//note that you send the data for shift register 2 first
  shiftOut(ds,shcp,MSBFIRST,data1);
  digitalWrite(stcp,HIGH);        
  delay(100);//why wait this long?
  digitalWrite(stcp,LOW);         
}
2
  • I think the OP's pin parameter was actually the raw binary pattern being sent to the shift register (rather than the number of the pin to be brought high). Dec 16, 2014 at 17:28
  • 1
    In that case you'd use, byte data1=lowByte(pin) byte data2=highByte(pin).
    – Gerben
    Dec 16, 2014 at 20:51
0
  • Configure SPI
  • Hook up your shift registers
    • common SS
    • common CLK
    • QH* of the first shift register to SER of the next

In order to transfer the bytes

  • Pull SS low
  • ShiftOut first byte
  • ShiftOut next byte
  • Pull SS high again to finish the SPI transfer


UPDATE

This is untested, just out of memory!

#include<SPI.h>
// note that the pins for CLK and MOSI are defined here

unsigned int ledPattern = 0b1111000011110000;
// store the state for 16 LEDs here

void setup(void)
{
    pinMode(SS, OUTPUT);
    digitalWrite(SS, HIGH);
    SPI.setDataMode(3);
    SPI.setClockDivider(4);  
    SPI.setBitOrder(MSBFIRST);

    // let's transfer 2 bytes
    digitalWrite(SS, LOW);
    SPI.transfer(ledPattern & 0xff00;) // high byte
    SPI.transfer(ledPattern & 0x00ff;) // low byte
    digitalWrite(SS, HIGH);
}


UPDATE II

You are by no means limited to 2 shift registers! In 2013, we did a 8x11 LED display for an upcycling art project. Eleven 74HC595 were chained to display some patterns. Each 'pixel' consisted of 3 12V SMD LEDs on a stripe. Consequently, each shift register was connected to an ULN2803.

I just uploaded a video from the construction phase, showing a test pattern.

2
  • could you give me an example how to transfer bytes?
    – letsjak
    Dec 15, 2014 at 18:57
  • @letsjak Done, but Gerben was faster :D Dec 15, 2014 at 19:20
0
void shift(int pin) {
  digitalWrite(stcp, LOW);

  // shift the bits out:
  shiftOut(ds, shcp, MSBFIRST, pin >> 8);
  shiftOut(ds, shcp, MSBFIRST, pin);
  // turn on the output so the LEDs can light up:
  digitalWrite(stcp, HIGH);

  delay(100);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.