// Pin definitions
const int DS0 = 8; // Serial Input Right
const int DS7 = 9; // Serial Input Left
const int S0 = 3; // Mode Select 0
const int S1 = 4; // Mode Select 1
const int OE1 = 5; // Output Enable 1
const int OE2 = 6; // Output Enable 2
const int CP = 2; // Clock Pulse
const int MR = 7; // master reset
const int io[] = {10,11,12,13,A5,A4,A3,A2}; // IO Pins 0 - 7
void setup() {
pinMode(DS0, OUTPUT);
pinMode(DS7, OUTPUT);
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(OE1, OUTPUT);
pinMode(OE2, OUTPUT);
pinMode(CP, OUTPUT);
pinMode(MR, OUTPUT);
for(int i = 0;i < 8;i++) {
pinMode(io[i], OUTPUT);
}
// OE1 + OE2 = Active LOW (if either is high = no output) HIGH = HI-Z
digitalWrite(OE1, LOW);
digitalWrite(OE2, LOW);
// Setting to Mode 0: Hold data (do nothing)
digitalWrite(S0, LOW);
digitalWrite(S1, LOW);
// MR is ACTIVE LOW, set HIGH to disable MR
digitalWrite(MR, HIGH);
// CP needs to start at LOW
digitalWrite(CP, LOW);
Serial.begin(9600);
}
void loop() {
clockPulse();
parallelLoad(0b00000000);
holdMode();
delay(1000);
parallelLoad(0b11111111);
holdMode();
delay(1000);
parallelLoad(0b00000000);
holdMode();
delay(1000);
shiftRight(1);
delay(1000);
shiftRight(0);
delay(1000);
shiftRight(1);
delay(1000);
shiftRight(0);
delay(1000);
shiftLeft(1);
delay(1000);
shiftLeft(0);
delay(1000);
shiftLeft(1);
delay(1000);
masterReset();
delay(1000);
}
void holdMode() {
setMode(LOW, LOW);
for(int x = 0; x < 8;x++) {
pinMode(io[x], INPUT);
}
clockPulse();
}
void masterReset() {
digitalWrite(MR,LOW);
digitalWrite(MR,HIGH);
}
void shiftRight(bool x) {
digitalWrite(DS0,x);
setMode(LOW, HIGH);
clockPulse();
}
void shiftLeft(bool x) {
digitalWrite(DS7,x);
setMode(HIGH, LOW);
clockPulse();
}
void parallelLoad(byte data) {
setMode(HIGH, HIGH);
for(int x = 0; x < 8; x++) {
pinMode(io[x], OUTPUT);
digitalWrite(io[x],bitRead(data,x));
}
clockPulse();
}
void setMode(bool s0, bool s1) {
digitalWrite(S0, s0);
digitalWrite(S1, s1);
}
void clockPulse() {
digitalWrite(CP, HIGH);
delayMicroseconds(1);
digitalWrite(CP, LOW);
delayMicroseconds(1);
}