from machine import Pin
from time import sleep
# Define your pins
ds = Pin(14, Pin.OUT)
oe = Pin(25, Pin.OUT)
mr = Pin(27, Pin.OUT, Pin.PULL_UP)
latch = Pin(12, Pin.OUT) # STCP
clock = Pin(13, Pin.OUT) # SHCP
pins = [
Pin(34, Pin.IN, Pin.PULL_DOWN),
Pin(35, Pin.IN, Pin.PULL_DOWN),
Pin(23, Pin.IN, Pin.PULL_DOWN),
Pin(22, Pin.IN, Pin.PULL_DOWN),
Pin(26, Pin.IN, Pin.PULL_DOWN),
Pin(33, Pin.IN, Pin.PULL_DOWN),
Pin(21, Pin.IN, Pin.PULL_DOWN),
Pin(19, Pin.IN, Pin.PULL_DOWN),
Pin(18, Pin.IN, Pin.PULL_DOWN),
Pin(5, Pin.IN, Pin.PULL_DOWN),
Pin(17, Pin.IN, Pin.PULL_DOWN),
Pin(16, Pin.IN, Pin.PULL_DOWN),
Pin(4, Pin.IN, Pin.PULL_DOWN),
Pin(2, Pin.IN, Pin.PULL_DOWN),
Pin(15, Pin.IN, Pin.PULL_DOWN),
Pin(32, Pin.IN, Pin.PULL_DOWN)
]
num_of_pins = 16
value = 1
def read_ios():
return [pin.value() for pin in pins]
def shift_out(value):
oe.value(1) # Disable output
latch.value(0) # Prepare latch
# # Clear the shift register
mr.value(1)
sleep(0.001)
mr.value(0)
sleep(0.001)
mr.value(1)
# Shift out the "high"" value
for i in range(num_of_pins-1, -1, -1):
ds.value((value >> i) & 1) # Set data pin
clock.value(0)
sleep(0.001)
clock.value(1)
latch.value(1) # Latch to output
oe.value(0) # Enable output
def turn_on_io(n):
shift_out(1 << n)
def analyze_matrix(matrix):
num_pins = len(matrix)
error_found = False # Flag to detect any issues
for i in range(num_pins):
if matrix[i][i] == 0: # Check diagonal element
print(f"Pin {i+1} is disconnected.")
error_found = True
for j in range(i+1, num_pins): # Check upper triangle
if matrix[i][j] == 1:
print(f"Short between Pin {i+1} and Pin {j+1}.")
error_found = True
if not error_found:
print(f"Cable OK - {num_pins} Pins") # All connections are okay
connectivity_matrix = [[0 for _ in range(num_of_pins)] for _ in range(num_of_pins)]
while True:
for i in range(num_of_pins):
turn_on_io(i)
sleep(0.1)
connectivity_matrix[i] = read_ios()
oe.value(1)
print("Matrix after updating row", i, ":")
for row in connectivity_matrix:
print(row)
analyze_matrix(connectivity_matrix) # Analyze and print results
print("\n")
# Delay
sleep(10)