/*
74HC165 Shift register input example
https://wokwi.com/arduino/projects/306031380875182657
(C) 2021, Uri Shaked
*/
const int dataPin = 2; /* Q7 */
const int clockPin = 3; /* CP */
const int latchPin = 4; /* PL */
const int numBits = 8; /* Set to 8 * number of shift registers */
void setup() {
Serial.begin(115200);
pinMode(dataPin, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
}
void loop() {
byte sda=0b00000010;
if(sda== read165())
{
Serial.print("xiangdeng");
Serial.println();
}
delay(1000);
}
//74HC165读取函数,将165中的Q0-Q7存储成一个字节,低位在前高位在后1000 0000代表Q0触发
// byte read165()
// {
// byte byteValue = 0; // 用于存储转换后的字节值
// // Step 1: Sample
// digitalWrite(latchPin, LOW);
// digitalWrite(latchPin, HIGH);
// // Step 2: Shift
// Serial.print("Bits: ");
// for (int i = 0; i < numBits; i++) {
// int bit = digitalRead(dataPin);
// if (bit == HIGH) {
// byteValue |= (1 << i);
// } else {
// byteValue |= (0 << i);
// }
// digitalWrite(clockPin, HIGH); // Shift out the next bit
// digitalWrite(clockPin, LOW);
// }
// Serial.print(byteValue,BIN);
// Serial.println();
// return byteValue;
// }