100% found this document useful (1 vote)
589 views3 pages

Arduino SN74HC165N Shift Register Guide

This document describes code for reading the state of digital pins connected to daisy-chained SN74HC165N 8-bit parallel-in/serial-out shift registers using an Arduino. It defines constants for the number of shift registers and pin connections. A read_shift_regs function reads the bit values from the serial output pin and represents the state in an unsigned integer. The pin values are displayed and checked for changes each loop iteration.

Uploaded by

Andy Zhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
589 views3 pages

Arduino SN74HC165N Shift Register Guide

This document describes code for reading the state of digital pins connected to daisy-chained SN74HC165N 8-bit parallel-in/serial-out shift registers using an Arduino. It defines constants for the number of shift registers and pin connections. A read_shift_regs function reads the bit values from the serial output pin and represents the state in an unsigned integer. The pin values are displayed and checked for changes each loop iteration.

Uploaded by

Andy Zhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 /*

2 * SN74HC165N_shift_reg
3 *
4 * Program to shift in the bit values from a SN74HC165N 8-bit
5 * parallel-in/serial-out shift register.
6 *
7 * This sketch demonstrates reading in 16 digital states from a
8 * pair of daisy-chained SN74HC165N shift registers while using
9 * only 4 digital pins on the Arduino.
10 *
11 * You can daisy-chain these chips by connecting the serial-out
12 * (Q7 pin) on one shift register to the serial-in (Ds pin) of
13 * the other.
14 *
15 * Of course you can daisy chain as many as you like while still
16 * using only 4 Arduino pins (though you would have to process
17 * them 4 at a time into separate unsigned long variables).
18 *
19 */
20
21 /* How many shift register chips are daisy-chained.
22 */
23 #define NUMBER_OF_SHIFT_CHIPS 2
24
25 /* Width of data (how many ext lines).
26 */
27 #define DATA_WIDTH NUMBER_OF_SHIFT_CHIPS * 8
28
29 /* Width of pulse to trigger the shift register to read and latch.
30 */
31 #define PULSE_WIDTH_USEC 5
32
33 /* Optional delay between shift register reads.
34 */
35 #define POLL_DELAY_MSEC 1
36
37 /* You will need to change the "int" to "long" If the
38 * NUMBER_OF_SHIFT_CHIPS is higher than 2.
39 */
40 #define BYTES_VAL_T unsigned int
41
42 int ploadPin = 8; // Connects to Parallel load pin the 165
43 int clockEnablePin = 9; // Connects to Clock Enable pin the 165
44 int dataPin = 11; // Connects to the Q7 pin the 165
45 int clockPin = 12; // Connects to the Clock pin the 165
46
47 BYTES_VAL_T pinValues;
48 BYTES_VAL_T oldPinValues;
49
50 /* This function is essentially a "shift-in" routine reading the
51 * serial Data from the shift register chips and representing
52 * the state of those pins in an unsigned integer (or long).
53 */
54 BYTES_VAL_T read_shift_regs()
55 {
56 long bitVal;
57 BYTES_VAL_T bytesVal = 0;
58
59 /* Trigger a parallel Load to latch the state of the data lines,
60 */
61 digitalWrite(clockEnablePin, HIGH);
62 digitalWrite(ploadPin, LOW);
63 delayMicroseconds(PULSE_WIDTH_USEC);
64 digitalWrite(ploadPin, HIGH);
65 digitalWrite(clockEnablePin, LOW);
66
67 /* Loop to read each bit value from the serial out line
68 * of the SN74HC165N.
69 */
70 for(int i = 0; i < DATA_WIDTH; i++)
71 {
72 bitVal = digitalRead(dataPin);
73
74 /* Set the corresponding bit in bytesVal.
75 */
76 bytesVal |= (bitVal << ((DATA_WIDTH-1) - i));
77
78 /* Pulse the Clock (rising edge shifts the next bit).
79 */
80 digitalWrite(clockPin, HIGH);
81 delayMicroseconds(PULSE_WIDTH_USEC);
82 digitalWrite(clockPin, LOW);
83 }
84
85 return(bytesVal);
86 }
87
88 /* Dump the list of zones along with their current status.
89 */
90 void display_pin_values()
91 {
92 [Link]("Pin States:\r\n");
93
94 for(int i = 0; i < DATA_WIDTH; i++)
95 {
96 [Link](" Pin-");
97 [Link](i);
98 [Link](": ");
99
100 if((pinValues >> i) & 1)
101 [Link]("HIGH");
102 else
103 [Link]("LOW");
104
105 [Link]("\r\n");
106 }
107
108 [Link]("\r\n");
109 }
110
111 void setup()
112 {
113 [Link](9600);
114
115 /* Initialize our digital pins...
116 */
117 pinMode(ploadPin, OUTPUT);
118 pinMode(clockEnablePin, OUTPUT);
119 pinMode(clockPin, OUTPUT);
120 pinMode(dataPin, INPUT);
121
122 digitalWrite(clockPin, LOW);
123 digitalWrite(ploadPin, HIGH);
124
125 /* Read in and display the pin states at startup.
126 */
127 pinValues = read_shift_regs();
128 display_pin_values();
129 oldPinValues = pinValues;
130 }
131
132 void loop()
133 {
134 /* Read the state of all zones.
135 */
136 pinValues = read_shift_regs();
137
138 /* If there was a chage in state, display which ones changed.
139 */
140 if(pinValues != oldPinValues)
141 {
142 [Link]("*Pin value change detected*\r\n");
143 display_pin_values();
144 oldPinValues = pinValues;
145 }
146
147 delay(POLL_DELAY_MSEC);
148
149
150 #include <FastLED.h>
151 #define LED_PIN 3
152 #define NUM_LEDS 8
153
154 int dataPin = 3;
155 int clockPin = 4;
156 int load = 5;
157 byte data;
158
159 CRGB leds[NUM_LEDS];
160
161 void setup() {
162 [Link]<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
163 [Link]<WS2812, 6, GRB>(leds, NUM_LEDS);
164
165 pinMode(dataPin,INPUT);
166 pinMode(clockPin,OUTPUT);
167 pinMode(load,OUTPUT);
168 [Link](9600);
169 digitalWrite(load, HIGH);
170 }
171
172
173
174 void loop() {
175
176 for (int i = 0; i <= 7; i++) {
177 // put your main code here, to run repeatedly:
178 digitalWrite(clockPin, HIGH);
179 digitalWrite(load, LOW); //pulling load pin low to load the data on the data inputs
180 delay(10);
181 digitalWrite(load, HIGH); //pulling it back to high
182 delay(10);
183 data = shiftIn(dataPin, clockPin, MSBFIRST); // shifting and loading data in data
184 [Link](data); // serial printing the data
185 delay(10);
186 }
187
188 for (int i = 0; i <= 7; i++) {
189 if (bitRead(data,i)){
190 leds[i] = CRGB ( 0, 255, 0);
191 FastLED[1].showLeds();
192 }
193 else {
194 leds[i] = CRGB ( 0, 0, 0);
195 FastLED[1].showLeds();
196 }
197
198 delay(40);
199 }
200

You might also like