Skip to main content
Associate
January 26, 2024
Question

Turn PWM on and off with a control variable

I'm using the STM32F103C8T6 microcontroller, better known as the blue pill.

I need to make a code that reads the voltage of a port (which I already managed to do) and depending on the input voltage it turns the PWM on or off. I'm having trouble because the code seems to be correct, but the PWM doesn't turn on.

Below I provide my Timer and PWM configuration in the hope that someone can help me. This is the code I made:

Captura de tela 2024-01-26 124900.png

 

Captura de tela 2024-01-26 125313.png

 

Captura de tela 2024-01-26 125347.png

 

Could anyone help me?

This topic has been closed for replies.

3 replies

TDK
January 26, 2024

HAL_TIM_PWM_Stop stops the timer. Call it before HAL_TIM_PWM_Start or use lower-level commands to start/stop only that channel.

https://github.com/STMicroelectronics/stm32f1xx_hal_driver/blob/ddf259a6eb9dc4ba421f5e2b1ae619bd77468214/Src/stm32f1xx_hal_tim.c#L1523

Is pulse set to a value that would provide a PWM signal as opposed to a DC signal?

 

"If you feel a post has answered your question, please click ""Accept as Solution""."
Associate
January 28, 2024

I'm sorry I did not understand your question. I just want to generate a PWM that varies depending on the voltage read

You mean that I initialize "HAL_PWM_Start" outside the while, right? So I use this "_HAL_TIM_DISABLE(htim)" in while?
But I'm using all PWM channels on TIM1, so using this code would disable all channels, wouldn't it?

TDK
January 29, 2024

Within your main loop, in each of your 3 if statements, you have 1 start command and 2 stop commands. Put the 2 stop commands before the 1 start command. The stop command disables the timer.

"If you feel a post has answered your question, please click ""Accept as Solution""."