from machine import Pin
import rp2
import time
PIN_IN = 4
@rp2.asm_pio()
def edge_detector():
in_(pins, 1)
mov(x, isr)
label("loop")
in_(pins, 1)
mov(y, isr)
jmp(x_not_y, "change")
jmp("loop")
label("change")
mov(isr, y)
push()
mov(x, y)
jmp("loop")
sm_in = rp2.StateMachine(0, edge_detector, freq=2000, in_base=Pin(PIN_IN))
sm_in.active(1)
print("检测 GPIO4 边沿...")
try:
while True:
if sm_in.rx_fifo():
level = sm_in.get() & 1
print(f"边沿!当前电平: {level}")
time.sleep_us(100)
except KeyboardInterrupt:
sm_in.active(0)
print("结束")