Skip to main content
jantje7600
Visitor II
February 27, 2017
Question

Phase-shift PWM with STM32F407

  • February 27, 2017
  • 10 replies
  • 3150 views
Posted on February 27, 2017 at 15:10

How can I create a phase-shifted pwm signal ? The frequency is fixed. The board is STM32F407 discovery board so I am not 100% sure if this is possible.

If it is, how can I achieve this?

#phase-shift #stm32f4-discovery
This topic has been closed for replies.

10 replies

waclawek.jan
Super User
February 27, 2017
Posted on February 27, 2017 at 15:34

Phase shifted, but referenced to what?

JW

Tesla DeLorean
Guru
February 27, 2017
Posted on February 27, 2017 at 17:11

Seem to recall toggle mode being used to control relative phase across different channels.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
waclawek.jan
Super User
February 27, 2017
Posted on February 27, 2017 at 22:37

Yes, but sort of rigid if you want to call it PWM...

JW

PS. Still don't know phase shift to what, external input, other pwm, what freq range etc.etc. Hate to polish the crystal ball.

Tesla DeLorean
Guru
February 27, 2017
Posted on February 27, 2017 at 22:55

Signs point to multi-phase output

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
dylan
Associate II
May 17, 2017
Posted on May 17, 2017 at 11:30

I am interested in this too, was this ever resolved? I would like to double edge PWM, is this possible with STM32? I have tried to do it using asymmetric centre-aligned PWM, but I cannot figue out how to define the second CCRx value (if that is what it is called). A visual reference of what I am trying to achieve:

0690X000006072DQAQ.png

Thanks.

waclawek.jan
Super User
May 17, 2017
Posted on May 17, 2017 at 12:11

Newer STM32 TIM have features to achieve this within one timer, but here it calls for two timers in master-slave arrangement, with the same compare configuration for the channel providing the output, master having one more compare channel set up to provide a trigger out at the 'offset', slave being reset from master.

It may need some tweaking when the offset changes to prevent spurious pulses.

JW

dylan
Associate II
May 17, 2017
Posted on May 17, 2017 at 12:27

Hi

Waclawek.Jan

‌,

Thanks for the response. I have the STM32F446RET6, does this have have the TIM features you speak of, if so could you please refer me to which timer supports this please? I also have a STM32L476RGT6U that I can use to achieve this.

Thanks,

Dylan

waclawek.jan
Super User
May 17, 2017
Posted on May 17, 2017 at 12:56

Look for 'Asymmetric PWM mode' and/or better 'Combined PWM mode' in the narrative of TIM chapters.

ST tends to maintain the version of IP blocks within a sub-family (although, unfortunately, they don't publish this info, at least not in a concise form, only as configuration for the idiotic CubeMX) so F446 probably no and L476 yes.

JW

dylan
Associate II
May 17, 2017
Posted on May 17, 2017 at 13:19

Great, I will look into that and report back, thanks

Waclawek.Jan

‌!

Omar BOUZOURRAA
Associate II
June 1, 2017
Posted on June 01, 2017 at 04:16

you can generate phase-shifted PWM signals with 2 different ways :

1) You can use the feature : Output toggle which can generate up to 4 phase-shifted PWM signals with controlled shift but all signals have the same duty-cycle which is 50%.

2) You can as a second alternative generate 2 phase-shifted PWM signals with controlled shift and duty-cycle : of course, signals have the same frequency which is the mainTimer frequency.

For the mcrocontroller STM32F4xx only output toggle is possible !!! The cofiguration is analog to the following one . there is only two differences :

1) the configuration of the CCMRx registers

2) the configuration of the CCRxregisters

Please refer to the reference manual for futrher information!!

NB : In both modes (asymmetric or output toggle) frequency is computed with the same following way :

/*Timer_frequency = APB1_frequency/((1 + prescaler value)(1 + Timer period)*2

Here you can find a configuration of the asymmetric PWM mode in the STM32F7 microcontroller :

For further information you can look at the reference manual the application note and the datasheet. You can make the same configuration in your STM32F334 but you should look before at the datasheet to configure pins correctly.

/*************************** Author : Omar Bouzourraa ********************/

/*!< At this stage I am going to configure TIM4 in PWM output Asymmetric mode

using registers' direct access.

In the datasheet of the stm32f7xx microcontroller, user can find that

the TIM4 channels are connected to PB6,PB7,PB8,PB9 and can be remapped

to PD12,PD13,PD14,PD15. In this program, TIM4 channels will be connected

to their default pins.

!!!! Source clock for the counter is the internal main clock coming from RCC

*/

/* Enabling GPIOB by configuring RCC_AHB1ENR which enables the GPIOB clock*/

RCC->AHB1ENR |= 1<<1;

/* Enabling TIM4 clock by configuring ther RCC-APB1ENR */

RCC->APB1ENR |= 1<<2;

/*************************************************************************/

/* selecting AF mode*/

GPIOB->MODER |= (2<<(2*6)) + (2<<(2*7)) + (2<<(2*8)) + (2<<(2*9));

/* Selecting output type : push-pull is selected in order to have more current in output*/

GPIOB->OTYPER = 0;

/* Selecting Output Speed : very high speed */

GPIOB->OSPEEDR = (3<<(2*6)) + (3<<(2*7)) + (3<<(2*8)) + (3<<(2*9));

/* TIM4 is connected to Alternate function 2*/

/* Connectinng GPIOB : PB6,PB7,PB8,PB9 to alternate function 2*/

GPIOB->AFR[0] = (2<<(4*6)) + (2<<(4*7));

GPIOB->AFR[1] = (2<<(4*0)) + (2<<(4*1));

/**************************************************************************/

/* Configuration of TIM4 Time-base*/

/*Timer_frequency = APB1_frequency/((1 + prescaler value)(1 + Timer period)*2

!!! When Asymmetric mode is configured,

*/

/* period configuration*/

TIM4->ARR = 0xFFFF;

/* Prescaler configuration*/

TIM4->PSC = 0;

/* configuration of CC1 in channel 1 : PB6*/

TIM4->CCR1 = (int)(0.4*TIM4->ARR);

/* configuration of CC2 in channel 2 : PB7*/

TIM4->CCR2 = (int)(0.6*TIM4->ARR);

/* configuration of CC3 in channel 3 : PB8*/

TIM4->CCR3 = (int)(0.8*TIM4->ARR);

/* configuration of CC4 in channel 4 : PB9*/

TIM4->CCR4 = (int)(0.8*TIM4->ARR);

/*

PWM in channel1 : PB6 will have a duty-cycle (0.4 + 0.6)/2 = 0.5 = 50%

PWM in channel3 : PB8 will have a duty-cycle (0.8 + 0.8)/2 = 0.6 = 60%

Signal in channel3 is shifted with an angle theta = (0.8 - 0.4)*360 = 144°

*/

/*************************************************************************/

/* Timer mode and counter configuration */

/* Enable TIM4 counter and configure counting mode as center-aligned mode1 : mode1,mode2,or mode3 will give the same wave form */

TIM4->CR1 |= 1 + (1<<5);

/* Configure TIM4_CH1,CH2,CH3 and CH4 as output and configure the asymmetric mode1 : Actually there is also mode2 */

TIM4->CCMR1 |= (0xE<<12) + (0xE<<4); //TIM_CH1 and CH2 config

TIM4->CCMR2 |= (0xE<<12) + (0xE<<4); //TIM_CH3 and CH4 config

/* Enable output on CH1,CH2,CH3 and CH4 and configure polarity as high in all channels*/

TIM4->CCER |= (1<<0) + (1<<4) +(1<<8) +(1<<12);