1 | //**************************************************************//
|
2 | // Name : shiftIn Example 2.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 |
|
10 | //define where your pins are
|
11 | int latchPin = 8;
|
12 | int dataPin = 9;
|
13 | int clockPin = 7;
|
14 |
|
15 | //Define variables to hold the data
|
16 | //for each shift register.
|
17 | //starting with non-zero numbers can help
|
18 | //troubleshoot
|
19 | byte switchVar1 = 72; //01001000
|
20 | byte switchVar2 = 159; //10011111
|
21 |
|
22 | char Taste[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
|
23 |
|
24 | void setup() {
|
25 | //start serial
|
26 | Serial.begin(9600);
|
27 |
|
28 | //define pin modes
|
29 | pinMode(latchPin, OUTPUT);
|
30 | pinMode(clockPin, OUTPUT);
|
31 | pinMode(dataPin, INPUT);
|
32 |
|
33 | }
|
34 |
|
35 | void loop() {
|
36 |
|
37 | //Pulse the latch pin:
|
38 | //set it to 1 to collect parallel data
|
39 | digitalWrite(latchPin,1);
|
40 | //set it to 1 to collect parallel data, wait
|
41 | delayMicroseconds(20);
|
42 | //set it to 0 to transmit data serially
|
43 | digitalWrite(latchPin,0);
|
44 |
|
45 | //while the shift register is in serial mode
|
46 | //collect each shift register into a byte
|
47 | //the register attached to the chip comes in first
|
48 | switchVar1 = shiftIn(dataPin, clockPin);
|
49 | switchVar2 = shiftIn(dataPin, clockPin);
|
50 |
|
51 | //Print out the results.
|
52 | //leading 0's at the top of the byte
|
53 | //(7, 6, 5, etc) will be dropped before
|
54 | //the first pin that has a high input
|
55 | //reading
|
56 |
|
57 | for (int n=0; n<=7; n++)
|
58 | {
|
59 | //so, when n is 3, it compares the bits
|
60 | //in switchVar1 and the binary number 00001000
|
61 | //which will only return true if there is a
|
62 | //1 in that bit (ie that pin) from the shift
|
63 | //register.
|
64 | if (switchVar1 & (1 << n) ){
|
65 | //print the value of the array location
|
66 | Serial.println(Taste[n]);
|
67 | }
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | //white space
|
75 | Serial.println("-------------------");
|
76 | //delay so all these print satements can keep up.
|
77 | delay(500);
|
78 | }
|
79 | }
|
80 |
|
81 | //------------------------------------------------end main loop
|
82 |
|
83 | ////// ----------------------------------------shiftIn function
|
84 | ///// just needs the location of the data pin and the clock pin
|
85 | ///// it returns a byte with each bit in the byte corresponding
|
86 | ///// to a pin on the shift register. leftBit 7 = Pin 7 / Bit 0= Pin 0
|
87 |
|
88 | byte shiftIn(int myDataPin, int myClockPin) {
|
89 | int i;
|
90 | int temp = 0;
|
91 | int pinState;
|
92 | byte myDataIn = 0;
|
93 |
|
94 | pinMode(myClockPin, OUTPUT);
|
95 | pinMode(myDataPin, INPUT);
|
96 |
|
97 | //we will be holding the clock pin high 8 times (0,..,7) at the
|
98 | //end of each time through the for loop
|
99 |
|
100 | //at the begining of each loop when we set the clock low, it will
|
101 | //be doing the necessary low to high drop to cause the shift
|
102 | //register's DataPin to change state based on the value
|
103 | //of the next bit in its serial information flow.
|
104 | //The register transmits the information about the pins from pin 7 to pin 0
|
105 | //so that is why our function counts down
|
106 | for (i=7; i>=0; i--)
|
107 | {
|
108 | digitalWrite(myClockPin, 0);
|
109 | delayMicroseconds(2);
|
110 | temp = digitalRead(myDataPin);
|
111 | if (temp) {
|
112 | pinState = 1;
|
113 | //set the bit to 0 no matter what
|
114 | myDataIn = myDataIn | (1 << i);
|
115 | }
|
116 | else {
|
117 | //turn it off -- only necessary for debuging
|
118 | //print statement since myDataIn starts as 0
|
119 | pinState = 0;
|
120 | }
|
121 |
|
122 | //Debuging print statements
|
123 | //Serial.print(pinState);
|
124 | //Serial.print(" ");
|
125 | //Serial.println (dataIn, BIN);
|
126 |
|
127 | digitalWrite(myClockPin, 1);
|
128 |
|
129 | }
|
130 | //debuging print statements whitespace
|
131 | //Serial.println();
|
132 | //Serial.println(myDataIn, BIN);
|
133 | return myDataIn;
|
134 | }
|