from machine import Pin
from time import sleep
# Inputs
P1 = Pin(18, Pin.IN)
P0 = Pin(19, Pin.IN)
Q1 = Pin(21, Pin.IN)
Q0 = Pin(22, Pin.IN)
# Outputs
msb_led = Pin(4, Pin.OUT) # P1 XNOR Q1
lsb_led = Pin(5, Pin.OUT) # P0 XNOR Q0
equal_led = Pin(2, Pin.OUT) # Final EQUAL
last_state = None
print("====================================")
print(" Two-Bit Equality Checker")
print(" EQUAL = XNOR(P1,Q1) AND XNOR(P0,Q0)")
print("====================================")
print()
print("P = P1P0")
print("Q = Q1Q0")
print()
print("Yellow LED = P1 matches Q1")
print("Blue LED = P0 matches Q0")
print("Red LED = Numbers Equal")
print()
print("P1 P0 Q1 Q0 | M1 M0 | EQUAL")
print("----------------------------")
while True:
p1 = P1.value()
p0 = P0.value()
q1 = Q1.value()
q0 = Q0.value()
# XNOR = 1 when bits are same
match1 = 1 if p1 == q1 else 0
match0 = 1 if p0 == q0 else 0
equal = match1 and match0
msb_led.value(match1)
lsb_led.value(match0)
equal_led.value(int(equal))
current = (p1, p0, q1, q0, match1, match0, int(equal))
if current != last_state:
print(p1, " ", p0, " ", q1, " ", q0,
" | ", match1, " ", match0,
" | ", int(equal))
last_state = current
sleep(0.1)