Header Ads

Serial to Parallel Shifting-Out with a 74HC595

At some time or another you may run out of pins on your Arduino board and need to extend it with shift registers. This example is based on the 74HC595. The datasheet refers to the 74HC595 as an "8-bit serial-in, serial or parallel-out shift register with output latches; 3-state." In other words, you can use it to control 8 outputs at a time while only taking up a few pins on your microcontroller. You can link multiple registers together to extend your output even more. (Users may also wish to search for other driver chips with "595" or "596" in their part numbers, there are many. The STP16C596 for example will drive 16 LED's and eliminates the series resistors with built-in constant current sources.)

How this all works is through something called "synchronous serial communication," i.e. you can pulse one pin up and down thereby communicating a data byte to the register bit by bit. It's by pulsing second pin, the clock pin, that you delineate between bits. This is in contrast to using the "asynchronous serial communication" of the Serial.begin() function which relies on the sender and the receiver to be set independently to an agreed upon specified data rate. Once the whole byte is transmitted to the register the HIGH or LOW messages held in each bit get parceled out to each of the individual output pins. This is the "parallel output" part, having all the pins do what you want them to do all at once.

The "serial output" part of this component comes from its extra pin which can pass the serial information received from the microcontroller out again unchanged. This means you can transmit 16 bits in a row (2 bytes) and the first 8 will flow through the first register into the second register and be expressed there. You can learn to do that from the second example.

"3 states" refers to the fact that you can set the output pins as either high, low or "high impedance." Unlike the HIGH and LOW states, you can"t set pins to their high impedance state individually. You can only set the whole chip together. This is a pretty specialized thing to do -- Think of an LED array that might need to be controlled by completely different microcontrollers depending on a specific mode setting built into your project. Neither example takes advantage of this feature and you won"t usually need to worry about getting a chip that has it.

Here is a table explaining the pin-outs adapted from the Phillip's datasheet.

clip_image001

PINS 1-7, 15

Q0 - Q7

Output Pins

PIN 8

GND

Ground, Vss

PIN 9

Q7

Serial Out

PIN 10

MR

Master Reclear, active low

PIN 11

SH_CP

Shift register clock pin

PIN 12

ST_CP

Storage register clock pin (latch pin)

PIN 13

OE

Output enable, active low

PIN 14

DS

Serial data input

PIN 16

Vcc

Positive supply voltage

Example 1: One Shift Register

The first step is to extend your Arduino with one shift register.

The Circuit

1. Turning it on

Make the following connections:

  • GND (pin 8) to ground,
  • Vcc (pin 16) to 5V
  • OE (pin 13) to ground
  • MR (pin 10) to 5V

This set up makes all of the output pins active and addressable all the time. The one flaw of this set up is that you end up with the lights turning on to their last state or something arbitrary every time you first power up the circuit before the program starts to run. You can get around this by controlling the MR and OE pins from your Arduino board too, but this way will work and leave you with more open pins.

clip_image002

2. Connect to Arduino

  • DS (pin 14) to Ardunio DigitalPin 11 (blue wire)
  • SH_CP (pin 11) to to Ardunio DigitalPin 12 (yellow wire)
  • ST_CP (pin 12) to Ardunio DigitalPin 8 (green wire)

From now on those will be referred to as the dataPin, the clockPin and the latchPin respectively. Notice the 0.1"f capacitor on the latchPin, if you have some flicker when the latch pin pulses you can use a capacitor to even it out.

clip_image003

3. Add 8 LEDs.

In this case you should connect the cathode (short pin) of each LED to a common ground, and the anode (long pin) of each LED to its respective shift register output pin. Using the shift register to supply power like this is called sourcing current. Some shift registers can't source current, they can only do what is called sinking current. If you have one of those it means you will have to flip the direction of the LEDs, putting the anodes directly to power and the cathodes (ground pins) to the shift register outputs. You should check the your specific datasheet if you aren't using a 595 series chip. Don't forget to add a 470-ohm resistor in series to protect the LEDs from being overloaded.

clip_image004

Circuit Diagram

clip_image005

The Code

Here are three code examples. The first is just some "hello world" code that simply outputs a byte value from 0 to 255. The second program lights one LED at a time. The third cycles through an array.

  • 595 logic table: clip_image006
  • 595 timing diagram: clip_image006[1]

The code is based on two pieces of information in the datasheet: the timing diagram and the logic table. The logic table is what tells you that basically everything important happens on an up beat. When the clockPin goes from low to high, the shift register reads the state of the data pin. As the data gets shifted in it is saved in an internal memory register. When the latchPin goes from low to high the sent data gets moved from the shift registers aforementioned memory register into the output pins, lighting the LEDs.

Example 2

In this example you'll add a second shift register, doubling the number of output pins you have while still using the same number of pins from the Arduino.

The Circuit

1. Add a second shift register.

Starting from the previous example, you should put a second shift register on the board. It should have the same leads to power and ground.

clip_image007

2. Connect the 2 registers.

Two of these connections simply extend the same clock and latch signal from the Arduino to the second shift register (yellow and green wires). The blue wire is going from the serial out pin (pin 9) of the first shift register to the serial data input (pin 14) of the second register.

clip_image008

3. Add a second set of LEDs.

In this case I added green ones so when reading the code it is clear which byte is going to which set of LEDs

clip_image009

Circuit Diagram

clip_image010

The Code

Here again are three code samples. If you are curious, you might want to try the samples from the first example with this circuit set up just to see what happens.

Code Sample 2.1 Dual Binary Counters There is only one extra line of code compared to the first code sample from Example 1. It sends out a second byte. This forces the first shift register, the one directly attached to the Arduino, to pass the first byte sent through to the second register, lighting the green LEDs. The second byte will then show up on the red LEDs.

Code Sample 2.2 2 Byte One By One Comparing this code to the similar code from Example 1 you see that a little bit more has had to change. The blinkAll() function has been changed to the blinkAll_2Bytes() function to reflect the fact that now there are 16 LEDs to control. Also, in version 1 the pulsings of the latchPin were situated inside the subfunctions lightShiftPinA and lightShiftPinB(). Here they need to be moved back into the main loop to accommodate needing to run each subfunction twice in a row, once for the green LEDs and once for the red ones.

Code Sample 2.3 - Dual Defined Arrays Like sample 2.2, sample 2.3 also takes advantage of the new blinkAll_2bytes() function. 2.3's big difference from sample 1.3 is only that instead of just a single variable called "data" and a single array called "dataArray" you have to have a dataRED, a dataGREEN, dataArrayRED, dataArrayGREEN defined up front. This means that line

data = dataArray[j];

becomes

dataRED = dataArrayRED[j];dataGREEN = dataArrayGREEN[j];

and

shiftOut(dataPin, clockPin, data);

becomes

shiftOut(dataPin, clockPin, dataGREEN);shiftOut(dataPin, clockPin, dataRED);

Examples

ShftOut11

COPY

1//**************************************************************//

2// Name : shiftOutCode, Hello World

3// Author : Carlyn Maw,Tom Igoe, David A. Mellis

4// Date : 25 Oct, 2006

5// Modified: 23 Mar 2010

6// Version : 2.0

7// Notes : Code for using a 74HC595 Shift Register //

8// : to count from 0 to 255

9//****************************************************************

10//Pin connected to ST_CP of 74HC595

11int latchPin = 8;

12//Pin connected to SH_CP of 74HC595

13int clockPin = 12;

14////Pin connected to DS of 74HC595

15int dataPin = 11;

16void setup() {

17//set pins to output so you can control the shift register

18pinMode(latchPin, OUTPUT);

19pinMode(clockPin, OUTPUT);

20pinMode(dataPin, OUTPUT);

21}

22void loop() {

23// count from 0 to 255 and display the number

24// on the LEDs

25for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) {

26// take the latchPin low so

27// the LEDs don't change while you're sending in bits:

28digitalWrite(latchPin, LOW);

29// shift out the bits:

30shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);

31//take the latch pin high so the LEDs will light up:

32digitalWrite(latchPin, HIGH);

33// pause before next value:

34delay(500);

35}

36}

ShftOut12

COPY

1/*

2Shift Register Example

3for 74HC595 shift register

4This sketch turns reads serial input and uses it to set the pins

5of a 74HC595 shift register.

6Hardware:

7* 74HC595 shift register attached to pins 8, 12, and 11 of the Arduino,

8as detailed below.

9* LEDs attached to each of the outputs of the shift register.

10Created 22 May 2009

11Created 23 Mar 2010

12by Tom Igoe

13*/

14//Pin connected to latch pin (ST_CP) of 74HC595

15const int latchPin = 8;

16//Pin connected to clock pin (SH_CP) of 74HC595

17const int clockPin = 12;

18////Pin connected to Data in (DS) of 74HC595

19const int dataPin = 11;

20void setup() {

21//set pins to output because they are addressed in the main loop

22pinMode(latchPin, OUTPUT);

23pinMode(dataPin, OUTPUT);

24pinMode(clockPin, OUTPUT);

25Serial.begin(9600);

26Serial.println("reset");

27}

28void loop() {

29if (Serial.available() > 0) {

30// ASCII '0' through '9' characters are

31// represented by the values 48 through 57.

32// so if the user types a number from 0 through 9 in ASCII,

33// you can subtract 48 to get the actual value:

34int bitToSet = Serial.read() - 48;

35// write to the shift register with the correct bit set high:

36registerWrite(bitToSet, HIGH);

37}

38}

39// This method sends bits to the shift register:

40void registerWrite(int whichPin, int whichState) {

41// the bits you want to send

42byte bitsToSend = 0;

43// turn off the output so the pins don't light up

44// while you're shifting bits:

45digitalWrite(latchPin, LOW);

46// turn on the next highest bit in bitsToSend:

47bitWrite(bitsToSend, whichPin, whichState);

48// shift the bits out:

49shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

50// turn on the output so the LEDs can light up:

51digitalWrite(latchPin, HIGH);

52}

ShftOut13

COPY

1/*

2

3 Shift Register Example

4

5 Turning on the outputs of a 74HC595 using an array

6

7 Hardware:

8

9 * 74HC595 shift register

10

11 * LEDs attached to each of the outputs of the shift register

12

13 */

14//Pin connected to ST_CP of 74HC595

15int latchPin = 8;

16//Pin connected to SH_CP of 74HC595

17int clockPin = 12;

18////Pin connected to DS of 74HC595

19int dataPin = 11;

20

21//holders for information you're going to pass to shifting function

22byte data;

23byte dataArray[10];

24

25void setup() {

26

27 //set pins to output because they are addressed in the main loop

28

29 pinMode(latchPin, OUTPUT);

30

31 Serial.begin(9600);

32

33 //Binary notation as comment

34

35 dataArray[0] = 0xFF; //0b11111111

36

37 dataArray[1] = 0xFE; //0b11111110

38

39 dataArray[2] = 0xFC; //0b11111100

40

41 dataArray[3] = 0xF8; //0b11111000

42

43 dataArray[4] = 0xF0; //0b11110000

44

45 dataArray[5] = 0xE0; //0b11100000

46

47 dataArray[6] = 0xC0; //0b11000000

48

49 dataArray[7] = 0x80; //0b10000000

50

51 dataArray[8] = 0x00; //0b00000000

52

53 dataArray[9] = 0xE0; //0b11100000

54

55 //function that blinks all the LEDs

56

57 //gets passed the number of blinks and the pause time

58

59 blinkAll_2Bytes(2,500);

60}

61

62void loop() {

63

64 for (int j = 0; j < 10; j++) {

65

66 //load the light sequence you want from array

67

68 data = dataArray[j];

69

70 //ground latchPin and hold low for as long as you are transmitting

71

72 digitalWrite(latchPin, 0);

73

74 //move 'em out

75

76 shiftOut(dataPin, clockPin, data);

77

78 //return the latch pin high to signal chip that it

79

80 //no longer needs to listen for information

81

82 digitalWrite(latchPin, 1);

83

84 delay(300);

85

86 }

87}

88

89// the heart of the program

90void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {

91

92 // This shifts 8 bits out MSB first,

93

94 //on the rising edge of the clock,

95

96 //clock idles low

97

98 //internal function setup

99

100 int i=0;

101

102 int pinState;

103

104 pinMode(myClockPin, OUTPUT);

105

106 pinMode(myDataPin, OUTPUT);

107

108 //clear everything out just in case to

109

110 //prepare shift register for bit shifting

111

112 digitalWrite(myDataPin, 0);

113

114 digitalWrite(myClockPin, 0);

115

116 //for each bit in the byte myDataOut&#xFFFD;

117

118 //NOTICE THAT WE ARE COUNTING DOWN in our for loop

119

120 //This means that %00000001 or "1" will go through such

121

122 //that it will be pin Q0 that lights.

123

124 for (i=7; i>=0; i--) {

125

126 digitalWrite(myClockPin, 0);

127

128 //if the value passed to myDataOut and a bitmask result

129

130 // true then... so if we are at i=6 and our value is

131

132 // %11010100 it would the code compares it to %01000000

133

134 // and proceeds to set pinState to 1.

135

136 if ( myDataOut & (1<<i) ) {

137

138 pinState= 1;

139

140 }

141

142 else {

143

144 pinState= 0;

145

146 }

147

148 //Sets the pin to HIGH or LOW depending on pinState

149

150 digitalWrite(myDataPin, pinState);

151

152 //register shifts bits on upstroke of clock pin

153

154 digitalWrite(myClockPin, 1);

155

156 //zero the data pin after shift to prevent bleed through

157

158 digitalWrite(myDataPin, 0);

159

160 }

161

162 //stop shifting

163

164 digitalWrite(myClockPin, 0);

165}

166

167//blinks the whole register based on the number of times you want to

168//blink "n" and the pause between them "d"

169//starts with a moment of darkness to make sure the first blink

170//has its full visual effect.

171void blinkAll_2Bytes(int n, int d) {

172

173 digitalWrite(latchPin, 0);

174

175 shiftOut(dataPin, clockPin, 0);

176

177 shiftOut(dataPin, clockPin, 0);

178

179 digitalWrite(latchPin, 1);

180

181 delay(200);

182

183 for (int x = 0; x < n; x++) {

184

185 digitalWrite(latchPin, 0);

186

187 shiftOut(dataPin, clockPin, 255);

188

189 shiftOut(dataPin, clockPin, 255);

190

191 digitalWrite(latchPin, 1);

192

193 delay(d);

194

195 digitalWrite(latchPin, 0);

196

197 shiftOut(dataPin, clockPin, 0);

198

199 shiftOut(dataPin, clockPin, 0);

200

201 digitalWrite(latchPin, 1);

202

203 delay(d);

204

205 }

206}

ShftOut21

COPY

1//**************************************************************//

2// Name : shiftOutCode, Dual Binary Counters //

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//Pin connected to ST_CP of 74HC595

10int latchPin = 8;

11//Pin connected to SH_CP of 74HC595

12int clockPin = 12;

13////Pin connected to DS of 74HC595

14int dataPin = 11;

15void setup() {

16//Start Serial for debugging purposes

17Serial.begin(9600);

18//set pins to output because they are addressed in the main loop

19pinMode(latchPin, OUTPUT);

20}

21void loop() {

22//count up routine

23for (int j = 0; j < 256; j++) {

24//ground latchPin and hold low for as long as you are transmitting

25digitalWrite(latchPin, 0);

26//count up on GREEN LEDs

27shiftOut(dataPin, clockPin, j);

28//count down on RED LEDs

29shiftOut(dataPin, clockPin, 255-j);

30//return the latch pin high to signal chip that it

31//no longer needs to listen for information

32digitalWrite(latchPin, 1);

33delay(1000);

34}

35}

36void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {

37// This shifts 8 bits out MSB first,

38//on the rising edge of the clock,

39//clock idles low

40..//internal function setup

41int i=0;

42int pinState;

43pinMode(myClockPin, OUTPUT);

44pinMode(myDataPin, OUTPUT);

45. //clear everything out just in case to

46. //prepare shift register for bit shifting

47digitalWrite(myDataPin, 0);

48digitalWrite(myClockPin, 0);

49//for each bit in the byte myDataOut&#xFFFD;

50//NOTICE THAT WE ARE COUNTING DOWN in our for loop

51//This means that %00000001 or "1" will go through such

52//that it will be pin Q0 that lights.

53for (i=7; i>=0; i--) {

54digitalWrite(myClockPin, 0);

55//if the value passed to myDataOut and a bitmask result

56// true then... so if we are at i=6 and our value is

57// %11010100 it would the code compares it to %01000000

58// and proceeds to set pinState to 1.

59if ( myDataOut & (1<<i) ) {

60pinState= 1;

61}

62else {

63pinState= 0;

64}

65//Sets the pin to HIGH or LOW depending on pinState

66digitalWrite(myDataPin, pinState);

67//register shifts bits on upstroke of clock pin

68digitalWrite(myClockPin, 1);

69//zero the data pin after shift to prevent bleed through

70digitalWrite(myDataPin, 0);

71}

72//stop shifting

73digitalWrite(myClockPin, 0);

74}

ShftOut22

COPY

1//**************************************************************//

2// Name : shiftOutCode, Dual One By One //

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//Pin connected to ST_CP of 74HC595

10int latchPin = 8;

11//Pin connected to SH_CP of 74HC595

12int clockPin = 12;

13////Pin connected to DS of 74HC595

14int dataPin = 11;

15//holder for information you're going to pass to shifting function

16byte data = 0;

17void setup() {

18//set pins to output because they are addressed in the main loop

19pinMode(latchPin, OUTPUT);

20}

21void loop() {

22//function that blinks all the LEDs

23//gets passed the number of blinks and the pause time

24blinkAll_2Bytes(1,500);

25// light each pin one by one using a function A

26for (int j = 0; j < 8; j++) {

27//ground latchPin and hold low for as long as you are transmitting

28digitalWrite(latchPin, 0);

29//red LEDs

30lightShiftPinA(7-j);

31//green LEDs

32lightShiftPinA(j);

33//return the latch pin high to signal chip that it

34//no longer needs to listen for information

35digitalWrite(latchPin, 1);

36delay(1000);

37}

38// light each pin one by one using a function A

39for (int j = 0; j < 8; j++) {

40//ground latchPin and hold low for as long as you are transmitting

41digitalWrite(latchPin, 0);

42//red LEDs

43lightShiftPinB(j);

44//green LEDs

45lightShiftPinB(7-j);

46//return the latch pin high to signal chip that it

47//no longer needs to listen for information

48digitalWrite(latchPin, 1);

49delay(1000);

50}

51}

52//This function uses bitwise math to move the pins up

53void lightShiftPinA(int p) {

54//defines a local variable

55int pin;

56//this is line uses a bitwise operator

57//shifting a bit left using << is the same

58//as multiplying the decimal number by two.

59pin = 1<< p;

60//move 'em out

61shiftOut(dataPin, clockPin, pin);

62}

63//This function uses that fact that each bit in a byte

64//is 2 times greater than the one before it to

65//shift the bits higher

66void lightShiftPinB(int p) {

67//defines a local variable

68int pin;

69//start with the pin = 1 so that if 0 is passed to this

70//function pin 0 will light.

71pin = 1;

72for (int x = 0; x < p; x++) {

73pin = pin * 2;

74}

75//move 'em out

76shiftOut(dataPin, clockPin, pin);

77}

78// the heart of the program

79void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {

80// This shifts 8 bits out MSB first,

81//on the rising edge of the clock,

82//clock idles low

83//internal function setup

84int i=0;

85int pinState;

86pinMode(myClockPin, OUTPUT);

87pinMode(myDataPin, OUTPUT);

88//clear everything out just in case to

89//prepare shift register for bit shifting

90digitalWrite(myDataPin, 0);

91digitalWrite(myClockPin, 0);

92//for each bit in the byte myDataOut&#xFFFD;

93//NOTICE THAT WE ARE COUNTING DOWN in our for loop

94//This means that %00000001 or "1" will go through such

95//that it will be pin Q0 that lights.

96for (i=7; i>=0; i--) {

97digitalWrite(myClockPin, 0);

98//if the value passed to myDataOut and a bitmask result

99// true then... so if we are at i=6 and our value is

100// %11010100 it would the code compares it to %01000000

101// and proceeds to set pinState to 1.

102if ( myDataOut & (1<<i) ) {

103pinState= 1;

104}

105else {

106pinState= 0;

107}

108//Sets the pin to HIGH or LOW depending on pinState

109digitalWrite(myDataPin, pinState);

110//register shifts bits on upstroke of clock pin

111digitalWrite(myClockPin, 1);

112//zero the data pin after shift to prevent bleed through

113digitalWrite(myDataPin, 0);

114}

115//stop shifting

116digitalWrite(myClockPin, 0);

117}

118//blinks both registers based on the number of times you want to

119//blink "n" and the pause between them "d"

120//starts with a moment of darkness to make sure the first blink

121//has its full visual effect.

122void blinkAll_2Bytes(int n, int d) {

123digitalWrite(latchPin, 0);

124shiftOut(dataPin, clockPin, 0);

125shiftOut(dataPin, clockPin, 0);

126digitalWrite(latchPin, 1);

127delay(200);

128for (int x = 0; x < n; x++) {

129digitalWrite(latchPin, 0);

130shiftOut(dataPin, clockPin, 255);

131shiftOut(dataPin, clockPin, 255);

132digitalWrite(latchPin, 1);

133delay(d);

134digitalWrite(latchPin, 0);

135shiftOut(dataPin, clockPin, 0);

136shiftOut(dataPin, clockPin, 0);

137digitalWrite(latchPin, 1);

138delay(d);

139}

140}

ShftOut23

COPY

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//Pin connected to ST_CP of 74HC595

10int latchPin = 8;

11//Pin connected to SH_CP of 74HC595

12int clockPin = 12;

13////Pin connected to DS of 74HC595

14int dataPin = 11;

15//holders for information you're going to pass to shifting function

16byte dataRED;

17byte dataGREEN;

18byte dataArrayRED[10];

19byte dataArrayGREEN[10];

20void setup() {

21//set pins to output because they are addressed in the main loop

22pinMode(latchPin, OUTPUT);

23Serial.begin(9600);

24//Arduino doesn't seem to have a way to write binary straight into the code

25//so these values are in HEX. Decimal would have been fine, too.

26dataArrayRED[0] = 0xFF; //11111111

27dataArrayRED[1] = 0xFE; //11111110

28dataArrayRED[2] = 0xFC; //11111100

29dataArrayRED[3] = 0xF8; //11111000

30dataArrayRED[4] = 0xF0; //11110000

31dataArrayRED[5] = 0xE0; //11100000

32dataArrayRED[6] = 0xC0; //11000000

33dataArrayRED[7] = 0x80; //10000000

34dataArrayRED[8] = 0x00; //00000000

35dataArrayRED[9] = 0xE0; //11100000

36//Arduino doesn't seem to have a way to write binary straight into the code

37//so these values are in HEX. Decimal would have been fine, too.

38dataArrayGREEN[0] = 0xFF; //11111111

39dataArrayGREEN[1] = 0x7F; //01111111

40dataArrayGREEN[2] = 0x3F; //00111111

41dataArrayGREEN[3] = 0x1F; //00011111

42dataArrayGREEN[4] = 0x0F; //00001111

43dataArrayGREEN[5] = 0x07; //00000111

44dataArrayGREEN[6] = 0x03; //00000011

45dataArrayGREEN[7] = 0x01; //00000001

46dataArrayGREEN[8] = 0x00; //00000000

47dataArrayGREEN[9] = 0x07; //00000111

48//function that blinks all the LEDs

49//gets passed the number of blinks and the pause time

50blinkAll_2Bytes(2,500);

51}

52void loop() {

53for (int j = 0; j < 10; j++) {

54//load the light sequence you want from array

55dataRED = dataArrayRED[j];

56dataGREEN = dataArrayGREEN[j];

57//ground latchPin and hold low for as long as you are transmitting

58digitalWrite(latchPin, 0);

59//move 'em out

60shiftOut(dataPin, clockPin, dataGREEN);

61shiftOut(dataPin, clockPin, dataRED);

62//return the latch pin high to signal chip that it

63//no longer needs to listen for information

64digitalWrite(latchPin, 1);

65delay(300);

66}

67}

68// the heart of the program

69void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {

70// This shifts 8 bits out MSB first,

71//on the rising edge of the clock,

72//clock idles low

73//internal function setup

74int i=0;

75int pinState;

76pinMode(myClockPin, OUTPUT);

77pinMode(myDataPin, OUTPUT);

78//clear everything out just in case to

79//prepare shift register for bit shifting

80digitalWrite(myDataPin, 0);

81digitalWrite(myClockPin, 0);

82//for each bit in the byte myDataOut&#xFFFD;

83//NOTICE THAT WE ARE COUNTING DOWN in our for loop

84//This means that %00000001 or "1" will go through such

85//that it will be pin Q0 that lights.

86for (i=7; i>=0; i--) {

87digitalWrite(myClockPin, 0);

88//if the value passed to myDataOut and a bitmask result

89// true then... so if we are at i=6 and our value is

90// %11010100 it would the code compares it to %01000000

91// and proceeds to set pinState to 1.

92if ( myDataOut & (1<<i) ) {

93pinState= 1;

94}

95else {

96pinState= 0;

97}

98//Sets the pin to HIGH or LOW depending on pinState

99digitalWrite(myDataPin, pinState);

100//register shifts bits on upstroke of clock pin

101digitalWrite(myClockPin, 1);

102//zero the data pin after shift to prevent bleed through

103digitalWrite(myDataPin, 0);

104}

105//stop shifting

106digitalWrite(myClockPin, 0);

107}

108//blinks the whole register based on the number of times you want to

109//blink "n" and the pause between them "d"

110//starts with a moment of darkness to make sure the first blink

111//has its full visual effect.

112void blinkAll_2Bytes(int n, int d) {

113digitalWrite(latchPin, 0);

114shiftOut(dataPin, clockPin, 0);

115shiftOut(dataPin, clockPin, 0);

116digitalWrite(latchPin, 1);

117delay(200);

118for (int x = 0; x < n; x++) {

119digitalWrite(latchPin, 0);

120shiftOut(dataPin, clockPin, 255);

121shiftOut(dataPin, clockPin, 255);

122digitalWrite(latchPin, 1);

123delay(d);

124digitalWrite(latchPin, 0);

125shiftOut(dataPin, clockPin, 0);

126shiftOut(dataPin, clockPin, 0);

127digitalWrite(latchPin, 1);

128delay(d);

129}

130}

Source-https://docs.arduino.cc/tutorials/communication/guide-to-shift-out

No comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();
Powered by Blogger.