Skip to main content
Associate
September 27, 2023
Question

STM32U575 change PWM output duty realtime

  • September 27, 2023
  • 2 replies
  • 1427 views

I tried to change PWM output duty without stopping or suspending PWM output, but between two times of writing new value to CCR, there must be a delay time inserted in. Following is the example code:

/* if delay_ms(300), code can run correctly, if changing it to delay_ms(200), a hardfault would occur

    volatile int val = 0;
    if (HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1) != HAL_OK)
    {
        /* PWM Generation Error */
        Error_Handler();
    }
    while (1)
    {
         __HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, val);
        delay_ms(300);
        if (val == 144)
        {
            val = 0;
        }
        else
        {
            val += 12;
        }
    }
Who can help me out that if it is a bug of this MCU?
 
This topic has been closed for replies.

2 replies

Issamos
Lead III
September 27, 2023

Hello @wildflame 

I think you should use DMA as in this video

Best regards.

II

wildflameAuthor
Associate
October 9, 2023

Hello,

Thanks for your reply!

I really want to know why this happens. Is there something wrong within my code or it a bug of the chip.

KnarfB
Super User
October 9, 2023

How is delay_ms implemented? It is not a HAL function.

waclawek.jan
Super User
October 9, 2023

 __HAL_TIM_SetCompare() just writes a value to TIMx_CCRx, so there's something else which throws the fault, not this function.

Do you have some sort of interrupt bound to this timer channel? If yes, maybe start there.

And, debug as usually hardfaults are debugged, i.e. observe stacked values of registers, mainly PC, observe disasm few lines before the value of PC, etc.

JW