Skip to main content
jlecl.1
Associate III
July 11, 2023
Solved

STM32F103 and PWM (12bits)

Dear everyone,

I'm trying to manage a DC motor with IC controller in PWM.

My frequency have been calculated for have 12bits resolutions (Fclk :72Mhz > Fpwm17.5Khz).

 

TIM2->CCR1 = 30;
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
HAL_GPIO_WritePin(GPIOB, MA_SENS_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, MA_SLEEP_Pin, GPIO_PIN_SET);

 

The desired frequency is good, but i'm not able to have the resolution.
I thinks, my DutyCycle command is not right but no idea to do different.

Do you have an idea ?

Capture.PNG

 

 

 

This topic has been closed for replies.
AScha.3
AScha.3Best answer
Super User
July 11, 2023

You set the prescaler to 72, so you get 1us ticks. Then 54 ticks in timer...17 khz with 5 bit resolution - as you set it.

For 12bits set prescaler to 0 and timer to 4095, then get 12 bits

And CCR 2048 is mid/half period.

But why so high frequency? Do you drive the motor direct with this pwm (MOSFET and freewheeling diode )? Then from my tests a much lower frequency is better, because of the high inductivity of the motor.

I used 200 Hz or so...

 

 

If you feel a post has answered your question, please click on " Best Answer ".

2 replies

AScha.3
AScha.3Best answer
Super User
July 11, 2023

You set the prescaler to 72, so you get 1us ticks. Then 54 ticks in timer...17 khz with 5 bit resolution - as you set it.

For 12bits set prescaler to 0 and timer to 4095, then get 12 bits

And CCR 2048 is mid/half period.

But why so high frequency? Do you drive the motor direct with this pwm (MOSFET and freewheeling diode )? Then from my tests a much lower frequency is better, because of the high inductivity of the motor.

I used 200 Hz or so...

 

 

If you feel a post has answered your question, please click on " Best Answer ".
jlecl.1
jlecl.1Author
Associate III
July 11, 2023

Thanks for you reply.

The 17,5Khz have been choosed for three raison:

> Current pulse smaller

> Sound not audible

> 12bits resolution as calcul with the formula.

I will test it now!