# Sharing Clock and Data for 74HC595 and CD4021B Shift Registers?

Can output shift registers (74HC595) and input shift registers (CD4021B) share the same clock and data lines on Arduino using SPI, with different latch pins "activating" either chain for input or output shifting?

I think this should work, but don't yet have all the parts I need to prototype it. I already have all that parts for an solution that does NOT work, and am trying to skip that "opps, this won't work" step. :-)

I am currently using SPI from an Arduino Pro Micro (5v) to drive a chain of 74HC595 shift registers for a multiplexed bank of 7 segment display boards. Each "Display Board" has two 74HC595's to drive a set of displays, and is connected via ribbon cable to the next (identical) board, which is the next display in a series of chained display boards. This is built and working great. I use a loop of the following to write out data to the 74HC595 shift registers.

SPI.transfer(myByteOut);


I would like to add an IR sensor (Vishay TSOP75356WTR) to each board. The Arduino would be able to detect if the IR sensor on each chained board is high or low.

My plan is to expand the existing display board setup by adding a parallel chain of input shift registers (CD4021B?) to shift back to the Arduino the IR sensor readings from each board. This would be done via a loop of

myByteIn = SPI.transfer(0);


Can one chain of 74HC595's, and one chain of CD4021B's share the same data and clock lines, using different "latch" pins to toggle between my input (CD4021B) reading and my output (74HC595) writing?

High speed is required, and Arduino only has one hardware based high speed pair, so I'd need to share the clock and data pins. Will that work? Anything else wrong with my plan?

• When you say parallel chain of input shift registers, I assume you mean you'd multiplex the data signal rather than attach the CD4021B to the end of the 74HC595 registers at the data output of the 595 registers? – Funkyguy Jul 23 '14 at 19:17

If they both can operate at a mutual SPI clock frequency, then I don't see a problem. SPI is a single-ended communication protocol so there shouldn't be a problem with attaching two wires to the same connection.

You will just have to make sure that you keep in mind what bits are currently stored in the registers at all times so that you can make sure you latch at the proper time.

As for the code, I'm not sure if the arduino environment will allow you to attach two devices to the same SPI object since it'll have its own latch attached to the object but let it be known that any data going out a SPI data line will go to both of the devices, you'll just have to latch them in an intelligent manner.

• That's what I think too, but have no experience to confirm. – starvingmind Jul 23 '14 at 19:26

If I may answer my own question. It should work. Here's how with arduino over SPI.

void setup() {
//Setup SPI
SPI.begin();
//Setup CD4021
pinMode(CD4021_PSC_PIN9, OUTPUT);
digitalWrite(CD4021_PSC_PIN9,HIGH);
//Setup 74HC595
pinMode(HC595_LATCH_PIN12, OUTPUT);
digitalWrite(HC595_LATCH_PIN12,HIGH);
}

void loop(){
//Shift out to 74HC595
digitalWrite(HC595_LATCH_PIN12,LOW);
SPI.transfer(BYTE_DATAOUT);
digitalWrite(HC595_LATCH_PIN12,HIGH);

//Shift in from CD4021B
digitalWrite(CD4021_PSC_PIN9,LOW);
BYTE_DATAIN = SPI.transfer(0x00);
digitalWrite(CD4021_PSC_PIN9,HIGH);
}


Learned from here, from someone who has done this exact implementation before.

• I can confirm this works. – starvingmind Jul 30 '14 at 14:47