cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H7 Complementary PWM: Issue on CHxN

MGian.1
Associate II

Hello,

I am trying to generate a complementary PWM with a timer in my STM32H723VGTx, I configured channel 1 on timer 1 with the PWM Generation CH1 CH1N mode.

I set all the parameters of the base timer and the value for the pulse of PWM, but during the validation process I didn't have any signal on the CH1N, I even tried on channel 2 but nothing.

Here my functions are then called in main.c:

/* PWM: */

void my_PWM_init(){

// Initialize TIM1 for PWM

TIM_HandleTypeDef htim1;

htim1.Instance = TIM1;

 

 

 

// Start TIM1

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); // Start channel 1

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2); // Start channel 2 as well

HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);

HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);

 

// Set PWM parameters for channel 1

Set_PWM_Frequency_DutyCycle(&htim1, 10000, 50, TIM_CHANNEL_1);

// Set PWM parameters for channel 2

Set_PWM_Frequency_DutyCycle(&htim1, 10000, 25, TIM_CHANNEL_2);

}

 

 

// Function to set PWM frequency and duty cycle for TIM1 channel

void Set_PWM_Frequency_DutyCycle(TIM_HandleTypeDef *htim, uint32_t frequency_hz, float duty_cycle_percent, uint32_t channel) {

// Calculate the period (ARR) value based on the desired frequency

uint32_t timer_clock = HAL_RCC_GetPCLK2Freq()*2; // Assuming TIM1 is on APB2

uint32_t period = timer_clock / frequency_hz;

 

// Calculate the pulse (CCR) value based on the desired duty cycle

uint32_t pulse = (uint32_t)(period * (duty_cycle_percent / 100.0f));

 

// Update TIM1 settings

TIM_OC_InitTypeDef sConfigOC;

sConfigOC.OCMode = TIM_OCMODE_PWM1;

sConfigOC.Pulse = pulse;

sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

 

if(HAL_TIM_PWM_ConfigChannel(htim, &sConfigOC, channel) != HAL_OK){

Error_Handler();

}

 

// Update the period (ARR) value

htim->Instance->ARR = period - 1;

 

// Set the new pulse (CCR) value

__HAL_TIM_SET_COMPARE(htim, channel, pulse);

}

 

 

 

6 REPLIES 6
TDK
Guru

Follow an existing example or refer to the documentation on how to use HAL PWM functions. In particular:

     (#) Configure the TIM in the desired functioning mode using one of the
       Initialization function of this driver:
       (++) HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel: to use the Timer to generate a
            PWM signal.

 If you're already calling those, show the full code.

> TIM_HandleTypeDef htim1;

> htim1.Instance = TIM1;

You're missing a lot more initialization parameters.

If you feel a post has answered your question, please click "Accept as Solution".
FBL
ST Employee

Hello @MGian.1 

Do you use CubeMX? If yes, please share your IOC file (e.g in excel format ..) 

Did you check your hardware configuration in datasheet?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

MGian.1
Associate II

Hello @FBL,

Here is my entire project!

I am just trying to configure the peripherals for the moment I am testing only.

Hello @MGian.1 

Make sure that the timer is configured correctly for PWM generation.

This includes setting the correct mode, setting the prescaler and period values, deadtime, and setting the pulse value for the PWM.

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hello @FBL,

The basic setting of the timer is correctly set as the PWM signal on CH1 is correct, the only problem is the complementary signal that is not provided.

I think that I neglected some enable or the pin configuration generated from the .ioc is wrong.

 

Make sure the pin in question works properly (think bad solder joints and shorts), e.g. by setting it as GPIO output and toggling "manually".

If that confirms HW is OK, then read out and check/post content of TIM and relevant GPIO registers.

JW