from machine import Pin
from time import sleep
# Inputs
# D2, D1, D0 represent a 3-bit data word
D2_sw = Pin(18, Pin.IN)
D1_sw = Pin(19, Pin.IN)
D0_sw = Pin(21, Pin.IN)
# Outputs
temp_led = Pin(4, Pin.OUT) # Shows TEMP = D2 XOR D1
parity_led = Pin(2, Pin.OUT) # Shows P = TEMP XOR D0
last_state = None
print("======================================")
print(" Even Parity Generator Lab")
print(" P = D2 XOR D1 XOR D0")
print("======================================")
print()
print("D2, D1, D0 = 3-bit data word")
print("P = even parity bit")
print()
print("If data has odd number of 1s, P = 1")
print("If data has even number of 1s, P = 0")
print()
print("D2 D1 D0 | TEMP | P | Total 1s with P")
print("--------------------------------------")
while True:
D2 = D2_sw.value()
D1 = D1_sw.value()
D0 = D0_sw.value()
# XOR chain
TEMP = D2 ^ D1
P = TEMP ^ D0
temp_led.value(TEMP)
parity_led.value(P)
total_ones = D2 + D1 + D0 + P
current_state = (D2, D1, D0, TEMP, P, total_ones)
if current_state != last_state:
print(D2, " ", D1, " ", D0, " | ",
TEMP, " |", P, "| ", total_ones)
last_state = current_state
sleep(0.1)