Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1251

Python • Raspberry pi pico w - micropython - capture PWM parameters

$
0
0
Hello,

I am having pi pico w. I generating PWM signal on one GPIO and I am trying to capture its parameters on another GPIO via PIO and micropython. The issue that I have is that I get only zeroes for all parameters. I tried tuning the frequencies of the PWM signal and of the PIO but no result. I will be glad if someone can help me. Here is the script:

Code:

from machine import Pin, PWM, freqimport timeimport rp2#set PWM on GPIO 0pwm = PWM(Pin(0)) pwm.freq(1000)pwm.duty_u16(30000)# Define a PIO program to measure PWM signal@rp2.asm_pio(set_init=rp2.PIO.IN_LOW)def pwm_measure():    wait(1, pin, 0)            # Wait for the first rising edge    set(x, 0)                  # Reset counter x (used for high time)    label("high_time")    jmp(pin, "high_time")      # Loop until the pin goes low    mov(isr, x)                # Store high time (x) in ISR    push()                     # Push high time to FIFO    wait(1, pin, 0)            # Wait for the next rising edge    set(x, 0)                  # Reset counter x (used for period)    label("period_time")    jmp(pin, "period_time")    # Loop until the pin goes high again    mov(isr, x)                # Store period (x) in ISR    push()                     # Push period to FIFO# Setup the state machinesm = rp2.StateMachine(0, pwm_measure, freq=1000000, in_base=Pin(3))# Start the state machinesm.active(1)try:    while True:        if sm.rx_fifo():            high_time = sm.get()  # Get high time from FIFO            period = sm.get()     # Get period from FIFO            if period > 0:                duty_cycle = (high_time / period) * 100                frequency = 1000000 / period  # Convert period to frequency            else:                duty_cycle = 0                frequency = 0            print("High Time (counts):", high_time)            print("Period (counts):", period)            print("Duty Cycle: {:.2f}%".format(duty_cycle))            print("Frequency: {:.2f} Hz".format(frequency))        time.sleep(1)        except KeyboardInterrupt:    sm.active(0)    print("Stopped by user")

Statistics: Posted by toshe — Mon Aug 12, 2024 7:15 am



Viewing all articles
Browse latest Browse all 1251

Trending Articles