/*
74HC165 Shift Register Demonstration 1
74hc165-demo.ino
Read from 8 switches and display values on serial monitor
DroneBot Workshop 2020
https://dronebotworkshop.com
*/
//3
// Define Connections to 74HC165
// PL pin 1
int load = 12;
// CE pin 15
int clockEnablePin = 4;
// Q7 pin 7
int dataIn = 27;
// CP pin 2
int clockIn = 14;
//74hc595
// Define pins for the shift register (change these to match your setup)
const int latchPin = 25; // connect to the RCLK pin (pin 12) of the first shift register
const int dataPin = 26; // connect to the SER pin (pin 14) of the first shift register
const int clockPin = 33; // connect to the SRCLK pin (pin 11) of the first shift register
// Number of cascaded shift registers
//const int numRegisters = 2;
int numRegisters = 3;
void setup()
{
// Setup Serial Monitor
Serial.begin(115200);
// Setup 74HC165 connections
pinMode(load, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockIn, OUTPUT);
pinMode(dataIn, INPUT);
pinMode (23, OUTPUT);
pinMode (21, OUTPUT);
// Set pins as outputs
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
}
void loop()
{
/* digitalWrite (21, LOW);
digitalWrite (23, HIGH);
// Write pulse to load pin
digitalWrite(load, LOW);
delayMicroseconds(5);
digitalWrite(load, HIGH);
delayMicroseconds(5);
// Get data from 74HC165
digitalWrite(clockIn, HIGH);
digitalWrite(clockEnablePin, LOW);
byte incoming = shiftIn(dataIn, clockIn, LSBFIRST);
digitalWrite(clockEnablePin, HIGH);
// Print to serial monitor
Serial.print("Pin States:\r\n");
Serial.println(incoming, BIN);
delay(2000);
digitalWrite (21, HIGH);
digitalWrite (23, LOW);
// Write pulse to load pin
digitalWrite(load, LOW);
delayMicroseconds(5);
digitalWrite(load, HIGH);
delayMicroseconds(5);
// Get data from 74HC165
digitalWrite(clockIn, HIGH);
digitalWrite(clockEnablePin, LOW);
incoming = shiftIn(dataIn, clockIn, LSBFIRST);
digitalWrite(clockEnablePin, HIGH);
// Print to serial monitor
Serial.print("Pin States:\r\n");
Serial.println(incoming, BIN);
delay(2000);/*
/*
// Example pattern to shift into the shift registers
byte ledsPattern = B10101010; // Adjust this pattern to control which LEDs are lit
// Write the data to the shift registers
digitalWrite(latchPin, LOW); // Start shifting data
for (int i = 0; i < numRegisters; i++) {
shiftOut(dataPin, clockPin, MSBFIRST, ledsPattern); // Shift data into the shift register
}
digitalWrite(latchPin, HIGH); // End shifting data
// Wait for a short period before updating the LEDs
delay(500);
*/
/* // Turn on the LED
writeRegister(0b00000001); // Binary for Q0
delay(500); // Wait for 500 milliseconds
// Turn off the LED
writeRegister(0b00000000); // All outputs off
delay(500); // Wait for 500 milliseconds
}
// Function to write a byte to the shift register
void writeRegister(byte data) {
// Send the data to the register
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, MSBFIRST, data);
digitalWrite(latchPin, HIGH);*/
/* // Write data to the registers
for (int i = 0; i < 256; i++) {
// Write the data to the registers
for (int reg = numRegisters - 1; reg >= 0; reg--) {
digitalWrite(latchPin, LOW);
// Send the data for each register
shiftOut(dataPin, clockPin, MSBFIRST, i >> (8 * reg));
digitalWrite(latchPin, HIGH);
}
// Delay for demonstration purposes
delay(500);*/
// Turn on LED at a specific position
int ledPosition = 18; // Choose which LED to light up (0-23 for 3 registers)
int registerNum = ledPosition / 8; // Calculate which register to use
int bitPosition = ledPosition % 8; // Calculate which bit to set in the register
// Write the data to the registers
for (int reg = numRegisters - 1; reg >= 0; reg--) {
digitalWrite(latchPin, LOW);
if (reg == registerNum) {
// Set the bit for the selected LED
shiftOut(dataPin, clockPin, MSBFIRST, 1 << bitPosition);
} else {
// Keep other bits low
shiftOut(dataPin, clockPin, MSBFIRST, 0);
}
digitalWrite(latchPin, HIGH);
}
// Delay for demonstration purposes
delay(500);
}