from machine import Pin
from time import sleep
# ------------------------------------------------------------
# ECEN 218 - Digital Logic Laboratory
# Lab 0: Virtual Digital Laboratory and Pico Introduction
#
# Raspberry Pi Pico pin assignments:
#
# GP2 -> Logic input A
# GP3 -> Logic input B
# GP4 <- Logic output Y
#
# The Pico will:
# 1. Generate all possible combinations of A and B
# 2. Read the resulting value of Y
# 3. Print the measured truth table
# 4. Compare the measured value to the expected value
# 5. Report whether the circuit passes the test
# ------------------------------------------------------------
# Configure Pico GPIO pins
A = Pin(2, Pin.OUT)
B = Pin(3, Pin.OUT)
Y = Pin(4, Pin.IN)
print()
print("----------------------------------------")
print("ECEN 218 - Lab 0")
print("Digital Logic Automated Test")
print("----------------------------------------")
print()
passed = True
print(" A B | Expected Measured")
print("------+-------------------")
for a in [0, 1]:
for b in [0, 1]:
# Apply digital inputs to the circuit
A.value(a)
B.value(b)
# Allow the circuit to settle
sleep(0.5)
# Measure the circuit output
measured = Y.value()
# ----------------------------------------------------
# STUDENT TASK:
#
# Complete the expression below so that "expected"
# contains the correct output of the AND function.
#
# Replace the 0 with the appropriate Python
# expression.
# ----------------------------------------------------
expected = a & b
# Display one row of the truth table
print(
" %d %d | %d %d"
% (a, b, expected, measured)
)
# Compare expected and measured results
if measured != expected:
passed = False
print()
print("----------------------------------------")
if passed:
print("TEST PASSED")
else:
print("TEST FAILED")
print("----------------------------------------")
print()
# Hold the final input values so the simulation remains active
while True:
sleep(1)