Hello everyone!
I am trying to control a 74HC595 shift register using an Arduino. When my code didn't work I tried the standard example which uses the built-in function (ShiftOut()) and confirmed that the IC does work and all the breadboard connections and pins are wired correctly, so the question remains, what is wrong with my code? And possibly how does the built-in ShiftOut() actually work, and how does it differ from what I wrote?
//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 4;
////Pin connected to DS of 74HC595
int dataPin = 10;
void setup() {
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
digitalWrite(latchPin, LOW);
digitalWrite(dataPin, LOW);
digitalWrite(clockPin, LOW);
Serial.begin(115200);
bool bit = false;
Serial.println("Before for loop");
for (int i = 0; i <8; i++)
{
// Write bit to dataPin
if (bit) digitalWrite(dataPin, HIGH);
else digitalWrite(dataPin, LOW);
// Take clock pin HIGH
digitalWrite(clockPin, HIGH);
// Wait until 74HC595 writes the bit
delay(20);
// Take clock pin LOW
digitalWrite(clockPin, LOW);
bit != bit;
// Debug outputs to serial
Serial.println("For loop");
Serial.println(i);
}
Serial.println("After for loop");
// Take latch pin HIGH
digitalWrite(latchPin, HIGH);
delay(20);
digitalWrite(latchPin, LOW);
}
void loop() {
}
What it does is that writes one bit a at a time on the dataPin (SER in my datasheet), then takes the clockPin (SRCLK in my datasheet) high for 20ms and switches it low again, and after 8 bits switches the latchPin (RCLK in my datasheet) high for 20ms and the low again in order to transfer the 8 written bits from the shift register to the storage register. The Output Enable pin (OE inverted in the datasheet) is wired permanently LOW so that outputs are always enabled. So is the SRCLR inverted input (shift register clear) wired always LOW. The only connections are the three pins written in the code and which, when used with ShiftOut() work.
What am I doing wrong here? Can someone give me a hand?