Skip to main content
Visitor II
June 25, 2024
Question

STM32 variable duty cycle using ADC with complementary pwm

  • June 25, 2024
  • 2 replies
  • 1267 views

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

    This topic has been closed for replies.

    2 replies

    ST Employee
    August 6, 2024

    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.

    Technical Moderator
    August 6, 2024

    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