cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-F030R8 PWM and Phase shift PWM

Bhavik
Associate III

Hello All,

I want to generate a different type of PWM along with its phase shift PWM as well. Below is the diagram for the output waveform. I am using the NUCLEO-F030R8 board as of now.


_legacyfs_online_stmicro_images_0693W00000bl9YJQAY.png(Sorry for the bad drawing)

Where Fhz and Th are the same for all instances as shown on the image, they also need to be changed on run time.

Waiting for assistance.

Thanks

23 REPLIES 23

I don't know what's "phase shift" in this waveform, but one way how to generate such waveform is to run two timers in master-slave interconnection - slave would generate output using PWM at the higher fHz frequency; master would generate PWM (without outputting it to a pin - unless you want to check it) which determines the "envelope"; slave would have set the slave-mode controller in TIMx_SMCR in Gated mode.

JW

Thanks @Community member​ for reply.


_legacyfs_online_stmicro_images_0693W00000dJvcfQAC.pngSo actual wave which I am trying to generate is similar to this.

Where the frequency and duty cycle are the same only difference is a phase shift.

I still don't understand: you want to generate both signals on two pins of the STM32? And the phase shift is variable somehow? Also, there lower signal on the left-hand side is shifted to the right, whereas on the right-hand side is shifted to the left, or there is one more pulse within the bunch, can you comment on that?

At any case, you should start with generating one of the signals, as I've described above.

JW

HA.5
Associate III

in your case you can use 2 or 3 timers, for example if you want 3 PWM signals with 3 different Phases you should use 3 Timers, for example Timer1 is Master for Timer2 and Timer 2 is Master for Timer3. so in this cas you can have 3 PWM Signals on 3 Pins on your board with 3 different Phases. i did it for 2 Phases, but 3 Phases is exactly like 2, only you need one more Timer

@Community member the image which i attached is cropped and is just to represent what i ment by phase shift. Let me clearify that yes it is just shifted a bit.

Nop I am not able to generate the any of the single. But i am able to generate a continuous PWM that's it.

(Note: it is just a cropped image and just for representing purpose it is not the exact one.


_legacyfs_online_stmicro_images_0693W00000dJwMiQAK.png

Bhavik
Associate III

@HA.5 Thanks,

i just want a 2 phase with 180° phase shift. Is it possible to achive it with single timer. If yes it will help me a lot. Because what i am trying to do required two many different PWMs.

HA.5
Associate III

yes, if you want to use only one Timer to reach that, it is possible too

HA.5
Associate III

for example here i use TIM2 to produce that, my clock frequency is 64MHZ and my ARR( htim2.Init.Period = 1000;) 1000, so it reduces my frequency to 64KHZ, in this case i have two pwm signal with the same frequency, but different duty cycles to have 90 degree phase shift.

while(1)

{

 HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);

HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);

}

static void MX_TIM2_Init(void)

{

 /* USER CODE BEGIN TIM2_Init 0 */

 /* USER CODE END TIM2_Init 0 */

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 TIM_OC_InitTypeDef sConfigOC = {0};

 /* USER CODE BEGIN TIM2_Init 1 */

 /* USER CODE END TIM2_Init 1 */

 htim2.Instance = TIM2;

 htim2.Init.Prescaler = 0;

 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim2.Init.Period = 1000;

 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

 if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)

 {

   Error_Handler();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

 {

   Error_Handler();

 }

 sConfigOC.OCMode = TIM_OCMODE_PWM1;

 sConfigOC.Pulse = 500;

 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

 if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)

 {

   Error_Handler();

 }

 sConfigOC.Pulse = 250;

 if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)

 {

   Error_Handler();

 }

 /* USER CODE BEGIN TIM2_Init 2 */

 /* USER CODE END TIM2_Init 2 */

 HAL_TIM_MspPostInit(&htim2);

}

HA.5
Associate III

you adjust these 2 different duty cycles with these 2 parameters

 sConfigOC.Pulse = 500;

 sConfigOC.Pulse = 250;

here is exactly 90 degree, but you can change them to have different phase shift degree