Advertisement
w0lfiesmith

Arduino Shift Register tutorial for MakeUseOf.com

Jan 3rd, 2012
1,655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.66 KB | None | 0 0
  1. /*     ---------------------------------------------------------
  2.  *     |  Shift Register Tutorial, based on                    |
  3.  *     |  Arduino Experimentation Kit CIRC-05                  |
  4.  *     |          .: 8 More LEDs :. (74HC595 Shift Register)   |
  5.  *     ---------------------------------------------------------
  6.  *     |  Modified by James @ MakeUseOf.com                    |
  7.  *     ---------------------------------------------------------
  8.  */
  9.  
  10. //Pin Definitions
  11. // 7HC595N has three pins
  12. int data = 2; // where we send the bits to control outputs
  13. int clock = 3; // keeps the data in sync
  14. int latch = 4; // tells the shift register when to activate the output sequence
  15.  
  16. void setup()
  17. {
  18.    // set the three control pins to output
  19.   pinMode(data, OUTPUT);
  20.   pinMode(clock, OUTPUT);  
  21.   pinMode(latch, OUTPUT);  
  22.  
  23.   Serial.begin(9600); // so we can send debug messages to serial monitor
  24. }
  25.  
  26. void loop(){
  27.    
  28.     outputBytes(); // our basic output which writes 8-bits to show how a shift register works.
  29.     //outputIntegers(); // sends an integer value as data instead of bytes, effectively counting in binary.
  30. }
  31.  
  32. void outputIntegers(){
  33.      for (int i=0;i<256;i++){
  34.         digitalWrite(latch, LOW);    
  35.         Serial.println(i);  // Debug, sending output to the serial monitor
  36.         shiftOut(data, clock, MSBFIRST, i);
  37.         digitalWrite(latch, HIGH);  
  38.         delay(100);    
  39.      }
  40. }
  41.  
  42. void outputBytes(){
  43.     /* Bytes, or 8-bits, are represented by a B followed by 8 0 or 1s.
  44.         In this instance, consider this to be like an array that we'll use to control
  45.         the 8 LEDs. Here I've started the byte value as 00000001
  46.     */    
  47.        
  48.     byte dataValues = B00000001; // change this to adjust the starting pattern
  49.    
  50.     /* In the for loop, we begin by pulling the latch low,
  51.         using the shiftOut Arduino function to talk to the shift register,
  52.         sending it our byte of dataValues representing the state of the LEDs
  53.         then pull the latch high to lock those into place.
  54.        
  55.         Finally, we shift the bits one place to the left, meaning the next iteration
  56.         will turn on the next LED in the series.
  57.        
  58.         To see the exact binary value being sent, check the serial monitor.
  59.     */
  60.    
  61.     for (int i=0;i<8;i++){
  62.       digitalWrite(latch, LOW);    
  63.       Serial.println(dataValues, BIN);  // Debug, sending output to the serial monitor
  64.       shiftOut(data, clock, MSBFIRST, dataValues);
  65.       digitalWrite(latch, HIGH);  
  66.       dataValues = dataValues << 1; // Shift the bits one place to the left -  change to >> to adjust direction
  67.       delay(100);    
  68.     }
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement