Chapter 17 74HC595 & LEDBar Graph
In second “for” cycle, the situation is the same. The difference is that x is shift from 0x80 to right in order.
"<<" is the left shift operator, which can make all bits of 1 byte shift by several bits to the left (high) direction
and add 0 on the right (low). For example, shift binary 00000001 by 1 bit to left:
byte x = 1 << 1;
← ← ← ← ← ← ←
The result of x is 2(binary 00000010).
There is another similar operator" >>". For example, shift binary 00000001 by 1 bit to right:
byte x = 1 >> 1;
→ → → → → → →
The result of x is 0(00000000).
X <<= 1 is equivalent to x = x << 1 and x >>= 1 is equivalent to x = x >> 1
uint8_t shiftIn (uint8_t dPin, uint8_t cPin, uint8_t order) ;
This is used to shift an 8-bit data value in with the data appearing on the dPin and the clock being sent out
on the cPin. Order is either LSBFIRST or MSBFIRST. The data is sampled after the cPin goes high. (So cPin
high, sample data, cPin low, repeat for 8 bits) The 8-bit value is returned by the function.
void shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) ;
void _shiftOut (uint8_t dPin, uint8_t cPin, uint8_t order, uint8_t val) ;
This is used to shift an 8-bit data value out with the data being sent out on dPin and the clock being sent
out on the cPin. order is as above. Data is clocked out on the rising or falling edge - ie. dPin is set, then
cPin is taken high then low - repeated for the 8 bits.
For more details about shift function, please refer to: http://wiringpi.com/reference/shift-library/
Python Code 17.1.1 LightWater02
First observe the project result, and then analyze the code.
1. Use cd command to enter 17.1.1_LightWater02 directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/17.1.1_LightWater02
2. Use python command to execute python code “LightWater02.py”.
python LightWater02.py
After the program is executed, LEDBar Graph begin to display flowing water light from left to right, then from
right to left.
The following is the program code:
im port RPi. GPIO as GPIO
im port time
# Defines the data bit that is transmitted preferentially in the shiftOut function.
LSBFIRST = 1