Author Topic: Phase Shifting a PWM signal  (Read 2503 times)

0 Members and 1 Guest are viewing this topic.

Offline Puffie40Topic starter

  • Regular Contributor
  • *
  • Posts: 54
  • Country: ca
  • Irregular Logic
Phase Shifting a PWM signal
« on: April 13, 2021, 05:53:03 pm »
I have an 24VAC Motor I am trying to drive with a microcontroller.  The motor windings originally used a capacitor to phase shift the signal (Im guessing this would be called a "Run Capacitor").

The motor is quite heavily geared, so it only needs to run at one speed, but it does need to be able to change rotation direction.  I figure two PWM Signals from a PIC16f15245 to a L298 can drive it, but how would I phase shift those signals in the code? 
« Last Edit: April 13, 2021, 05:56:02 pm by Puffie40 »
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12807
Re: Phase Shifting a PWM signal
« Reply #1 on: April 13, 2021, 06:55:24 pm »
You wouldn't.  The CCP modules in that PIC are the 'classic' 10 bit PWM ones and it only has a single timer that can drive them in PWM mode, so offset PWM isn't possible.

You *could* do offset waveform generation using two CCP modules in output compare mode, with a free-running common timer 1 timebase, but that involves doing 16 bit maths in an ISR for each edge to calculate how much to add to the channels CCPRx register to set when the hardware will next flip the output pin.

Maybe look at PICs with motor control 16 bit PWM peripherals?
See TB3137 High-Precision 16-Bit PWM Technical Brief

The alternative that your PIC *might* just be capable of would be software DDS to generate quadrature sinewaves with the PWM module running as fast as you can while still getting enough resolution to output them.   However if you don't want a lot of messing around with high current dual rail supplies, you'll actually need three PWM channels for the three motor terminals.
« Last Edit: April 13, 2021, 08:28:37 pm by Ian.M »
 

Offline Puffie40Topic starter

  • Regular Contributor
  • *
  • Posts: 54
  • Country: ca
  • Irregular Logic
Re: Phase Shifting a PWM signal
« Reply #2 on: April 13, 2021, 07:25:05 pm »
Oof!  Looking around on the internet made me suspect that the PIC I chose wouldn't be able to do it , so that's why I wrote the post.  Thank you for that.

It looks like switching over to a PIC16F1579 would be not have much impact on the design I have.  I'll start poking though the datasheet.
 

Online SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14306
  • Country: fr
Re: Phase Shifting a PWM signal
« Reply #3 on: April 13, 2021, 08:52:34 pm »
Not sure what your MCU would have to do apart from driving the motor, but if it's its main task, you could certainly generate the signals by bit banging instead of using a PWM peripheral, and thus generate exactly what you want.
 

Offline Puffie40Topic starter

  • Regular Contributor
  • *
  • Posts: 54
  • Country: ca
  • Irregular Logic
Re: Phase Shifting a PWM signal
« Reply #4 on: April 13, 2021, 09:44:39 pm »
Not sure what your MCU would have to do apart from driving the motor, but if it's its main task, you could certainly generate the signals by bit banging instead of using a PWM peripheral, and thus generate exactly what you want.
The MCU's only other tasks is to monitor two limit switches and an external control switch.  The other pins are driving the L298 - two enables, four pwm outputs.

so the total is 3 digital inputs, 2 digital outputs, and 4 PWM pins.

I could Bit-Bang it - The motor only needs 50-60hz to run, so a fairly simple MCU could do it.  I do want to try figure these PWM modules out, though.
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5835
  • Country: es
Re: Phase Shifting a PWM signal
« Reply #5 on: April 13, 2021, 09:45:32 pm »
What PWM frequency? Are you going to drive the motor with simple square wave? Or pseudo sine wave?

If it's simple square wave drive, that's easy, just start PWM1, wait the time that corresponds to the phase required, start PWM2.



If it's pesudo sine wave, then update two different pwm timers in their own interrupts.
I understand that the phase shift is fixed. So you only have to make the 360º sine wave.
Make a table with pwm values, and a "phase" variable.
PWM1 interrupts takes next PWM value and updates the phase.
PWM2 interrupts reads the phase and sets the PWM value adding the wanted shift.



I know the pictures are 3-phase but it's the same.
« Last Edit: April 13, 2021, 10:00:03 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline Puffie40Topic starter

  • Regular Contributor
  • *
  • Posts: 54
  • Country: ca
  • Irregular Logic
Re: Phase Shifting a PWM signal
« Reply #6 on: April 13, 2021, 09:53:25 pm »
What PWM frequency? Are you going to drive the motor with simple square wave? Or pseudo sine wave?
The parameters I have is a 60Hz (16.7ms) simple square wave with a %50 duty cycle.  A square wave is not the best for the motor, but at the moment it would be the simplest to set up for a guy who is still shaking the rust off his education :)
 

Offline DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5835
  • Country: es
Re: Phase Shifting a PWM signal
« Reply #7 on: April 13, 2021, 10:08:34 pm »
Then it's super easy!
Assuming you want 90º shift:
- Set the PWM1 and PWM2 timers for those 16.7mS. The period counter will be "x". But don't start any yet.
- Set the PWM values as 50% of the period.
- Start PWM1
- You know the timer counter for 90º will be "x/4". So keep polling the PWM1 counter. When it reaches that value, start PWM2.

Now both PWM are running with a 90º phase shift.

Also, there are pic16/pic18 with motor-specific hardware. You know, complementary PWM generation, dead time, fault detection pin (To latch all PWM outputs low without user intevention).
« Last Edit: April 13, 2021, 10:12:30 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: Puffie40

Offline james_s

  • Super Contributor
  • ***
  • Posts: 21611
  • Country: us
Re: Phase Shifting a PWM signal
« Reply #8 on: April 14, 2021, 12:07:16 am »
Can you just use the original capacitor with a relay to select which winding is fed through the capacitor? It might also work to use an inductor to create a lag and select between the capacitor and inductor to change direction, I've never tried that so I don't know if it would actually work though.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf