|
| 1 | +import time |
| 2 | +import rp2 |
| 3 | +from machine import Pin |
| 4 | + |
| 5 | +# Create a PIO program to read a number, wait for 10 cycles, and then return it |
| 6 | +@rp2.asm_pio( |
| 7 | + in_shiftdir=rp2.PIO.SHIFT_LEFT, |
| 8 | + out_shiftdir=rp2.PIO.SHIFT_LEFT, |
| 9 | +) |
| 10 | + |
| 11 | +def wait_prog(): |
| 12 | + pull(block) # Pull a number from the FIFO into the OSR |
| 13 | + mov(x, osr) # Move the value from the OSR to the X scratch register |
| 14 | + nop() [9] # Wait for 10 cycles |
| 15 | + mov(isr, x) # Move the value from the X scratch register to the ISR |
| 16 | + push() # Push the value from the ISR to the FIFO |
| 17 | + |
| 18 | +# Instantiate a state machine with the wait program, at 2000Hz |
| 19 | +sm = rp2.StateMachine(0, wait_prog, freq=2000) |
| 20 | + |
| 21 | +# Activate the state machine |
| 22 | +sm.active(1) |
| 23 | + |
| 24 | +# Send a number to the state machine |
| 25 | +sm.put(123) |
| 26 | +sm.put(578) |
| 27 | +sm.put(9999) |
| 28 | +sm.put(1111) |
| 29 | +sm.put(2222) |
| 30 | + |
| 31 | +# Wait for a moment to let the state machine process the number |
| 32 | +#time.sleep(0.01) |
| 33 | + |
| 34 | +if False: |
| 35 | + # Get the result back from the state machine |
| 36 | + print("Result:", sm.get()) # Will print: Result: 123 |
| 37 | + # Get the result back from the state machine |
| 38 | + print("Result:", sm.get()) # Will print: Result: 578 |
| 39 | + # Get the result back from the state machine |
| 40 | + print("Result:", sm.get()) # Will print: Result: 9999 |
| 41 | + # Get the result back from the state machine |
| 42 | + print("Result:", sm.get()) # Will print: Result: 1111 |
| 43 | + # Get the result back from the state machine |
| 44 | + print("Result:", sm.get()) # Will print: Result: 2222 |
| 45 | +else: |
| 46 | + while True: |
| 47 | + # if data is available |
| 48 | + if not sm.empty(): |
| 49 | + data = sm.get() |
| 50 | + print("Data received:", data) |
| 51 | + #await asyncio.sleep(0) # Yield to other tasks |
| 52 | + |
| 53 | +# Deactivate the state machine |
| 54 | +sm.active(0) |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
0 commit comments