I would like to measure the time of a pulse in low level. So I used the PWM example in order to generate a pulse on the LED. And I wrote a PIO script that first waits for the pin being low and then decrements scratch register x untkl the pin goes high. At least this was the intention of this script. But it does not work, I hope that somebody can give me a hint what I did wrong or misunderstood.
Many thanks for any help!
KR,
Ch.
Code: Select all
# Example using PWM to fade an LED.
import time
import rp2
from machine import Pin, PWM
# measure time between falling edges
@rp2.asm_pio(set_init=rp2.PIO.IN_HIGH)
def perMeas():
label('mainloop')
set(x,0)
wait(0, pin, 0)
label('lo_loop')
jmp(x_dec,'next')
label('next')
jmp(pin,'hi')
jmp('lo_loop')
label('hi')
mov(isr,x)
push()
jmp('mainloop')
# Construct PWM object, with LED on Pin(25).
pwm = PWM(Pin(25))
# Set the PWM frequency.
pwm.freq(10)
# Fade the LED in and out a few times.
duty = 10
direction = 1
# for _ in range(8 * 256):
sm4 = rp2.StateMachine(1, perMeas, freq=200000, in_base=Pin(25)) # pin16 used
sm4.active(1)
while True:
duty += direction
if duty > 255:
duty = 255
direction = -1
elif duty < 0:
duty = 0
direction = 1
pwm.duty_u16(duty * duty)
time.sleep(1.0)
while sm4.rx_fifo():
print(2**32-sm4.get(),end=' ')
else:
print('empty',end=' ')
print()