I am working on a CircuitPython-Grand Central M4 based project where I need to output 12x40 kHz PWM signals with varying duty cycle. Each PWM is assigned a specific output pin and a list of duty cycle values. The PWMs need to be generated quasi simultaneously, at least with minimum delay. In the attached drawing, I illustrated for a few of the output pins what I want to get and what I measured with a scope using the simple code below (the code is just for explanation purposes, the list of duty cycle in the real application contains 100s of values). Well, no need to say that the results are far off the expectations.
There is a large delay in-between the pulses on the different outputs. The "apparent" duty-cycle/frequency of the generated signals are completely messed up. Is the delay mainly due to the internal functioning of the PWMOut class when modifying the duty_cycle attribute? Something like an internal timer that prevents the execution of further instructions in the code before a certain state has been reached? From a more general perspective, would you have any recommendations on how to approach the problem?
Code: Select all
# libraries
import board
import pulseio
#Output PWM frequency
fPWM=40000
#Output PWM pins and signals
pwm_pins=[board.A1, board.A2, board.A12,board.A15,board.D32, board.D34,
board.D38,board.D39,board.D14, board.D15, board.D27,board.D28]
pwm_signals=[pulseio.PWMOut(i, frequency = fPWM) for i in pwm_pins]
#Dummy values of the duty cycles for the PWM signals. Each signal is assigned a duty cycle list with length 'len_dc_list'
len_dc_list=2
pwm_dc_list=[[50+10*j+20*i for i in range(0,len_dc_list,1)] for j in range(0,len(pwm_pins),1)]
#Loop varying duty cycles of the output PWM pins
for j in range(0,len_dc_list,1):
for i in range(0,len(pwm_signals),1):
pwm_signals[i].duty_cycle=pwm_dc_list[i][j]*2**8
pwm_signals[i].duty_cycle=0