Electronic – arduino – Sharing Clock and Data for 74HC595 and CD4021B Shift Registers

arduinoshift-registerspi

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?

Best Answer

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.