// 74HC165 Pins
const int PL = 7; // Parallel load pin (active LOW)
const int CP = 8; // Clock pin
const int QH = 9; // Serial out
// 74HC595 Pins
const int DS = 6; // Serial in
const int SH_CP = 5; // Shift register clock
const int ST_CP = 4; // Storage register clock (latch)
const int numChips = 2; // Number of 165s and 595s
void setup() {
// Set up pins
pinMode(PL, OUTPUT);
pinMode(CP, OUTPUT);
pinMode(QH, INPUT);
pinMode(DS, OUTPUT);
pinMode(SH_CP, OUTPUT);
pinMode(ST_CP, OUTPUT);
// Start serial communication
Serial.begin(9600);
Serial.println("System initialized.");
}
void loop() {
byte inputBytes[numChips];
// Load parallel inputs from 74HC165
digitalWrite(PL, LOW);
delayMicroseconds(5);
digitalWrite(PL, HIGH);
// Read bytes from 74HC165
Serial.print("Read from 74HC165: ");
for (int i = 0; i < numChips; i++) {
inputBytes[i] = shiftIn(QH, CP, MSBFIRST);
Serial.print("0b");
for (int bit = 7; bit >= 0; bit--) {
Serial.print((inputBytes[i] >> bit) & 1);
}
Serial.print(" ");
}
Serial.println();
/*
Serial.print("Read from 74HC165: ");
for (int i = 0; i < numChips; i++) {
inputBytes[i] = shiftIn(QH, CP, MSBFIRST);
Serial.print("0b");
Serial.print(inputBytes[i], BIN);
Serial.print(" ");
}
Serial.println();*/
// Send to 74HC595
digitalWrite(ST_CP, LOW);
Serial.print("Writing to 74HC595: ");
for (int i = numChips - 1; i >= 0; i--) {
shiftOut(DS, SH_CP, MSBFIRST, inputBytes[i]);
Serial.print("0b");
Serial.print(inputBytes[i], BIN);
Serial.print(" ");
}
digitalWrite(ST_CP, HIGH);
Serial.println();
Serial.println("----");
delay(1000); // Adjustable delay for debugging
}