SunFounder ESP32 Starter Kit
# Initialize the pins for the 74HC595 shift register
sdi = machine.Pin(25, machine.Pin.OUT) # DS
rclk = machine.Pin(27, machine.Pin.OUT) # STcp
srclk = machine.Pin(26, machine.Pin.OUT) # SHcp
3. A function called hc595_shift() is defined to write an 8-bit data to the shift register.
def hc595_shift(dat):
# Set the RCLK pin to low
rclk.off()
# Iterate through each bit (from 7 to 0)
for bit in range(7, -1, -1):
# Extract the current bit from the input data
value = 1 & (dat >> bit)
# Set the SRCLK pin to low
srclk.off()
# Set the value of the SDI pin
sdi.value(value)
# Clock the current bit into the shift register by setting the␣
˓→SRCLK pin to high
srclk.on()
# Latch the data into the storage register by setting the RCLK pin to␣
˓→high
rclk.on()
4. About the for loop.
for i in range(16):
if i < 8:
num = (num << 1) + 1 # Shift left and set the least␣
˓→significant bit to 1
elif i >= 8:
num = (num & 0b01111111) << 1 # Mask the most significant bit␣
˓→and shift left
hc595_shift(num) # Shift the current value into the 74HC595
print("{:0>8b}".format(num)) # Print the current value in binary␣
˓→format
time.sleep_ms(200) # Wait 200 milliseconds before shifting the␣
˓→next value
• The variable i is used to control the output binary value. In the first 8 iterations, the value of
num will be successively 00000001, 00000011, 00000111, . . ., 11111111, which is left-shifted
by one bit and then added by 1.
• In the 9th to 16th iterations, the highest bit of 1 is first changed to 0, and then left-shifted by one
bit, resulting in the output values of 00000010, 00000100, 00001000, . . . , 10000000.
• In each iteration, the value of num is passed to the hc595_shift() function to control the shift
register to output the corresponding binary value.
324 Chapter 3. For MicroPython User