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
Bhavik
Associate III

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

Bhavik
Associate III

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 

Bhavik
Associate III

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

HA.5
Associate III

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
Associate III

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 III

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 III

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.

HA.5
Associate III

but when the final out put is 180° phase shift, use just the code what i wrote but change one of the channel to TIM_OCMODE_PWM2; and it gives you exactly 180 degree phase shift and you can have the same duty cycle too. i write you the code here:

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; // adjust frequency

 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim2.Init.Period = 1000; // adjust frequency

 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_PWM2; // pwm2 and pwm1 have 180° phase shift

 sConfigOC.Pulse = 500; // adjust duty cycle for PWM2

 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.OCMode = TIM_OCMODE_PWM1; // pwm2 and pwm1 have 180° phase shift

 sConfigOC.Pulse = 500; // adjust duty cycle for PWM1

 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
Associate III

@HA.5 thanks i will surely try and come back with results.

Bhavik
Associate III

@HA.5​ I tried and I am able to produce the output. Sorry If I asked or may ask a stupid question since I am unfamiliar with this controller.

Here are the results.


_legacyfs_online_stmicro_images_0693W00000dK3SJQA0.png