Skip to main content
Bhavik
Associate II
May 30, 2023
Question

Nucleo-F030R8 PWM and Phase shift PWM

  • May 30, 2023
  • 13 replies
  • 11933 views

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

This topic has been closed for replies.

13 replies

waclawek.jan
Super User
June 2, 2023

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

Bhavik
BhavikAuthor
Associate II
June 2, 2023

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.

waclawek.jan
Super User
June 2, 2023

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

Bhavik
BhavikAuthor
Associate II
June 2, 2023

@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

HA.5
Associate II
June 2, 2023

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

Bhavik
BhavikAuthor
Associate II
June 2, 2023

@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 II
June 5, 2023

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

HA.5
Associate II
June 5, 2023

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);

}

Bhavik
BhavikAuthor
Associate II
June 5, 2023

Thanks @HA.5 for your answer. I will surely try out that and post back results here.

HA.5
Associate II
June 5, 2023

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

Bhavik
BhavikAuthor
Associate II
June 5, 2023

Surely. I will try this and post back my results. @HA.5​  Thanks.​

Bhavik
BhavikAuthor
Associate II
June 5, 2023

What i came across is that to generate the wave form (as similar to question - just a single wave form as per image) We need two pwm which can be logically & to get the resultant out put.

Is that true?

Is there any other way to do it without using any addon timer. Like combine pwm output?

I came across post where something similar is required. I need help to understand the same. Putting refrence link and image.

Link

https://community.st.com/s/feed/0D50X0000C4PPGhSQO

Image
_legacyfs_online_stmicro_images_0693W00000dK0VtQAK.png 

Image is just for reference and not represent exact requirements of frequency and duty cycle.

@HA.5 @Community member 

HA.5
Associate II
June 5, 2023

we have 2 out put pins, for example PA9 and PA10, and you want 2 PWM signals on these pins, for example on PA9 sth and on PA10 another one with 90 degree phase shift, is that what you want?

Bhavik
BhavikAuthor
Associate II
June 5, 2023

Final output waveform need to be similar as black color waveform in PinX and 180° phase shift of same wave form in PinY. Thats what the final goal is.

Sorry if it is not proper here or in any of the above comment.

HA.5
Associate II
June 5, 2023

ok i got it now, so you need sth like the black color, for example on PA9 a signal, and on PA10 with 180°phase shift another one. as i see on the picture you need a pasue(delay) between them too.

so as said before you can use one Timer (as i wrote the code) with phase shift what you want(but 2 different duty cycles) to reach that, but if you want to have the same duty cycle too, you should use 2 timers and not only one(as far as I know).

i can write you the code with 2 Timers if it is what you help

HA.5
Associate II
June 5, 2023

there is also another way to produce that with only one timer, you can use bit patterns and interrupt. so in this way you can generate the pulses by set and reset the pins and not using HAL_PWM_START functions.