import machine
import time
# Define GPIO pins
data_pin = machine.Pin(17, machine.Pin.OUT)
clock_pin = machine.Pin(27, machine.Pin.OUT)
latch_pin = machine.Pin(22, machine.Pin.OUT)
dp_pin = machine.Pin(5, machine.Pin.OUT)
# Define digit selection pins
digi_pins = [
machine.Pin(6, machine.Pin.OUT), # Digi 1
machine.Pin(7, machine.Pin.OUT), # Digi 2
machine.Pin(8, machine.Pin.OUT), # Digi 3
machine.Pin(9, machine.Pin.OUT), # Digi 4
]
# Function to shift data to the shift register
def shift_out(data, clock, latch, value):
for _ in range(8):
data.value((value >> 7) & 1)
value <<= 1
clock.value(1)
clock.value(0)
latch.value(1)
latch.value(0)
# Function to display a digit on the 7-segment display
def display_digit(digit):
# Define a dictionary for 7-segment representation
segments = {
0: 0b00111111,
1: 0b00000110,
2: 0b01011011,
3: 0b01001111,
4: 0b01100110,
5: 0b01101101,
6: 0b01111101,
7: 0b00000111,
8: 0b01111111,
9: 0b01101111,
}
# Display the digit on the 7-segment display
digi_pins[0].value(1) # Select the digit
shift_out(data_pin, clock_pin, latch_pin, segments[digit%10])
time.sleep(1)
digi_pins[1].value(1)
shift_out(data_pin, clock_pin, latch_pin, segments[digit//10%10])
time.sleep(1)
digi_pins[2].value(1)
shift_out(data_pin, clock_pin, latch_pin, segments[digit//100%10])
time.sleep(1)
digi_pins[3].value(1)
shift_out(data_pin, clock_pin, latch_pin, segments[digit//1000%10])
time.sleep(1)
digi_pins[0].value(0) # Deselect the digit
digi_pins[1].value(0)
digi_pins[2].value(0)
digi_pins[3].value(0)
# Main loop
counter = 0
while True:
# Display the counter value on the 7-segment display
display_digit(counter)
# Increment the counter
counter += 1
# Add a delay for visibility
time.sleep(1)