cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 PWM

lk1991
Associate II
Posted on December 05, 2014 at 19:08

Hello,

I'm trying to change the  duty cycle of TIM1 in PWM Mode, but somehow my code is not working.

void TIM1_CC_IRQHandler(void)

{

  HAL_NVIC_ClearPendingIRQ(TIM1_CC_IRQn);

  HAL_TIM_IRQHandler(&htim1);

    cx++;

    if (cx>450)

    {

        cx=0;

    }

    TIM1->CCR1=cx;

    TIM1->CCR2=cx;

    TIM1->CCR3=cx;

}

If I remove the lines

    TIM1->CCR1=cx;

    TIM1->CCR2=cx;

    TIM1->CCR3=cx;

the code works fine, but without changing the duty cycle of course.

How can I change the duty cycle on the fly?

Thanks
2 REPLIES 2
Posted on December 05, 2014 at 19:21

Depends if you have it in immediate load mode, I'd probably opt to update them in the Update interrupt so you aren't trying to chase the edge as it advances.

HAL stuff, really can't help you with that. Ive posted some Servo PWM and other PWM examples using the Standard Peripheral Library to the forum.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Amel NASRI
ST Employee
Posted on December 10, 2014 at 12:18

Hi Lucas,

Please refer to

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Change%20CCR1%20value%20with%20HAL

where similar question was asked.

-Mayla-

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.