Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 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. static int latchPin = 8;
  12. //Pin connected to SH_CP of 74HC595
  13. static int clockPin = 12;
  14. ////Pin connected to DS of 74HC595
  15. static int dataPin = 11;
  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] = 11111111; //11111111
  31. dataArrayRED[1] = 11111111; //11111110
  32. dataArrayRED[2] = 00000000; //11111100
  33. dataArrayRED[3] = 11111111; //11111000
  34. dataArrayRED[4] = 00000000; //11110000
  35. dataArrayRED[5] = 11111111; //11100000
  36. dataArrayRED[6] = 00000000; //11000000
  37. dataArrayRED[7] = 11111111; //10000000
  38. dataArrayRED[8] = 00000000; //00000000
  39. dataArrayRED[9] = 11111111; //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] = 11111111; //11111111
  44. dataArrayGREEN[1] = 0xFF; //01111111
  45. dataArrayGREEN[2] = 0xFF; //00111111
  46. dataArrayGREEN[3] = 0xFF; //00011111
  47. dataArrayGREEN[4] = 0xFF; //00001111
  48. dataArrayGREEN[5] = 0xFF; //00000111
  49. dataArrayGREEN[6] = 0xFF; //00000011
  50. dataArrayGREEN[7] = 0xFF; //00000001
  51. dataArrayGREEN[8] = 0xFF; //00000000
  52. dataArrayGREEN[9] = 0xFF; //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. //load the light sequence you want from array
  60. dataRED = B00000010;
  61. dataGREEN = B00001001;
  62. //ground latchPin and hold low for as long as you are transmitting
  63. digitalWrite(latchPin, 0);
  64. //move 'em out
  65. shiftOut(dataPin, clockPin, B11110000);
  66. shiftOut(dataPin, clockPin, B00010010);
  67. shiftOut(dataPin, clockPin, B01001001);
  68. shiftOut(dataPin, clockPin, B00100100);
  69. //return the latch pin high to signal chip that it
  70. //no longer needs to listen for information
  71. digitalWrite(latchPin, 1);
  72. digitalWrite(latchPin, 0);
  73.  
  74. delay (2000);
  75. shiftOut(dataPin, clockPin, B11110000);
  76. shiftOut(dataPin, clockPin, B00011011);
  77. shiftOut(dataPin, clockPin, B01101101);
  78. shiftOut(dataPin, clockPin, B10110110);
  79. //return the latch pin high to signal chip that it
  80. //no longer needs to listen for information
  81. digitalWrite(latchPin, 1);
  82. digitalWrite(latchPin, 0);
  83.  
  84. delay (2000);
  85. shiftOut(dataPin, clockPin, B11110000);
  86. shiftOut(dataPin, clockPin, B00001001);
  87. shiftOut(dataPin, clockPin, B00100100);
  88. shiftOut(dataPin, clockPin, B10010010);
  89. //return the latch pin high to signal chip that it
  90. //no longer needs to listen for information
  91. digitalWrite(latchPin, 1);
  92. digitalWrite(latchPin, 0);
  93.  
  94. }
  95.  
  96. void loop() {
  97. }
  98.  
  99.  
  100.  
  101. // the heart of the program MSB first
  102. void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
  103. int i=0;
  104. int pinState;
  105. pinMode(myClockPin, OUTPUT);
  106. pinMode(myDataPin, OUTPUT);
  107. digitalWrite(myDataPin, 0);
  108. digitalWrite(myClockPin, 0);
  109.  
  110. for (i=7; i>=0; i--) {
  111. digitalWrite(myClockPin, 0);
  112. if ( myDataOut & (1<<i) ) {
  113. pinState= 1;
  114. }
  115. else {
  116. pinState= 0;
  117. }
  118.  
  119. //Sets the pin to HIGH or LOW depending on pinState
  120. digitalWrite(myDataPin, pinState);
  121. //register shifts bits on upstroke of clock pin
  122. digitalWrite(myClockPin, 1);
  123. //zero the data pin after shift to prevent bleed through
  124. digitalWrite(myDataPin, 0);
  125. }
  126.  
  127. //stop shifting
  128. digitalWrite(myClockPin, 0);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement