'''
Project: 3 Button UpDown Counter using PIO StateMachine
Project by: Jim F, Calgary AB
Date Created: 20 Nov 2024
Date Updated: 20 Nov 2024

References:
Paul McWhorter's Pico W PIO 2 Button UpDown Tutorial
Inspired By: https://www.youtube.com/watch?v=0D6NNOWm9Og
'''

from machine import Pin
from rp2 import asm_pio, PIO, StateMachine
from time import sleep

pin13 = Pin(13, Pin.IN, Pin.PULL_DOWN)
pin14 = Pin(14, Pin.IN, Pin.PULL_DOWN)
pin15 = Pin(15, Pin.IN, Pin.PULL_DOWN)

@asm_pio(out_init = (PIO.OUT_LOW, ) *4, set_init = PIO.OUT_LOW, out_shiftdir = PIO.SHIFT_RIGHT)
def pio_prog():
    label("start")
    set(y,0b0000)
    wrap_target()
    label("display")
    mov(pins,y)
    mov(isr,null)
    mov(osr,null)
    label("getInput")
    in_(pins,3)
    mov(x,isr)
    jmp(not_x,"getInput")
    mov(osr,y)
    set(y,0b0100)
    jmp(x_not_y,"dec")
    wait(0,pin,2)
    jmp("start")
    label("dec")
    wait(0,pin,0)
    set(y,0b0001)
    jmp(x_not_y,"inc")
    mov(y,osr)
    jmp(y_dec,"display")
    jmp("getInput")
    label("inc")
    wait(0,pin,1)
    mov(y,osr)
    mov(y,invert(y))
    jmp(y_dec,"nxt")
    label("nxt")
    mov(y,invert(y))
    mov(osr,y)
    jmp("display")
    wrap()

sm0 = StateMachine(0, pio_prog, freq = 2000, out_base = Pin(0), in_base = pin13)
sm0.active(1)

while True :
    sleep(0.5)

BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT