0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MicroPython で RP2040 の PIO を使うサンプル その1

Posted at

調べながら,自分なりの使用例をまとめてみました

いろいろな方法でGPIO P25, P16, P17に値を書く

  • set().side() を使う場合
main.py
import machine, rp2

@rp2.asm_pio(
    set_init=
        rp2.PIO.OUT_HIGH, # P25用(BLUE LED)  初期値 H
    sideset_init=(
        rp2.PIO.OUT_HIGH, # P16用(GREEN LED) 初期値 H
        rp2.PIO.OUT_HIGH  # P17用(RED LED)   初期値 H
        )
    )
def prog():
    set(pins, 0b0).side(0b00)
    #        P25=L  P17=L P16=L

state_machine = rp2.StateMachine(
    0, prog,
    set_base=machine.Pin(25),
    sideset_base=machine.Pin(16)
    )

state_machine.active(1)
  • ステートマシンを2つ使う例(あえて mov()使用)
main.py
import machine, rp2

@rp2.asm_pio( out_init=(rp2.PIO.OUT_HIGH,rp2.PIO.OUT_HIGH) )
def prog_0():
    set(x, 0b00) # P17=L P16=L
    mov(pins, x)

state_machine_0 = rp2.StateMachine(0, prog_0,out_base=machine.Pin(16))
state_machine_0.active(1)

@rp2.asm_pio( out_init=rp2.PIO.OUT_HIGH )
def prog_1():
    set(x, 0b0) # P25=L
    mov(pins, x)

state_machine_1 = rp2.StateMachine(1, prog_1,out_base=machine.Pin(25))
state_machine_1.active(1)
  • System から数値を送って GPIO P16, P17に値を書く(P25はside()で指定)
main.py
import machine, rp2

@rp2.asm_pio(
    sideset_init=rp2.PIO.OUT_HIGH, # P25用
    out_init=(rp2.PIO.OUT_HIGH,rp2.PIO.OUT_HIGH), # P16 P17用
    out_shiftdir=rp2.PIO.SHIFT_RIGHT
    )
def prog():
    pull()                # TX FIFO -> OSR
    out(pins, 2).side(0)  # OSR -> pins 2bit, 0 -> P25
    pass

state_machine = rp2.StateMachine(
    0, prog,
    out_base=machine.Pin(16),
    sideset_base=machine.Pin(25)
    )

state_machine.active(1)
state_machine.put(0b00) # P17=L P16=L

備考

XIAO RP2040 を使っています。オンボードの3色LEDが P25(BLUE) P16(GREEN) P17(RED) に割り当てられていて,PIOのデバッグには重宝します。(Lで点灯 Hで消灯)

out() および mov()の出力は,ともに out_init で初期化し,out_base で先頭のピンを指定します。これが最初分からずに苦労しました。参考リンクの筆者さんに感謝です。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?