import rp2
from machine import Pin
import time
# Define PIO program for SM0 to trigger an interrupt on button press
@rp2.asm_pio()
def button_irq():
wrap_target()
wait(1, pin, 0) [31] # Wait for the button press (low signal on GPIO 11)
nop() [31]
nop() [31]
nop() [31]
nop() [31]
irq(block, 0) # Trigger interrupt 0
wait(0, pin, 0) # Wait for the button release (high signal on GPIO 11)
nop() [31]
nop() [31]
nop() [31]
nop() [31]
wrap()
# Initialize State Machine 0 for the button (GPIO 11)
button_pin = Pin(11, Pin.IN, Pin.PULL_DOWN)
sm_button = rp2.StateMachine(0, button_irq, freq=2000, in_base=button_pin)
# GPIO pin for the LED (GPIO 18)
led_pin = Pin(18, Pin.OUT)
# Interrupt handler for the button press
def button_handler(sm):
led_pin.value(not led_pin.value()) # Toggle LED state
# Attach the interrupt handler to the State Machine's IRQ
sm_button.irq(button_handler)
# Activate the state machine
sm_button.active(1)
# Keep the program running
while True:
time.sleep(1)