Download Print this page

Raspberry Pi Pico Python SDK Manual page 21

A micropython environment for rp2040 microcontrollers

Advertisement

Raspberry Pi Pico Python SDK
18
19
20
rp2.PIO(0).irq(lambda
21
22 sm = rp2.StateMachine(0, irq_test, freq=1000)
23 sm.active(1)
24 time.sleep(1)
25 sm.active(0)
An example program that blinks at 1Hz and raises an IRQ at 1Hz to print the current millisecond timestamp,
Pico MicroPython Examples:
 1
# Example using PIO to blink an LED and raise an IRQ at 1Hz.
 2
 3
import
 4
from
 5
import
 6
 7
 8 @rp2.asm_pio(set_init=rp2.PIO.OUT_LOW)
 9
def
blink_1hz():
10
# Cycles: 1 + 1 + 6 + 32 * (30 + 1) = 1000
11
irq(rel(0))
12
set(pins, 1)
13
set(x, 31)
14
label("delay_high")
15
nop()
16
jmp(x_dec, "delay_high")
17
18
# Cycles: 1 + 7 + 32 * (30 + 1) = 1000
19
set(pins, 0)
20
set(x, 31)
21
label("delay_low")
22
nop()
23
jmp(x_dec, "delay_low")
24
25
26
# Create the StateMachine with the blink_1hz program, outputting on Pin(25).
27 sm = rp2.StateMachine(0, blink_1hz, freq=2000, set_base=Pin(25))
28
29
# Set the IRQ handler to print the millisecond timestamp.
30
sm.irq(lambda
31
32
# Start the StateMachine.
33 sm.active(1)
or to wait for a pin change and raise an IRQ.
Pico MicroPython Examples:
 1
# Example using PIO to wait for a pin change and raise an IRQ.
 2
#
 3
# Demonstrates:
 4
#
- PIO wrapping
 5
#
- PIO wait instruction, waiting on an input pin
 6
#
- PIO irq instruction, in blocking mode with relative IRQ number
 7
#
- setting the in_base pin for a StateMachine
 8
#
- setting an irq handler for a StateMachine
 9
#
- instantiating 2x StateMachine's with the same program and different pins
3.9. PIO Support
pio: print(pio.irq().flags()))
https://github.com/raspberrypi/pico-micropython-examples/tree/master/pio/pio_1hz.py
time
machine
import
Pin
rp2
p: print(time.ticks_ms()))
https://github.com/raspberrypi/pico-micropython-examples/tree/master/pio/pio_pinchange.py
[5]
[29]
[6]
[29]
Lines 1 - 33
Lines 1 - 46
20

Advertisement

loading
Need help?

Need help?

Do you have a question about the Pico Python SDK and is the answer not in the manual?

Subscribe to Our Youtube Channel

This manual is also suitable for:

Pico seriesPico 2