Skip to main content
lk1991
Associate II
December 5, 2014
Question

STM32F4 PWM

  • December 5, 2014
  • 2 replies
  • 628 views
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
    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    December 5, 2014
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Amel NASRI
    ST Technical Moderator
    December 10, 2014
    To give better visibility on the answered topics, please click on "Best Answer" on the reply which solved your issue or answered your question.