cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 variable duty cycle using ADC with complementary pwm

Abdoulaye
Associate

Hi all!

I am new on STM32 this is my first project on STM32CubeIDE. I want to generate variable duty cycle using ADC with complementary PWM. When I did the code for one channel PWM it works well and the duty cycle varies  but when I did it for the complementary PWM it does not work . Here is a picture of the code. What should I do to make it works for the complementary PWM? I will be grateful for your help.

 

while (1)

{

HAL_ADC_Start(&hadc1);

HAL_ADC_PollForConversion(&hadc1,HAL_MAX_DELAY);

pwm = HAL_ADC_GetValue(&hadc1);

duty = (pwm*899/4095);

HAL_Delay(1);

}

 

__HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, duty);

HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1); //AH=H,S1

2 REPLIES 2
Sarra.S
ST Employee

Hello @Abdoulaye, welcome to ST Community, 

In your main loop, update the duty cycle for the complementary PWM signals

 HAL_ADC_Start(&hadc1);
    HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
    uint32_t pwm = HAL_ADC_GetValue(&hadc1);
    uint32_t duty = (pwm * 899 / 4095);

    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, duty);
    __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1N, duty); // Update complementary channel

 Also, enable dead-time insertion in your timer configuration.

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.

Michael E
ST Employee

Hello @Abdoulaye ,

Could you please specify the reference of your STM32?
There are also several articles available on the wiki here that may help you.

Best Regards

 

 

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.