74HC597B1 8-bit shift register with input flip-flops

Used Products:

1. 74HC597B1
2. 8 Position On/Off DIP Switch
3. Arduino Mega 2560

Content:

Çeşme: http://easymcu.ru/publ/12-1-0-201

Code:

#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); #define doPulse(pin) digitalWrite(pin, HIGH); delayMicroseconds(10); digitalWrite(pin, LOW); #define shcpPin 47 // clock pin. shift register clock input (LOW-to-HIGH, edge-triggered) #define plPin 49 // parallel load input (active LOW) #define stcpPin 51 // latch #define qPin 53 // serial data output byte switchVar = 0; void setup() { lcd.begin(16, 2); pinMode(qPin, INPUT); pinMode(stcpPin, OUTPUT); pinMode(plPin, OUTPUT); pinMode(shcpPin, OUTPUT); digitalWrite(shcpPin, LOW); digitalWrite(plPin, LOW); digitalWrite(stcpPin, LOW); Serial.begin(9600); } void loop() { byte q = getData(); if (switchVar != q) { switchVar = q; Serial.print("dataIn: "); Serial.print(q, BIN); Serial.println(); lcd.clear(); lcd.setCursor(0, 0); lcd.print("DEC: "+String(q)); lcd.setCursor(0, 1); lcd.print("BIN: "+String(q, BIN)); } delay(100); } byte getData() { byte i; byte pinBit; uint16_t accumulator = 0; doPulse (stcpPin); digitalWrite(plPin, HIGH); for(i=0; i<8; i++) { pinBit = digitalRead(qPin); accumulator |= (pinBit << (8-1-i)); doPulse(shcpPin); } digitalWrite(plPin, LOW); digitalWrite(stcpPin, HIGH); return accumulator; }