// HARDWARE CONNECTIONS
// Connect the following pins between your Arduino and the 74HC165 Breakout Board
// Connect pins A-H to 5V or GND or switches or whatever
const int clk_pin = 2; // Connect Pin 2 to CLK (the clock that times the shifting)
const int shld_pin = 3; // Connect Pin 3 to SH/!LD (shift or active low load)
const int data_pin = 4; // Connect Pin 4 to SER_OUT (serial data out)
const int ce_pin = 5; // Connect Pin 5 to !CE (clock enable, active low)
int clockpin = 6;
int latchpin = 7;
int datapin = 8;
// Variable to store the 8 values loaded from the shift register
byte incoming;
// sending to the shift register:
byte data = 0;
// The part that runs once
void setup() {
// Initialize serial to gain the power to obtain relevant information, 9600 baud
Serial.begin(9600);
// Initialize each digital pin to either output or input
// We are commanding the shift register with each pin with the exception of the serial
// data we get back on the data_pin line.
pinMode(shld_pin, OUTPUT);
pinMode(ce_pin, OUTPUT);
pinMode(clk_pin, OUTPUT);
pinMode(data_pin, INPUT);
// outputs 595
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
// Required initial states of these two pins according to the datasheet timing diagram
digitalWrite(clk_pin, HIGH);
digitalWrite(shld_pin, HIGH);
}
// The part that runs to infinity and beyond
void loop() {
// Read the shift register, it likes that
incoming = read_shift_regs();
// Print out the values being read from the shift register
Serial.print("ABCDEFGH : ");
// Print every 1 and 0 that correlates with A through H
print_byte(incoming);
// This way works too but leaves out the leading zeros
//Serial.println(incoming,BIN);
//
mirror165to595();
// All on, all off
//oneAfterAnother();
// Scroll down the line
//oneOnAtATime();
// Like above, but back and forth
//pingPong();
// Blink random LEDs
//randomLED();
//
//marquee();
// Bit patterns from 0 to 255
//binaryCount();
// Wait for some arbitrary amount of time
delay(2000);
}
// This code is intended to trigger the shift register to grab values from it's A-H inputs
byte read_shift_regs() {
// An 8 bit number to carry each bit value of A-H
byte the_shifted = 0;
// Trigger loading the state of the A-H data lines into the shift register
digitalWrite(shld_pin, LOW);
// Requires a delay here according to the datasheet timing diagram
delayMicroseconds(5);
digitalWrite(shld_pin, HIGH);
delayMicroseconds(5);
// Required initial states of these two pins according to the datasheet timing diagram
pinMode(clk_pin, OUTPUT);
pinMode(data_pin, INPUT);
digitalWrite(clk_pin, HIGH);
// Enable the clock
digitalWrite(ce_pin, LOW);
// Get the A-H values
the_shifted = shiftIn(data_pin, clk_pin, MSBFIRST);
// Disable the clock
digitalWrite(ce_pin, HIGH);
return the_shifted;
}
// A function that prints all the 1's and 0's of a byte, so 8 bits +or- 2
void print_byte(byte val) {
byte i;
for (byte i = 0; i <= 7; i++) {
// Magic bit shift, if you care look up the <<, >>, and & operators
Serial.print(val >> i & 1, BIN);
}
// Go to the next line, do not collect $200
Serial.print("\n");
}
void shiftWrite(int desiredPin, boolean desiredState) {
// This function lets you make the shift register outputs
// HIGH or LOW in exactly the same way that you use digitalWrite().
// Change desired bit to 0 or 1 in "data"
bitWrite(data, desiredPin, desiredState);
// Now we'll actually send that data to the shift register.
// The shiftOut() function does all the hard work of
// manipulating the data and clock pins to move the data
// into the shift register:
shiftOut(datapin, clockpin, MSBFIRST, data); //Send "data" to the shift register
// Toggle the latchPin to make "data" appear at the outputs
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
}
void oneAfterAnother() {
// This function will turn on all the LEDs, one-by-one,
// and then turn them off all off, one-by-one.
int index;
int delayTime = 100; // Time (milliseconds) to pause between LEDs
// Make this smaller for faster switching
// Turn all the LEDs on
for (index = 0; index <= 7; index++) {
shiftWrite(index, HIGH);
delay(delayTime);
}
// Turn all the LEDs off
for (index = 7; index >= 0; index--) {
shiftWrite(index, LOW);
delay(delayTime);
}
}
void oneOnAtATime() {
// This function will turn the LEDs on and off, one-by-one.
int index;
// Time (milliseconds) to pause between LEDs
int delayTime = 100;
// Make this smaller for faster switching step through the LEDs, from 0 to 7
for (index = 0; index <= 7; index++) {
// turn LED on
shiftWrite(index, HIGH);
// pause to slow down the sequence
delay(delayTime);
// turn LED off
shiftWrite(index, LOW);
}
}
void pingPong() {
// This function turns on the LEDs, one at a time, in both directions.
int index;
// time (milliseconds) to pause between LEDs
int delayTime = 100;
// make this smaller for faster switching
// step through the LEDs, from 0 to 7
for (index = 0; index <= 7; index++) {
// turn LED on
shiftWrite(index, HIGH);
// pause to slow down the sequence
delay(delayTime);
// turn LED off
shiftWrite(index, LOW);
}
// step through the LEDs, from 7 to 0
for (index = 7; index >= 0; index--) {
// turn LED on
shiftWrite(index, HIGH);
// pause to slow down the sequence
delay(delayTime);
// turn LED off
shiftWrite(index, LOW);
}
}
void randomLED() {
// This function will randomly turn on and off LEDs.
int index;
// time (milliseconds) to pause between LEDs
int delayTime = 100;
// make this smaller for faster switching
// pick a random number between 0 and 7
index = random(8);
// turn LED on
shiftWrite(index, HIGH);
// pause to slow down the sequence
delay(delayTime);
// turn LED off
shiftWrite(index, LOW);
}
void marquee() {
// This function will mimic "chase lights" like those around signs.
int index;
// Time (milliseconds) to pause between LEDs
int delayTime = 200;
// Make this smaller for faster switching
// Step through the first four LEDs
// (We'll light up one in the lower 4 and one in the upper 4)
for (index = 0; index <= 3; index++) {
// Turn a LED on
shiftWrite(index, HIGH);
// Skip four, and turn that LED on
shiftWrite(index + 4, HIGH);
// Pause to slow down the sequence
delay(delayTime);
// Turn both LEDs off
shiftWrite(index, LOW);
shiftWrite(index + 4, LOW);
}
}
void binaryCount() {
// This function creates a visual representation of the on/off pattern of bits in a byte.
// time (milliseconds) to pause between LEDs
int delayTime = 500;
// make this smaller for faster switching
// Send the data byte to the shift register:
shiftOut(datapin, clockpin, MSBFIRST, data);
// Toggle the latch pin to make the data appear at the outputs:
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
// Add one to data, and repeat!
// (Because a byte type can only store numbers from 0 to 255, if we add
// more than that, it will "roll around" back to 0 and start over)
data++;
// Delay so you can see what's going on:
delay(delayTime);
}
void mirror165to595() {
// Odczytaj dane z 74HC165
byte in = read_shift_regs();
// Zapisz je do zmiennej wyjściowej
data = in;
// Wyślij bajt do 74HC595
digitalWrite(latchpin, LOW);
shiftOut(datapin, clockpin, MSBFIRST, data);
digitalWrite(latchpin, HIGH);
}