The next thing I wanted to get working with PIO was reading the pin values. I struggled with this until I realized that I needed to initialize the pins and set them to pull_up before starting the state machine. Anyway, here's an example reading all available pins and returning their states on the fifo:
@rp2.asm_pio( set_init=[PIO.IN_HIGH]*32 )
set(x, 31) # call nop 32 times to slow things down
for n in range(32): # Initialize all the pins with PULL_UP
Pin(n, Pin.IN, Pin.PULL_UP)
print("Couldn't initialize pin:", n)
sm = rp2.StateMachine(0, pio_junk.echo_pins,
for n in range(10): # pull pin states from the fifo ten times!
out = sm.get()
And the output looks like the following. Some of the bits change as I press keys on the keyboard:>>>
MPY: soft reboot
Checking Safety Pin 25...
In Main Now
Couldn't initialize pin: 30
Couldn't initialize pin: 31
MicroPython v1.19.1 on 2022-06-18; Arduino Nano RP2040 Connect with RP2040
Type "help()" for more information.
In Part 3, we'll track the pin states and only output a value when there is a change.
And in Part 4, we'll use an irq to trigger a python function only when there is a key press event so we don't need to keep a cpu core busy watching for button presses.
Post a Comment