from machine import I2C, Pin
import time
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=100000)
PCF_IN = 0x20 # PCF8574 (entrada)
PCF_OUT = 0x38 # PCF8574A (salida)
RTC = 0x68 # DS1307
def bcd(b):
return (b >> 4) * 10 + (b & 0x0F)
# Puerto de entrada: poner todas las líneas en alto
i2c.writeto(PCF_IN, b'\xFF')
# Arrancar el RTC si está detenido
s = i2c.readfrom_mem(RTC, 0x00, 1)[0]
if s & 0x80:
i2c.writeto_mem(RTC, 0x00, bytes([s & 0x7F]))
while True:
dato = i2c.readfrom(PCF_IN, 1)[0] # leer 8 entradas
i2c.writeto(PCF_OUT, bytes([dato])) # escribir en la salida
t = i2c.readfrom_mem(RTC, 0x00, 7)
seg = bcd(t[0] & 0x7F)
minu = bcd(t[1])
hora = bcd(t[2] & 0x3F)
dia = bcd(t[4])
mes = bcd(t[5])
print("Entrada: {:08b} | {:02d}/{:02d} {:02d}:{:02d}:{:02d}".format(
dato, dia, mes, hora, minu, seg))
time.sleep(1)