from machine import Pin, PWM, Timer
import time
from HC595 import SerieHC595
#declaracion de variables
inicio = 0
time1 = 0
time2 = 0
numero = 0
power = 0
n_chips = 4
buzzer = 0
reset = 0
#declaración de pines
bot_reset = Pin(18, Pin.IN, Pin.PULL_DOWN)
bot_up = Pin(20, Pin.IN, Pin.PULL_DOWN)
bot_down = Pin(19, Pin.IN, Pin.PULL_DOWN)
bot_pwr = Pin(21, Pin.IN, Pin.PULL_DOWN)
vcc = Pin(1, Pin.OUT, value=0)
bzz = PWM(Pin(4))
bzz.freq(1000)#frecuencia del PWM
integrado = SerieHC595(0,n_chips,2,3,0)
'''
Con el primer "0" se indica que modulo SPI está en uso, el 0 o el 1
Con "n_chips" indico la cantidad de esclavos
Con el "2" indico que utiliza el GP2 que es el SPI0 clk
Con el "3" indico que se utiliza el GP3 el cual es SPI0 TX
Con el "0" indico que se utiliza el GP0 (puede ser cualquier pin)
'''
def zfill_manual(valor, a):
return '{:0>{w}}'.format(valor, w=a)
def lectura_de_boton(boton):
global inicio, time1, time2, numero, power, buzzer, reset
time1 = time.ticks_ms()
time2 = time.ticks_diff(time1, inicio)
if(time2 > 150):
inicio = time.ticks_ms()#Al apretar el botón, genera rebote, el cual inicia esta linea, generando que la diferencia de tiempo sea menor a 150ms
if boton == bot_reset:
reset = 1 - reset
numero = 0
if boton == bot_up and power == 1:
numero += 1
buzzer = 1
reset = 0
if boton == bot_down:
if numero > 0 and power == 1:
numero -= 1
buzzer = 1
reset = 0
if boton == bot_pwr:
power = 1 - power
bot_reset.irq(handler=lectura_de_boton, trigger = Pin.IRQ_RISING)
bot_up.irq(handler=lectura_de_boton, trigger = Pin.IRQ_RISING)
bot_down.irq(handler=lectura_de_boton, trigger = Pin.IRQ_RISING)
bot_pwr.irq(handler=lectura_de_boton, trigger = Pin.IRQ_RISING)
def buzz_off(timer):
global bzz
bzz.duty_ns(0)
while True:
if numero > 9999:
numero = 0
if power == 1:
vcc.value(1)
else:
vcc.value(0)
if buzzer == 1:
#estado = machine.disable_irq() #en "estado" guardamos el estado de la funcion irq
bzz.duty_ns(512)#el dc va de 0 a 1023, 512 es el 50%
tim = Timer()
tim.init(mode=Timer.ONE_SHOT, period = 1000, callback=buzz_off)
buzzer = 0
#machine.enable_irq(estado) #volvemos a activar las interrupciones
valor_str = zfill_manual(numero, n_chips)
integrado.enviar(valor_str, reset)
if reset == 1:
time.sleep_ms(250)
reset = 0
time.sleep_ms(10)Loading
pi-pico-w
pi-pico-w