from machine import Pin, ADC, Timer
import time
# Salidas para los segmentos
leds = (0, 1, 2, 3, 4, 5, 6)
lista_Pin = list()
for i in range(7):
lista_Pin.append(Pin(leds[i], Pin.OUT))
# habilitadores
uni = Pin(15, Pin.OUT)
dec = Pin(14, Pin.OUT)
digitos = {
0: [1, 1, 1, 1, 1, 1, 0], # 0
1: [0, 1, 1, 0, 0, 0, 0], # 1
2: [1, 1, 0, 1, 1, 0, 1], # 2
3: [1, 1, 1, 1, 0, 0, 1], # 3
4: [0, 1, 1, 0, 0, 1, 1], # 4
5: [1, 0, 1, 1, 0, 1, 1], # 5
6: [1, 0, 1, 1, 1, 1, 1], # 6
7: [1, 1, 1, 0, 0, 0, 0], # 7
8: [1, 1, 1, 1, 1, 1, 1], # 8
9: [1, 1, 1, 1, 0, 1, 1], # 9
10: [0, 0, 0, 0, 0, 0, 0] # ALL OFF
}
def mostrar(numero):
for pin, estado in zip(lista_Pin, digitos[numero]):
pin.value(estado)
# Variables de control
ref = 50 # Referencia
show = 0 # 0 para ADC, 1 para referencia y 2 para nada
config = 0 # 0 normal y 1 configuración
info = 0 # Variable global con el valor del ADC
adc = ADC(26)
tim = Timer() # Temporizador
tim2 = Timer() # Temporizador 2
def temporizador(tim): # Lee ADC cada 300 ms
global info
val_adc = adc.read_u16()
info = int((100/65535)*val_adc)
if info == 100:
info = 99
def tempo2(tim2): # Hace parpadear la referencia
global show
if show == 1:
show = 2
else:
show = 1
# Inicializa al temporizador
tim.init(period=300, mode=Timer.PERIODIC, callback=temporizador)
set_boton = Pin( 16, Pin.IN, Pin.PULL_UP )
def set_handler(Pin):
global config, show
if config == 0:
config = 1
show = 1
tim2.init(period=500, mode=Timer.PERIODIC, callback=tempo2)
else:
config = 0
show = 0
tim2.deinit()
set_boton.irq(trigger=Pin.IRQ_FALLING, handler=set_handler)
# Los displays inician inhabilitados
uni.value(1)
dec.value(1)
while 1:
if show == 0:
unidades = info%10
decenas = info//10
elif show == 1:
unidades = ref%10
decenas = ref//10
else:
unidades = 10
decenas = 10
mostrar(unidades)
uni.value(0)
time.sleep_ms(5)
uni.value(1)
mostrar(decenas)
dec.value(0)
time.sleep_ms(5)
dec.value(1)