Hardware Reference
In-Depth Information
Now, follow the steps for writing to the shift register above. First, the LATCH
pin is set low so that the current LED states are not changed while new values
are shifted in. Then, the LED states are shifted into the registers in order on the
CLOCK edge from the DATA line. After all the values have been shifted in, the
LATCH pin is set high again, and the values are outputted from the shift register.
ShiftingSerialDatafromtheArduino
Now that you understand what's happening behind the scenes, you can write
the Arduino code to control the shift register in this fashion. As with all your
previous experiments, you use a convenient function that's built in to the Arduino
IDE to shift data into the register IC. You can use the shiftOut() function to
easily shift out 8 bits of data onto an arbitrary I/O pin. It accepts four parameters:
The data pin number
The clock pin number
The bit order
The value to shift out
If, for example, you want to shift out the alternating pattern described in the
previous section, you could use the shiftOut() function as follows:
shiftOut(DATA, CLOCK, MSBFIRST, B10101010);
The DATA and CLOCK constants are set to the pin numbers for those lines.
MSBFIRST indicates that the most significant bit will be sent first (the leftmost
bit when looking at the binary number to send). You could alternatively send
the data with the LSBFIRST setting, which would start by transmitting the bits
from the right side of the binary data. The final parameter is the number to be
sent. By putting a capital B before the number, you are telling the Arduino IDE
to interpret the following numbers as a binary value rather than as a decimal
integer.
Next, you build a physical version of the system that you just learned about
in the previous sections. First, you need to get the shift register wired up to
your Arduino:
The DATA pin will connect to pin 8.
The LATCH pin will connect to pin 9.
The CLOCK pin will connect to pin 10.
Don't forget to use current limiting resistors with your LEDs. Reference the
diagram shown in Figure 7-4 to set up the circuit.
Search WWH ::




Custom Search