PIO issue #19693
Unanswered
mjrx
asked this question in
Hardware & Peripherals
PIO issue
#19693
Replies: 1 comment 1 reply
|
You need to set the #!/micropython
# vim: fileencoding=utf-8: ts=4: sw=4: expandtab:
from machine import Pin
from rp2 import PIO,StateMachine,asm_pio,bootsel_button as button
from time import sleep_ms
#PIO program: continuously read input pin and set output pin accordingly
@asm_pio(set_init=PIO.OUT_HIGH) # Output pin starts HIGH
def mirror_pin_jmp():
wrap_target()
jmp (pin, "high") # If input pin is HIGH, jump to 'high'
label ("low")
mov (x, pins)
mov (isr, x)
push ()
set (pins, 0) # Otherwise, set output LOW
jmp ("end") # Skip the HIGH set
label ("high")
mov (x, pins)
mov (isr, x)
push ()
set (pins, 1) # Set output HIGH
label ("end")
nop () [5] # Small delay (debounce-friendly)
wrap ()
# Pin configuration
# INPUT_PIN = 6 # GP6 as input, but it always reponds to GPIO0
# OUTPUT_PIN = 16 # GP16, 17 0r 25 as output, RED=17, GRN=16, BLU=25
INPUT_PIN = 16 # DEBUG
OUTPUT_PIN = 25 # DEBUG LED
# Create state machine
sm = StateMachine(
0, mirror_pin_jmp,
freq=5000,
set_base=OUTPUT_PIN,
jmp_pin=INPUT_PIN
)
# Start the state machine
sm.restart()
sm.active(True)
print(f"Mirroring GP{INPUT_PIN} -> GP{OUTPUT_PIN} using PIO. Press Ctrl+C to stop.")
try:
old = -1
while not button():
sleep_ms(100)
new = sm.get()
if new != old:
old = new
print('\n', bin(new))
else:
print('.',end='')
except KeyboardInterrupt:
pass
sm.active(False)
del sm
print("Stopped PIO state machine...")
outpin = Pin(OUTPUT_PIN, Pin.OUT, value=0) |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
No matter what I set the in_base to in this program, it only responds to GPIO0. I'm stumped. The set_base works OK, I can change that to any output.
rom machine import Pin
import rp2
import time
PIO program: continuously read input pin and set output pin accordingly
@rp2.asm_pio(set_init=rp2.PIO.OUT_HIGH) # Output pin starts HIGH
def mirror_pin_jmp():
wrap_target()
jmp(pin, "high") # If input pin is HIGH, jump to 'high'
set(pins, 0) # Otherwise, set output LOW
mov(x, pins)
mov(isr, x)
push()
jmp("end") # Skip the HIGH set
label("high")
mov(x, pins)
mov(isr, x)
push()
set(pins, 1) # Set output HIGH
label("end")
nop() [5] # Small delay (debounce-friendly)
wrap()
Pin configuration
INPUT_PIN = 6 # GP6 as input, but it always reponds to GPIO0
OUTPUT_PIN = 16 # GP16, 17 0r 25 as output, RED=17, GRN=16, BLU=25
Create state machine
sm = rp2.StateMachine(
0, mirror_pin_jmp,
freq=5000, # Lower frequency for debounce (20 µs per loop)
in_base=Pin(INPUT_PIN, Pin.IN, Pin.PULL_UP),
set_base=Pin(OUTPUT_PIN, Pin.OUT)
)
Start the state machine
sm.active(1)
print(f"Mirroring GP{INPUT_PIN} -> GP{OUTPUT_PIN} using PIO. Press Ctrl+C to stop.")
try:
while True:
time.sleep(1) # CPU idle, PIO runs in hardware
print(sm.get())
except KeyboardInterrupt:
sm.active(0)
print("Stopped PIO state machine...")
All reactions