You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.4 KiB

4 years ago
  1. //**************************************************************//
  2. // Name : shiftIn Example 1.1 //
  3. // Author : Carlyn Maw //
  4. // Date : 25 Jan, 2007 //
  5. // Version : 1.0 //
  6. // Notes : Code for using a CD4021B Shift Register //
  7. // : //
  8. //****************************************************************
  9. //This tut from arduino
  10. // doesn't work.
  11. /* IO Definitions ProCAT Flash*/
  12. //#define DATA_IN 4
  13. //#define CLK 5
  14. //#define LATCH_EN 6
  15. //define where your pins are
  16. int dataPin = 4;
  17. int clockPin = 5;
  18. int latchPin = 6;
  19. //Define variables to hold the data
  20. //for shift register.
  21. //starting with a non-zero numbers can help
  22. //troubleshoot
  23. byte switchVar1 = 72; //01001000
  24. void setup() {
  25. //start serial
  26. Serial.begin(9600);
  27. //define pin modes
  28. pinMode(latchPin, OUTPUT);
  29. pinMode(clockPin, OUTPUT);
  30. pinMode(dataPin, INPUT);
  31. }
  32. void loop() {
  33. //Pulse the latch pin:
  34. //set it to 1 to collect parallel data
  35. digitalWrite(latchPin,1);
  36. //set it to 1 to collect parallel data, wait
  37. delayMicroseconds(20);
  38. //set it to 0 to transmit data serially
  39. digitalWrite(latchPin,0);
  40. //while the shift register is in serial mode
  41. //collect each shift register into a byte
  42. //the register attached to the chip comes in first
  43. switchVar1 = shiftIn(dataPin, clockPin);
  44. //Print out the results.
  45. //leading 0's at the top of the byte
  46. //(7, 6, 5, etc) will be dropped before
  47. //the first pin that has a high input
  48. //reading
  49. Serial.println(switchVar1, BIN);
  50. //white space
  51. Serial.println("-------------------");
  52. //delay so all these print satements can keep up.
  53. delay(500);
  54. }
  55. //------------------------------------------------end main loop
  56. ////// ----------------------------------------shiftIn function
  57. ///// just needs the location of the data pin and the clock pin
  58. ///// it returns a byte with each bit in the byte corresponding
  59. ///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0
  60. byte shiftIn(int myDataPin, int myClockPin) {
  61. int i;
  62. double temp = 0;
  63. int pinState;
  64. byte myDataIn = 0;
  65. pinMode(myClockPin, OUTPUT);
  66. pinMode(myDataPin, INPUT);
  67. //we will be holding the clock pin high 8 times (0,..,7) at the
  68. //end of each time through the for loop
  69. //at the begining of each loop when we set the clock low, it will
  70. //be doing the necessary low to high drop to cause the shift
  71. //register's DataPin to change state based on the value
  72. //of the next bit in its serial information flow.
  73. //The register transmits the information about the pins from pin 7 to pin 0
  74. //so that is why our function counts down
  75. for (i=7; i>=0; i--)
  76. {
  77. digitalWrite(myClockPin, 0);
  78. delayMicroseconds(0.2);
  79. temp = digitalRead(myDataPin);
  80. if (temp) {
  81. pinState = 1;
  82. //set the bit to 0 no matter what
  83. myDataIn = myDataIn | (1 << i);
  84. }
  85. else {
  86. //turn it off -- only necessary for debuging
  87. //print statement since myDataIn starts as 0
  88. pinState = 0;
  89. }
  90. //Debuging print statements
  91. //Serial.print(pinState);
  92. //Serial.print(" ");
  93. //Serial.println (dataIn, BIN);
  94. digitalWrite(myClockPin, 1);
  95. }
  96. //debuging print statements whitespace
  97. Serial.println();
  98. Serial.println(myDataIn, BIN);
  99. return myDataIn;
  100. }