Answer the question
In order to leave comments, you need to log in
How to setup PWM for servo in STM32?
There is a code from an example with an option when clocking comes from an internal generator at 8 MHz, it is working.
And here's what I don't understand. Judging by the circuit from the cube on the bus from which the timer is also clocked at 8 MHz, prescaler TIM2-> PSC = 6, how could it be 50 Hz?
Now, I clock from an external quartz at 8MHz, when it is connected, the STM32 itself (wtf) sets the multiplier and starts at 72MHz.
I would like to understand how to calculate the PWM timer for a servo at a different frequency (72MHz), can anyone describe the calculation sequence?
// Настраиваем таймер таймер 2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // Тактируем
TIM2->CCER |= (TIM_CCER_CC2E); // Разрешаем таймеру использовать ногу PA1 для ШИМа
TIM2->CCMR1 |= (TIM_CCMR1_OC2M_1 | TIM_CCMR1_OC2M_2);
TIM2->PSC = 6; // Настраиваем предделитель, чтобы частота ШИМа была в районе 50 Гц
TIM2->CR1 |= TIM_CR1_CEN; // Запускаем
Answer the question
In order to leave comments, you need to log in
1. Throw away the "cube"!
2. Throw out SPL, HAL and other abominations!
3. Read the reference manual until clear!
The "self" chip will not do anything until you configure it. If you continue to use the "cube", then it will be very bad with logic.
72 MHz is a normal internal clock frequency for, say, STM32F1xx. What chip? It is necessary to dance from this.
Open the reference manual in the RCC section and read. Let's say STM32F1xx TIM2 is clocked from APB1. APB1 is from AHB, and that one is directly from SYSCLK. If you clock from an external quartz at 8 MHz, then you need to select HSE as the input for PLL in the configuration, and PLL as the input for SYSCLK (by the way, when clocking from the internal RC circuit, the maximum frequency of SYSCLK will be 36 MHz) and set the AHB prescalers as necessary, APB1 and TIM2_PSC. You also need to remember to allow clocking of all this peripherals.
If the AHB and APB1 prescalers are set to 1, and the PLL multiplier is set to 9, then we get 72 MHz at the TIM2 clock input. If the TIM2_PSC register is set to 6, then the frequency will be divided by 7 - you get a "crooked" number.
What is 50Hz PWM? Pulse repetition rate? If so, then you need to dance on the required accuracy of the duty cycle. If you want a 50Hz PWM with 8-bit fill, then the timer frequency should be 256 times the 50Hz, i.e. be 12800Hz. When clocking TIM2 directly, i.e. 72 MHz, in TIM2_PSC you will need to write the number 5624 - then its frequency will be exactly 12800 Hz. In TIM2_ARR we write 255, and in TIM2_CCRx (x depends on which leg is selected by the PWM output and how the timer is connected to it) we will write a number from 0 to 255, depending on the required filling.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question