Advertisement
bitwise_gamgee

Untitled

Jul 7th, 2023
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | Help | 0 0
  1. /*
  2. Materials:
  3.  
  4.     Arduino board
  5.     Two 74HC595 Shift Registers
  6.     16 LEDs
  7.     16 220-Ohm resistors
  8.     Breadboard
  9.     Jumper wires
  10.  
  11. Connection:
  12.  
  13. Connect the data pin (DS), latch pin (STCP), and clock pin (SHCP) of both shift registers in parallel to the Arduino (pins 8, 12, and 11 respectively).
  14. Connect the serial output (Q7S) of the first shift register to the data input (DS) of the second shift register.
  15. Connect each output pin of both shift registers to a different LED via a 220-Ohm resistor.
  16.  
  17. Purpose:
  18.  
  19. We're looping through each bit of two bytes. The first shiftOut() function call sends a byte with a single '1' bit to the first register. The second shiftOut() call sends a byte of all '0's to the second register (only on the first loop, when j == 0). This allows us to light up each LED in sequence from 1 to 16. Remember, this is a very simple example and there are many more ways to use daisy-chained shift registers!
  20. */
  21.  
  22. //Pin connected to ST_CP (12) of 74HC595
  23. int latchPin = 8;
  24. //Pin connected to SH_CP (11) of 74HC595
  25. int clockPin = 12;
  26. ////Pin connected to DS (14) of 74HC595
  27. int dataPin = 11;
  28.  
  29. void setup() {
  30.   pinMode(latchPin, OUTPUT);
  31.   pinMode(clockPin, OUTPUT);  
  32.   pinMode(dataPin, OUTPUT);
  33. }
  34.  
  35. void loop() {
  36.   for (int j = 0; j < 2; j++) { // two bytes for two shift registers
  37.     for (int i = 0; i < 8; i++) { // loop through 8 bits
  38.       digitalWrite(latchPin, LOW);
  39.       shiftOut(dataPin, clockPin, LSBFIRST, 1 << i); // shift out bit
  40.       if (j == 0) shiftOut(dataPin, clockPin, LSBFIRST, 0); // fill second register with zeros
  41.       digitalWrite(latchPin, HIGH); // update output
  42.       delay(250);
  43.     }
  44.   }
  45. }
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement