// The 74HC595 uses a type of serial connection called SPI
// (Serial Peripheral Interface) that requires three pins:
int datapin = 5;
int clockpin = 6;
int latchpin = 7;
// We'll also declare a global variable for the data we're
// sending to the shift register:
byte data = 0;
//Pins connected to 74HC165
const int dataPinIn = 2; /* Q7 */
const int clockPinIn = 3; /* CP */
const int latchPinIn = 4; /* PL */
const int numBits = 8; /* Set to 8 * number of shift registers */
void setup() {
Serial.begin(115200);
//74HC595 Set the three SPI pins to be outputs:
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
//74HC165
pinMode(dataPinIn, INPUT);
pinMode(clockPinIn, OUTPUT);
pinMode(latchPinIn, OUTPUT);
}
void shiftWrite(int desiredPin, boolean desiredState){
// This function lets you make the shift register outputs
// HIGH or LOW in exactly the same way that you use digitalWrite().
bitWrite(data,desiredPin,desiredState); //Change desired bit to 0 or 1 in "data"
// Now we'll actually send that data to the shift register.
// The shiftOut() function does all the hard work of
// manipulating the data and clock pins to move the data
// into the shift register:
shiftOut(datapin, clockpin, MSBFIRST, data); //Send "data" to the shift register
//Toggle the latchPin to make "data" appear at the outputs
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
}
byte shiftRead(uint8_t i){
//set the shift register in the Sampling state
//reads the inputs from pins D0…D7 and stores them
digitalWrite(latchPinIn, LOW);
digitalWrite(latchPinIn , HIGH);
digitalWrite(clockPinIn,HIGH);
byte incoming = shiftIn(dataPinIn, clockPinIn, MSBFIRST);
return bitRead(incoming, i);
}
void loop() {
//turn on each led in loop
for(int i=0; i< 8; i++){
//turn on the led i (595 OUTPUT)
data = 0; //reset data
shiftWrite(i, 1);
for(int j=0; j<8; j++){
Serial.print(shiftRead(j));
}
Serial.println();
delay(500);
}
}