Shift Registers help you multiply your inputs and outputs.
// Shifts 8 bits out MSB first, on rising clock edgevoid 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 74HC595int clockPin = 2; //Pin connected to SH_CP of 74HC595int dataPin = 0; //Pin connected to DS of 74HC595void 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 edgevoid 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<
| Title | Description | # | Cost | Link | Picture |
| 74HC595 PCB | This PCB maps the 74HC595 shift register chip to an LED bar. | 1 | $1.5 | Link |
|
| Shift Register | IC SHIFT REGISTER 8BIT 16-SOP Tri-State Serial to ParallelValue:74HC | 1 | $0.65 | Link |
|
| Shift Register | IC 8-BIT SHIFT REGISTER 16-DIP Serial to Parallel Tri-State 2 V ~ 6 VValue:74HC | 1 | $0.63 | Link |
|
| Shift Register CD4021 16-DIP | Parallel-in Serial-Out CD4021 Shift Register from Texas Instruments. Through-hole DIP package.Value:Parallel to Serial | 1 | $0.52 | Link |
|
| Shift Register | IC STATIC SHFT REG 8STG 16-TSSOP Parallel or Serial to Serial 3 V ~ 18 VValue: Parallel to Serial | 1 | $0.45 | Link |
|
Comments: