Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Issue phase shifting PWM to different pins
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Issue phase shifting PWM to different pins

by grieder on Tue Jul 07, 2020 8:30 pm

Hello,
I am working with a circuitpython on a Feather M4 Express.
I would like to output 4 square waves at 100 Hz phase shifted 90 degrees on 4 pins.

pin 1 : 0 degree phase shift
pin 2 : 90 degree phase shift
pin 3 : 180 degree phase shift
pin 4 : 270 degree phase shift

Here is the code I am using to try to accomplish this goal.

Code: Select all | TOGGLE FULL SIZE
def set_up(pin):
    dio = pulseio.PWMOut(pin, duty_cycle=(65535 // 2), frequency=100)
    microcontroller.delay_us(12500)
    return dio

pins = [board.A1, board.A3, board.D11, board.D9, board.D13]

for i in pins:
    pwm_list.append(set_up(i))



When checking the output with an oscilloscope I found it very weird that if i don't use these specific pins some of the waveforms are not shifted at all(i.e if i use pin D9 and pin D10 they would be in phase instead of shifted)

I have a lot of open pins so i was able to switch around a good amount to find this combination that worked.
Unfortunately, I have some overlap between waveforms that should be shifted 180 degrees.

Looking at the documentation it seems I should be able to output up to 8 waveforms from a single counter (if it is a TCC counter)
I have looked but cannot find documentation on how to shift waveforms from the same counter, as I believe this would fix my problem.

Maybe PWM isn't the write method to use for my goal, but i cannot find another method of producing these square waves.

Any help is greatly appreciated!!

Thank you,
grieder

grieder
 
Posts: 2
Joined: Tue Jul 07, 2020 8:05 pm

Re: Issue phase shifting PWM to different pins

by kevinjwalters on Wed Jul 08, 2020 8:27 am

I've not tried this or if I have I've forgotten if it works well but there's PulseOut. If you can get them in sync then that might work to let you define pulses (with carrier having a duty_cycle of 65535) to give you the precise phases you want.

kevinjwalters
 
Posts: 739
Joined: Sun Oct 01, 2017 3:15 pm

Re: Issue phase shifting PWM to different pins

by kevinjwalters on Wed Jul 08, 2020 8:48 am

Hmm, I've realised that doesn't work because you can only send one block of pulses at a time and there's no repeat/looping feature.

kevinjwalters
 
Posts: 739
Joined: Sun Oct 01, 2017 3:15 pm

Re: Issue phase shifting PWM to different pins

by tannewt on Wed Jul 08, 2020 2:51 pm

CircuitPython doesn't currently have an explicit way of doing this.

I think you are managing by finding pins which allocate to different timers and therefore, they start at zero at different times. You can do this a bit easier by setting `variable_frequency=True`. That will allocate a different timer for each PWMOut because they can each change the top value of the timer to change their frequency.

Please beware that CircuitPython's execution time can vary and potentially mess up your delay before starting the next PWMOut.

tannewt
 
Posts: 1789
Joined: Thu Oct 06, 2016 8:48 pm

Re: Issue phase shifting PWM to different pins

by grieder on Wed Jul 08, 2020 5:01 pm

tannewt wrote:CircuitPython doesn't currently have an explicit way of doing this.

I think you are managing by finding pins which allocate to different timers and therefore, they start at zero at different times. You can do this a bit easier by setting `variable_frequency=True`. That will allocate a different timer for each PWMOut because they can each change the top value of the timer to change their frequency.

Please beware that CircuitPython's execution time can vary and potentially mess up your delay before starting the next PWMOut.


Thank you, I figured it had to do with which timers were being used.
It seems to me that it would be more reliable setting all the waves from the same timer.

Do you know if this is possible in MicroPython or would I need to write something in C to make this robust?

grieder
 
Posts: 2
Joined: Tue Jul 07, 2020 8:05 pm

Re: Issue phase shifting PWM to different pins

by tannewt on Thu Jul 09, 2020 11:51 am

I don't think it's possible in MicroPython and don't know if it's possible from the C level either. I'd suggest checking the TCC section of the SAMD51 data sheet to see if phase shifting is possible from one TCC. On the SAMD21, thea had to use two TCCs: https://blog.thea.codes/phase-shifted-pwm-on-samd/

tannewt
 
Posts: 1789
Joined: Thu Oct 06, 2016 8:48 pm

Re: Issue phase shifting PWM to different pins

by kevinjwalters on Wed Aug 05, 2020 7:10 pm

Is the nRF52840 any different with controlling or specifying phase for PWM? I'm just looking at pin 5 and 6 on a CLUE and they don't look in phase to me for a PWM running at 25kHz.

kevinjwalters
 
Posts: 739
Joined: Sun Oct 01, 2017 3:15 pm

Re: Issue phase shifting PWM to different pins

by tannewt on Mon Aug 10, 2020 6:13 pm

It's probably different because it's a different platform and phase isn't part of the common API. I didn't implement it so I don't know the details. The implementation is here: https://github.com/adafruit/circuitpyth ... o/PWMOut.c

tannewt
 
Posts: 1789
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.