This code is for a basic LED chaser using a 74HC595 Shift Register. We use our shiftOut function for people curious about how it works, but you can use the one
included with the Arduino library if you prefer. So the shiftOut function goes like this:
// Shifts 8 bits out MSB first, on rising clock edge
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
if ( myDataOut & (1 << i) ) {
pinState= 1;
}
else {
pinState= 0;
}
digitalWrite(myDataPin, pinState); //write the bit
digitalWrite(myClockPin, 1); //shift bits rising clock pin
digitalWrite(myDataPin, 0);//0 data to prevent bleed through
}
//stop shifting
digitalWrite(myClockPin, 0);
}
The heart of this function is the for loop which iterates over all the bits in the byte. Each bit is checked to see if it is a 1 or 0 in the line:
if ( myDataOut & (1 << i) )
Here, we are using the
bit-wise AND (&) operator, as well as the
left shift operator( << ) to single out the ith bit.
Now, the code to display the LED chaser call this shiftOut function inside a for loop, each time outputting another power of 2 (e.g. 1,2,4,8,16). These numbers in binary have a single one and all the other bits are 0. So from the shift register's perspective one LED is on and all the others are off. Here is the code:
void loop() {
for (int i = 0; i < 8; i++) {
// latchPin low so LEDs don't change
// while you are sending in bits:
byte curb = 1 << i;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, curb);
digitalWrite(latchPin, HIGH);
delay(500);
}
}
Notice that again, we use the left shift bit-wise operator ( << ) , here we use it to get consecutive powers of two (this is more efficient than multiplying). Lastly, make sure to set the pin modes on all the shift register pins to output.
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
Alright now grab the file and get your knight rider on!
/* LucidTronix Shift Register
* LED chaser w/ 74HC595
* Tutorial at:
* http://www.lucidtronix.com/tutorials/2
*/
int latchPin = 1; //Pin connected to ST_CP of 74HC595
int clockPin = 2; //Pin connected to SH_CP of 74HC595
int dataPin = 0; //Pin connected to DS of 74HC595
void setup() {
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
}
void loop() {
for (int i = 0; i < 8; i++) {
// latchPin low so LEDs don't change
// while you are sending in bits:
byte curb = 1 << i;
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, curb);
digitalWrite(latchPin, HIGH);
delay(500);
}
}
// Shifts 8 bits out MSB first, on rising clock edge
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
digitalWrite(myDataPin, pinState); //write the bit
digitalWrite(myClockPin, 1); //shift bits rising clock pin
digitalWrite(myDataPin, 0);//0 data to prevent bleed through
}
//stop shifting
digitalWrite(myClockPin, 0);
}
Comments: