STM32 (7) - Principle Analysis of Timer Output PWM

concept + code


1. Output comparison

OC (Output Compare) output comparison The output
comparison can set the output level to 1, set to 0 or reverse the operation by comparing the relationship between CNT and CCR register values, which is used to output PWM waveforms with a certain frequency and duty cycle
. Both the advanced timer and the general-purpose timer have 4 output compare channels. The
first 3 channels of the advanced timer additionally have the function of dead zone generation and complementary output

2. Principle of PWM

PWM (Pulse Width Modulation) pulse width modulation
In a system with inertia, the required analog parameters can be equivalently obtained by modulating the width of a series of pulses, which are often used in motor speed control and other fields. PWM parameters
:
frequency = 1 / TS
duty cycle = TON / TS (the ratio of high level to total time), if the duty cycle is 50%, high level = 5V, low level = 0V, then the analog voltage is 2.5V.
Resolution Rate = duty cycle change step, ie accuracy.
insert image description here
The basic structure of PWM:
insert image description here
insert image description here
When the CNT count is less than CCR, set high level, when CNT is greater than CCR, set low level, when CNT=ARR triggers an event, the count value returns to zero, and starts a new cycle, so that Can continuously output PWM signal. Therefore, it can be seen that the setting of the CCR value is closely related to the duty cycle.
PWM frequency: Freq = CK_PSC / (PSC + 1) / (ARR + 1)
CK_PSC is the prescaler frequency, generally the system frequency. PSC is the frequency division factor, and ARR is the maximum count value of the automatic reload register.
PWM duty cycle: Duty = CCR / (ARR + 1)
CCR is a limited comparison value of output compare output PWM.
PWM resolution: Reso = 1 / (ARR + 1)

3. Output PWM

Take the punctual atomic PWM experiment as an example: use the CH2 channel of TIM3 to output PWM waves at the PB5 port.

1. PWM initialization

void TIM3_PWM_Init(u16 arr,u16 psc)
{
    
    
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);    //使能定时器3时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);  //使能GPIO外设和AFIO复用功能模块时钟
    GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3部分重映射  TIM3_CH2->PB5

   //设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形    GPIOB.5
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
   //初始化TIM3
    //溢出时间time=(arr+1)*(psc+1)/144000000
    TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
    TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
    TIM_TimeBaseStructure.TIM_ClockDivision = 1; //设置时钟分割:TDTS = Tck_tim
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位

    //初始化TIM3 Channel2 PWM模式
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
    TIM_OCInitStructure.TIM_Pulse = 50;
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM3 OC2

    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  //使能TIM3在CCR2上的预装载寄存器
    TIM_Cmd(TIM3, ENABLE);  //使能TIM3
}

The most important things here are:
1. The parameters of this function are arr and psc, which represent the value of the auto-reload register and the prescaler coefficient respectively. According to these two parameters, the frequency and duty cycle of PWM can be calculated.

2. In the function, first enable the timer TIM3 and the clock of the GPIOB peripheral, and the clock of the multiplexing function module of the GPIO. Then, map the TIM3_CH2 signal to the GPIOB.5 pin through the GPIO_PinRemapConfig function.
Here is a question: why configure the AFIO multiplexing function module clock? As shown in the figure below:
insert image description here
You can see that the default multiplexing function of PB5 is I2C1_SMBA/ SPI3_MOSI, I2S3_SD, and its redefinition function is TIM3_CH2, so it cannot be reused simply and directly, and remapping must be configured.

3. Initialize channel 2 of TIM3 through the TIM_OCInitTypeDef structure, select the timer mode as PWM2, enable the comparison output, set the output polarity to high level, and set the duty cycle to 50%.
Finally, enable the preload register of TIM3 on CCR2 through the TIM_OC2PreloadConfig function, and enable TIM3 through the TIM_Cmd function.

2. Output specified frequency PWM wave

   TIM3_Int_Init(5999,0);
   //TIM3_PWM_Init(5999,0);//不分频。PWM 频率=96000/(5999+1)=16KHz

Reference PWM frequency: Freq = CK_PSC / (PSC + 1) / (ARR + 1) , here my chip system frequency is 96MHz, corresponding to CK_PSC, ARR is the count value, PSC is the prescaler value, substitute into the formula, get: frequency F=16000Hz=16KHz.
Therefore, this code outputs a PWM wave with a fixed frequency of 16KHz.

3. Output PWM wave with variable duty cycle

TIM3_Int_Init(5999,0);
u16 pulse=400;
TIM_SetCompare2(TIM3,pulse);

For timer TIM3, the value range of CCR2 register is 0 to ARR, that is, the value of compare register cannot be greater than the value of auto-reload register . Therefore, when setting the duty cycle, the input duty cycle value needs to be converted into the value of the corresponding compare register through calculation. That is to say, the value of pulse can only be set between 0-5999, and note that the function called here is the TIM_SetCompare2() function, which is the comparison register CCR2 corresponding to channel 2. If it is ch3, the TIM_SetCompare3() function should be called. In this way, the duty cycle of the PWM wave is modified.

Guess you like

Origin blog.csdn.net/qq_53092944/article/details/130703311