|
//Tut from sparkfun, edited for multiple shift ins. This one works.
|
|
//see 74165 data sheets for more details on how it works
|
|
|
|
/* IO Definitions ProCAT Flash*/
|
|
|
|
//#define DATA_IN 4
|
|
//#define CLK 5
|
|
//#define LATCH_EN 6
|
|
|
|
// 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 data_pin = 4; // Connect Pin 11 to SER_OUT (serial data out)
|
|
const int clk_pin = 5; // Connect Pin 12 to CLK (the clock that times the shifting)
|
|
const int shld_pin = 6; // Connect Pin 8 to SH/!LD (shift or active low load)
|
|
|
|
const int ce_pin = 9; // Connect Pin 9 to !CE (clock enable, active low)
|
|
|
|
byte incoming; // Variable to store the 8 values loaded from the shift register
|
|
byte incoming2; // Variable to store the 8 values loaded from the shift register
|
|
byte incoming3; // Variable to store the 8 values loaded from the shift register
|
|
|
|
byte results1;
|
|
byte results2;
|
|
byte results3;
|
|
|
|
// 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);
|
|
|
|
// 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_shift_regs(); // Read the shift register, it likes that
|
|
|
|
|
|
|
|
// Print out the values being read from the shift register
|
|
Serial.println("\nThe incoming values of the shift register are: ");
|
|
Serial.print("ABCDEFGH : ");
|
|
print_byte(results1); // Print every 1 and 0 that correlates with A through H
|
|
//Serial.println(incoming,BIN); // This way works too but leaves out the leading zeros
|
|
|
|
Serial.print("IJKLMNOP : ");
|
|
print_byte(results2);
|
|
Serial.print("QRSTUVWX : ");
|
|
print_byte(results3);
|
|
|
|
//Serial.println(incoming,BIN); // This way works too but leaves out the leading zeros
|
|
|
|
|
|
|
|
delay(2000); // Wait for some arbitrary amount of time
|
|
|
|
}
|
|
|
|
// This code is intended to trigger the shift register to grab values from it's A-H inputs
|
|
byte read_shift_regs()
|
|
{
|
|
byte the_shifted = 0; // An 8 bit number to carry each bit value of A-H
|
|
|
|
// Trigger loading the state of the A-H data lines into the shift register
|
|
digitalWrite(shld_pin, LOW);
|
|
delayMicroseconds(5); // Requires a delay here according to the datasheet timing diagram
|
|
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);
|
|
digitalWrite(ce_pin, LOW); // Enable the clock
|
|
|
|
// Get the A-H values
|
|
results1 = shiftIn(data_pin, clk_pin, MSBFIRST);
|
|
results2 = shiftIn(data_pin, clk_pin, MSBFIRST);
|
|
results3 = shiftIn(data_pin, clk_pin, MSBFIRST);
|
|
|
|
digitalWrite(ce_pin, HIGH); // Disable the clock
|
|
|
|
//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++)
|
|
{
|
|
Serial.print(val >> i & 1, BIN); // Magic bit shift, if you care look up the <<, >>, and & operators
|
|
}
|
|
Serial.print("\n"); // Go to the next line, do not collect $200
|
|
}
|