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
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.
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);
}
-
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).Peter Bloomfield– Peter Bloomfield2014年12月16日 17:28:11 +00:00Commented Dec 16, 2014 at 17:28 -
1In that case you'd use,
byte data1=lowByte(pin)
byte data2=highByte(pin)
.Gerben– Gerben2014年12月16日 20:51:38 +00:00Commented Dec 16, 2014 at 20:51
- Configure SPI
- Hook up your shift registers
- common
SS
- common
CLK
QH*
of the first shift register toSER
of the next
- common
In order to transfer the bytes
- Pull
SS
low ShiftOut
first byteShiftOut
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.
-
could you give me an example how to transfer bytes?letsjak– letsjak2014年12月15日 18:57:35 +00:00Commented Dec 15, 2014 at 18:57
-
@letsjak Done, but Gerben was faster :DKlaus-Dieter Warzecha– Klaus-Dieter Warzecha2014年12月15日 19:20:03 +00:00Commented Dec 15, 2014 at 19:20
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);
}