britneybeatey

Shift Register Final

Jun 23rd, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. //**************************************************************//
  2. // Name : shiftOutCode, Predefined Dual Array Style //
  3. // Author : Carlyn Maw, Tom Igoe //
  4. // Date : 25 Oct, 2006 //
  5. // Version : 1.0 //
  6. // Notes : Code for using a 74HC595 Shift Register //
  7. // : to count from 0 to 255 //
  8. //****************************************************************
  9.  
  10. //Pin connected to ST_CP of 74HC595
  11. int latchPin = 8;
  12. //Pin connected to SH_CP of 74HC595
  13. int clockPin = 9;
  14. ////Pin connected to DS of 74HC595
  15. int dataPin = 10;
  16.  
  17. //holders for infromation you're going to pass to shifting function
  18. byte dataRED;
  19. byte dataGREEN;
  20. byte dataArrayRED[10];
  21. byte dataArrayGREEN[10];
  22.  
  23. void setup() {
  24. //set pins to output because they are addressed in the main loop
  25. pinMode(latchPin, OUTPUT);
  26. Serial.begin(9600);
  27.  
  28. //Arduino doesn't seem to have a way to write binary straight into the code
  29. //so these values are in HEX. Decimal would have been fine, too.
  30. dataArrayRED[0] = 0xFF; //11111111
  31. dataArrayRED[1] = 0xFE; //11111110
  32. dataArrayRED[2] = 0xFC; //11111100
  33. dataArrayRED[3] = 0xF8; //11111000
  34. dataArrayRED[4] = 0xF0; //11110000
  35. dataArrayRED[5] = 0xE0; //11100000
  36. dataArrayRED[6] = 0xC0; //11000000
  37. dataArrayRED[7] = 0x80; //10000000
  38. dataArrayRED[8] = 0x00; //00000000
  39. dataArrayRED[9] = 0xE0; //11100000
  40.  
  41. //Arduino doesn't seem to have a way to write binary straight into the code
  42. //so these values are in HEX. Decimal would have been fine, too.
  43. dataArrayGREEN[0] = 0xFF; //11111111
  44. dataArrayGREEN[1] = 0x7F; //01111111
  45. dataArrayGREEN[2] = 0x3F; //00111111
  46. dataArrayGREEN[3] = 0x1F; //00011111
  47. dataArrayGREEN[4] = 0x0F; //00001111
  48. dataArrayGREEN[5] = 0x07; //00000111
  49. dataArrayGREEN[6] = 0x03; //00000011
  50. dataArrayGREEN[7] = 0x01; //00000001
  51. dataArrayGREEN[8] = 0x00; //00000000
  52. dataArrayGREEN[9] = 0x07; //00000111
  53.  
  54. //function that blinks all the LEDs
  55. //gets passed the number of blinks and the pause time
  56. blinkAll_2Bytes(2,500);
  57. }
  58.  
  59. void loop() {
  60.  
  61.  
  62. for (int j = 0; j < 10; j++) {
  63. //load the light sequence you want from array
  64. dataRED = dataArrayRED[j];
  65. dataGREEN = dataArrayGREEN[j];
  66. //ground latchPin and hold low for as long as you are transmitting
  67. digitalWrite(latchPin, 0);
  68. //move 'em out
  69. shiftOut(dataPin, clockPin, dataGREEN);
  70. shiftOut(dataPin, clockPin, dataRED);
  71. //return the latch pin high to signal chip that it
  72. //no longer needs to listen for information
  73. digitalWrite(latchPin, 1);
  74. delay(300);
  75. }
  76. }
  77.  
  78.  
  79.  
  80. // the heart of the program
  81. void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  82. // This shifts 8 bits out MSB first,
  83. //on the rising edge of the clock,
  84. //clock idles low
  85.  
  86. //internal function setup
  87. int i=0;
  88. int pinState;
  89. pinMode(myClockPin, OUTPUT);
  90. pinMode(myDataPin, OUTPUT);
  91.  
  92. //clear everything out just in case to
  93. //prepare shift register for bit shifting
  94. digitalWrite(myDataPin, 0);
  95. digitalWrite(myClockPin, 0);
  96.  
  97. //for each bit in the byte myDataOut�
  98. //NOTICE THAT WE ARE COUNTING DOWN in our for loop
  99. //This means that %00000001 or "1" will go through such
  100. //that it will be pin Q0 that lights.
  101. for (i=7; i>=0; i--) {
  102. digitalWrite(myClockPin, 0);
  103.  
  104. //if the value passed to myDataOut and a bitmask result
  105. // true then... so if we are at i=6 and our value is
  106. // %11010100 it would the code compares it to %01000000
  107. // and proceeds to set pinState to 1.
  108. if ( myDataOut & (1<<i) ) {
  109. pinState= 1;
  110. }
  111. else {
  112. pinState= 0;
  113. }
  114.  
  115. //Sets the pin to HIGH or LOW depending on pinState
  116. digitalWrite(myDataPin, pinState);
  117. //register shifts bits on upstroke of clock pin
  118. digitalWrite(myClockPin, 1);
  119. //zero the data pin after shift to prevent bleed through
  120. digitalWrite(myDataPin, 0);
  121. }
  122.  
  123. //stop shifting
  124. digitalWrite(myClockPin, 0);
  125. }
  126.  
  127.  
  128. //blinks the whole register based on the number of times you want to
  129. //blink "n" and the pause between them "d"
  130. //starts with a moment of darkness to make sure the first blink
  131. //has its full visual effect.
  132. void blinkAll_2Bytes(int n, int d) {
  133. digitalWrite(latchPin, 0);
  134. shiftOut(dataPin, clockPin, 0);
  135. shiftOut(dataPin, clockPin, 0);
  136. digitalWrite(latchPin, 1);
  137. delay(200);
  138. for (int x = 0; x < n; x++) {
  139. digitalWrite(latchPin, 0);
  140. shiftOut(dataPin, clockPin, 255);
  141. shiftOut(dataPin, clockPin, 255);
  142. digitalWrite(latchPin, 1);
  143. delay(d);
  144. digitalWrite(latchPin, 0);
  145. shiftOut(dataPin, clockPin, 0);
  146. shiftOut(dataPin, clockPin, 0);
  147. digitalWrite(latchPin, 1);
  148. delay(d);
  149. }
  150. }
Add Comment
Please, Sign In to add comment