cancel
Showing results for 
Search instead for 
Did you mean: 

STM32U575 change PWM output duty realtime

wildflame
Associate

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?
 
4 REPLIES 4
Issamos
Lead II

Hello @wildflame 

I think you should use DMA as in this video

Best regards.

II

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.

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

 __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