74HC166N 8-bit parallel-in/serial-out shift register

used products:

1. 74HC166N
2. 8 Position On/Off DIP Switch
3. Arduino UNO
4. HD74LS166AP

Arduino:

#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); #define PIN_PL 8 #define PIN_CP 9 #define PIN_Q7 10 int j; int value; byte switchVar = 0; void setup() { lcd.begin(16, 2); Serial.begin(9600); pinMode(PIN_PL, OUTPUT); pinMode(PIN_CP, OUTPUT); pinMode(PIN_Q7, INPUT); } void loop() { byte dataIn = 0; digitalWrite(PIN_PL, 0); digitalWrite(PIN_CP, 0); digitalWrite(PIN_CP, 1); digitalWrite(PIN_PL, 1); for(j = 0; j < 8; j++) { value = digitalRead(PIN_Q7); if (value) { dataIn = dataIn | (1 << j); } digitalWrite(PIN_CP, LOW); digitalWrite(PIN_CP, HIGH); } if (switchVar != dataIn) { switchVar = dataIn; Serial.print("dataIn: "); Serial.print(dataIn, BIN); Serial.println(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("DEC: "+String(dataIn)); lcd.setCursor(0, 1); lcd.print("BIN: "+String(dataIn, BIN)); } }

C:

#include <avr/io.h> #include <util/delay.h> #include <Arduino.h> #define PIN_PL PB0 #define PIN_CP PB1 #define PIN_Q7 PB2 int j; int main(void) { Serial.begin(9600); DDRB |= (1<<PIN_PL); DDRB |= (1<<PIN_CP); DDRB &= ~(1<<PIN_Q7); while (1) { PORTB &= ~(1<<PIN_PL); PORTB &= ~(1<<PIN_CP); PORTB |= (1<<PIN_CP); PORTB |= (1<<PIN_PL); byte dataIn = 0; for (j = 0; j < 8; j++) { if (PINB & (1<<PIN_Q7)) { dataIn = dataIn | (1 << j); } PORTB &= ~(1<<PIN_CP); PORTB |= (1<<PIN_CP); } Serial.print("dataIn: ");Serial.print(dataIn, BIN); Serial.println(); _delay_ms(50); } return 0; }