Table of contents
Configure the PWM output module
Classification of timers
The STM32G431x chip provides three types of timers: basic timer, general timer, advanced timer
Figure source STM32G431RB data manual.pdf
This time introduces the pwm output function of the advanced timer
What is PWM?
Full name pulse width regulation
What does PWM do?
Adjust the duration of the high level in the unit cycle (the term is the duty cycle)
Rough principle: Set a comparison value in a counting cycle. When the counter is less than this number, it can output high level or low level. Once it is greater than this number, the state will be reversed, so the time of high level can be controlled. Beginner programming is often used as a breathing light. The principle of the breathing light is that the high level continues to decrease, and the led gradually dims.
Configure the PWM output module
1.CubeMX configuration
Tim15_ch1_PA2
Tim15_ch2_PA3
1) Find the pin with tim15 (you can search for tim15 in the lower right corner of cubemx to quickly determine the relevant pin)
2) Configure TIM15 to use two internal clock channels
GPIO port speed can be adjusted higher
3) Configuration of tim15 (output frequency)
The configuration to be noted here is the value of Counter period(AUTORELOAD ), which determines the output frequency
80 000 000 / 80 = 1 000 000 After frequency division, 1 000 000 pwm needs to output 1k frequency 1 000 000/1 000=1000 AUTORELOAD must be set to 999 (1000-1) pwm needs to output 2k frequency 1 000 000/2 000=500 AUTORELOAD must be set to 499 (500-1)
The macro definition that specifically sets AUTORELOAD in the hal library essentially calls the register
/**
* @brief Set the TIM Autoreload Register value on runtime without calling another time any Init function.
* @param __HANDLE__ TIM handle.
* @param __AUTORELOAD__ specifies the Counter register new value.
* @retval None
*/
#define __HAL_TIM_SET_AUTORELOAD(__HANDLE__, __AUTORELOAD__)
4) Configure duty cycle related
Here you need to pay attention to the value of Pulse, which determines the duty cycle of your pwm (the duration of the high level and then the unit cycle)
If you want to output a PWM duty cycle of 10%, you should use the value of AUTORELOAD * 10% to get the value to be set by Pulse. Here 300/1000=30% 600/1000=60%, so the duty cycle of pwm channel 1 is 30% pwm channel 2 duty cycle is 60% and the corresponding macro definition of hal library modifies the value of this register
#define __HAL_TIM_SET_COMPARE(__HANDLE__, __CHANNEL__, __COMPARE__)
@param __HANDLE__ TIM handle.
@param __CHANNEL__ TIM Channels to be configured.
This parameter can be one of the following values:
@arg TIM_CHANNEL_1: TIM Channel 1~6 selected
@param __COMPARE__ specifies the Capture Compare register new value.
2. Configuration in keil5
1) Update the generated code to the original tim.c file (user-defined reserved)
2) Main function configuration initialization
PWM_Output_TIM15_Init();
HAL_TIM_PWM_Start(&htim15,TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim15,TIM_CHANNEL_2);
Output pwm small exercise
By default, PA1 outputs 1kHz 50% PWM. After pressing the B1 button again, PA1 outputs 2KHz 10% pulse signal for 5 seconds to return to the default state.
if(key_down==B1)
{
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,50);//输出修改 改变pwm信号的占空比 50/500=10%
__HAL_TIM_SET_AUTORELOAD(&htim2,499); //10% 2k 1000000/500=2k
do_time=1;
uwTick_key_delay = uwTick;
}
if(((uwTick-uwTick_key_delay)>= 5000)&&(do_time == 1)){
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,500);//输出修改 500/1000=50%
__HAL_TIM_SET_AUTORELOAD(&htim2,999); //%50 1k 1000000/1000=1k
do_time=0
}
```