2021-01-20 08:34:08 -05:00
|
|
|
# Example using PIO to blink an LED and raise an IRQ at 1Hz.
|
2023-02-06 23:03:14 -05:00
|
|
|
# Note: this does not work on Pico W because it uses Pin(25) for LED output.
|
2021-01-20 08:34:08 -05:00
|
|
|
|
|
|
|
import time
|
|
|
|
from machine import Pin
|
|
|
|
import rp2
|
|
|
|
|
|
|
|
|
|
|
|
@rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
|
|
|
|
def blink_1hz():
|
|
|
|
# fmt: off
|
|
|
|
# Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000
|
|
|
|
irq(rel(0))
|
|
|
|
set(pins, 1)
|
|
|
|
set(x, 31) [5]
|
|
|
|
label("delay_high")
|
|
|
|
nop() [29]
|
|
|
|
jmp(x_dec, "delay_high")
|
|
|
|
|
2022-12-15 12:32:10 -05:00
|
|
|
# Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000
|
|
|
|
nop()
|
2021-01-20 08:34:08 -05:00
|
|
|
set(pins, 0)
|
2022-12-15 12:32:10 -05:00
|
|
|
set(x, 31) [5]
|
2021-01-20 08:34:08 -05:00
|
|
|
label("delay_low")
|
|
|
|
nop() [29]
|
|
|
|
jmp(x_dec, "delay_low")
|
|
|
|
# fmt: on
|
|
|
|
|
|
|
|
|
|
|
|
# Create the StateMachine with the blink_1hz program, outputting on Pin(25).
|
|
|
|
sm = rp2.StateMachine(0, blink_1hz, freq=2000, set_base=Pin(25))
|
|
|
|
|
|
|
|
# Set the IRQ handler to print the millisecond timestamp.
|
|
|
|
sm.irq(lambda p: print(time.ticks_ms()))
|
|
|
|
|
|
|
|
# Start the StateMachine.
|
|
|
|
sm.active(1)
|