SN74LS165AN Parallel-Load 8-Bit Shift Registers

Used Products:

1. 8 Position On/Off DIP Switch
2. Arduino Mega 2560
3. LCD LiquidCrystal LCM1602C
4. SN74LS165AN

Content:

Çeşme: http://kludgerus.blogspot.com.tr/2012/01/arduino-dip-switches-and-binary.html

Code:

#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); #define DEBUGPIN 42 // From http://wardyprojects.blogspot.com/2011/05/74hc165-piso-shift-register-arduino.html //The terminology used here (PL, CP, Q7 and so on) are taken DIRECTLY from the 74HC165's datasheet. #define PIN_PL (53) //pin 13 on arduino goes to pin 1 of 74HC165 #define PIN_CP (52) //pin 12 on arduino goes to pin 2 of 74HC165 #define PIN_Q7 (51) //pin 11 on arduino goes to pin 9 of 74HC165 void setup() { Serial . begin(9600); digitalWrite(DEBUGPIN, LOW); lcd.begin(16, 2); //Hardwire pin 15 of both 74HC165 chips to ground to enable them all the time. //For peace of mind I also hardwired pin 10 of the upstream chip to //GND to force zeroes to be shifted in instead of allowing it to float. //Given that the arduino is "downstream". pinMode(PIN_PL, OUTPUT); pinMode(PIN_CP, OUTPUT); pinMode(PIN_Q7, INPUT); digitalWrite(PIN_PL, HIGH); digitalWrite(PIN_CP, LOW); } // End setup() void loop() { read_dip(); } // End loop() int DEBUG() { return(digitalRead(DEBUGPIN)); } // End DEBUG() void read_dip() { int i; int j; int value; //byte dip; //load logic bits from the DIP switches digitalWrite(PIN_PL, LOW); //LOAD BITS //reset "load" line, this freezes the internal buffer on both chips digitalWrite(PIN_PL, HIGH); String t = ""; for(j = 0; j < 8; j++) { value = digitalRead(PIN_Q7); //read next "switch" digitalWrite(PIN_CP, HIGH); digitalWrite(PIN_CP, LOW); t += String(value); //print switch's value if (DEBUG()) Serial.println(value); } lcd.clear(); lcd.setCursor(0, 0); lcd.print("SN74LS165AN"); lcd.setCursor(0, 1); lcd.print("INPUT: "+t); if (DEBUG()) { Serial . println("t: "); Serial . println(t); Serial . println(""); //Serial . println("----------------"); } // Slight delay to let things settle down delay(500); }