cancel
Showing results for 
Search instead for 
Did you mean: 

Phase-shift PWM with STM32F407

jantje7600
Associate
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
10 REPLIES 10
Omar BOUZOURRAA
Associate II
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);