ตัวอย่างนี้สาธิตการเขียนโค้ดเพื่อให้ PIO คอยตรวจสอบอินพุตแล้วให้เอาต์พุตเปลี่ยนสถานะของเอาต์พุตตามสถานะของอินพุตที่อ่านเข้ามา และใช้วงจรปุ่มกดสร้างสัญญาณอินพุต ไม่กดได้ลอจิก High และแต่ถ้ากดปุ่มค้างไว้จะได้ลอจิก Low
import utime as time
from rp2 import PIO, asm_pio, StateMachine
from machine import Pin
@asm_pio( set_init=PIO.OUT_LOW )
def pio_test():
wrap_target()
set(pins,1) # 1-cycle: drive output pin to 1
set(pins,0) # 1-cycle: drive output pin to 0
wrap() # 0-cycle: unconditional jump to wrap target
# create an instance of StateMachine 0 (SM0)
sm = StateMachine(0, pio_test, freq=2_000_000, set_base=Pin(14) )
sm.active(1) # run the SM0
try:
while True:
time.sleep_ms(10)
except KeyboardInterrupt:
pass
finally:
sm.active(0) # stop SM0
import utime as time
from rp2 import PIO, asm_pio, StateMachine
from machine import Pin
@asm_pio( sideset_init=PIO.OUT_LOW )
def test():
wrap_target()
nop() .side(1) # drive side pin high
nop() .side(0) # drive side pin low
wrap()
sm = StateMachine(0, test, freq=2_000_000, sideset_base=Pin(14) )
sm.active(1) # run the SM0
try:
while True:
time.sleep_ms(10)
except KeyboardInterrupt:
pass
finally:
sm.active(0) # stop SM0
import utime as time
from rp2 import PIO, asm_pio, StateMachine
from machine import Pin
@asm_pio( set_init=(PIO.OUT_LOW) )
def pio_test():
label('loop')
set(pins,1) # 1-cycle: drive output pin to 1
set(pins,0) # 1-cycle: drive output pin to 0
jmp('loop') # 1-cycle: unconditional jump
# 5MHz/3 = 1.667 MHz
sm = StateMachine(0, pio_test, freq=5_000_000, set_base=Pin(14) )
sm.active(1)
try:
while True:
time.sleep_ms(10)
except KeyboardInterrupt:
pass
finally:
sm.active(0)
import utime as time
from rp2 import PIO, asm_pio, StateMachine
from machine import Pin
@asm_pio( set_init=(PIO.OUT_LOW) )
def pio_test():
label('loop')
set(pins,1) [1] # 2 cycles: drive output pin to 1
set(pins,0) [1] # 2 cycles: drive output pin to 0
jmp('loop') # 1 cycle: unconditional jump
# output frequency: 5MHz/5 = 1MHz
sm = StateMachine(0, pio_test, freq=5_000_000, set_base=Pin(14) )
sm.active(1)
try:
while True:
time.sleep_ms(10)
except KeyboardInterrupt:
pass
finally:
sm.active(0)
import utime as time
from rp2 import PIO, asm_pio, StateMachine
from machine import Pin
@asm_pio( set_init=(PIO.OUT_LOW), sideset_init=(PIO.OUT_LOW) )
def pio_test():
wrap_target()
set(pins,1) .side(0) # GP14=1, GP15=0
set(pins,0) .side(1) # GP14=0, GP15=1
wrap()
sm = StateMachine(0, pio_test, freq=2_000_000,
set_base=Pin(14), sideset_base=Pin(15) )
sm.active(1)
try:
while True:
time.sleep_ms(10)
except KeyboardInterrupt:
pass
finally:
sm.active(0)
import utime as time
from rp2 import PIO, asm_pio, StateMachine
from machine import Pin
@asm_pio( set_init=(PIO.OUT_LOW) )
def pio0():
wait(0, pin, 0) # wait for input button goes low
nop() [0]
wrap_target()
set(pins,1)
set(pins,0) [2]
wrap()
@asm_pio( set_init=(PIO.OUT_LOW) )
def pio1():
wait(0, pin, 0) # wait for input button goes low
nop() [1]
wrap_target()
set(pins,1)
set(pins,0) [2]
wrap()
@asm_pio( set_init=(PIO.OUT_LOW) )
def pio2():
wait(0, pin, 0) # wait for input button goes low
nop() [2]
wrap_target()
set(pins,1)
set(pins,0) [2]
wrap()
@asm_pio( set_init=(PIO.OUT_LOW) )
def pio3():
wait(0, pin, 0) # wait for input button goes low
nop() [3]
wrap_target()
set(pins,1)
set(pins,0) [2]
wrap()
button = Pin(14, Pin.IN, Pin.PULL_UP)
pins = [Pin(13),Pin(12),Pin(11),Pin(10)]
pio_funcs = [pio0, pio1, pio2, pio3]
sm_list = []
for i in range(4):
sm_list.append( StateMachine(i,
pio_funcs[i], freq=2_000_000,
in_base=button, set_base=pins[i] ) )
for sm in sm_list:
sm.active(1)
try:
while True:
time.sleep_ms(10)
except KeyboardInterrupt:
pass
finally:
for sm in sm_list:
sm.active(0)
import utime as time
from rp2 import PIO, asm_pio, StateMachine
from machine import Pin
@asm_pio( set_init=(PIO.OUT_HIGH) )
def pio_test():
wrap_target()
label('start')
jmp(pin,'drive_high') # check jump pin for branch
set(pins,0) # drive output low
jmp('start')
label('drive_high')
set(pins,1) # drive output pin
wrap()
in_pin = Pin(14, Pin.IN, Pin.PULL_UP )
out_pin = Pin(15, Pin.OUT )
sm = StateMachine(0)
sm.init( pio_test, freq=5_000_000,
set_base=out_pin, jmp_pin=in_pin )
sm.active(1) # start the SM
try:
while True:
time.sleep_ms(10)
except KeyboardInterrupt:
pass
finally:
sm.active(0)
import utime as time
from rp2 import PIO, asm_pio, StateMachine
from machine import Pin
@asm_pio( out_shiftdir=PIO.SHIFT_RIGHT,
in_shiftdir=PIO.SHIFT_LEFT,
out_init=PIO.OUT_LOW )
def pio_test():
wrap_target()
in_(pins,1) # shift input pin into ISR
mov(osr,isr) # copy from ISR to OSR
out(pins,1) # shift data from OSR to output pin
wrap() # jump to wrap target
# create an instance of StateMachine 0 (SM0)
sm = StateMachine(0, pio_test, freq=15_000_000,
out_base=Pin(15), in_base=Pin(14) )
sm.active(1) # run the SM0
try:
while True:
time.sleep_ms(10)
except KeyboardInterrupt:
pass
finally:
sm.active(0) # stop SM0