Blue Bridge Cup Embedded-Timer (2) (Key)-PWM and output comparison

The blue bridge cup timers mainly include TIM1 (advanced timer, which will be introduced later), TIM2, and TIM3. Here take Timer 2 as an example to write the PWM configuration of the general timer.

Point 1

The PWM frequency is determined by ARR, and the duty cycle is determined by the CCRX register.

Remember to enable the multiplexed clock of the IO port.

The concept of PWM mode 1 and PWM mode 2:
Insert picture description here

Configuration steps

1. Find the IO port corresponding to the TIM2 channel in the data sheet.
Insert picture description here

2. Write initialization function (notes are very detailed)

 void Tim2_PWM_Config(u16 arr,u16 psc)//都是16位的寄存器
{
    
    
 GPIO_InitTypeDef GPIO_InitStructure;
 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
 TIM_OCInitTypeDef TIM_OCInitStructure;
 
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); //使能复用时钟
 
 TIM_TimeBaseInitStructure.TIM_Period=arr;//自动重装载值设置值的时候要注意减一位
 TIM_TimeBaseInitStructure.TIM_Prescaler=psc;//预分频系数
 TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;//计数模式
 TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);//定时器初始化
 
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOA,&GPIO_InitStructure);
 
 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//PWM1模式
 TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//输出状态使能
 TIM_OCInitStructure.TIM_Pulse=500;//设置占空比
 TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//高电平有效还是低电平有效。
 TIM_OC2Init(TIM2,&TIM_OCInitStructure);
 
 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//PWM1模式
 TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//输出状态使能
 TIM_OCInitStructure.TIM_Pulse=700;//设置占空比
 TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//高电平有效还是低电平有效。
 TIM_OC3Init(TIM2,&TIM_OCInitStructure);
 
 
 TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能预装载寄存器
 TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能预装载寄存器
 
 TIM_Cmd(TIM2,ENABLE);//定时器使能。
}

Note: Just change the relative value of'TIM_OCInitStructure.TIM_Pulse' to output waveforms with different duty cycles.

3. In addition, library functions that must be mastered and used skillfully:

void TIM_SetCounter(TIM_TypeDef* TIMx, uint16_t Counter);//设置计数器的数值
void TIM_SetAutoreload(TIM_TypeDef* TIMx, uint16_t Autoreload);
void TIM_SetCompare1(TIM_TypeDef* TIMx, uint16_t Compare1);//设置定时器通道1的比较值,也就是CCR1寄存器的值
void TIM_SetCompare2(TIM_TypeDef* TIMx, uint16_t Compare2);
void TIM_SetCompare3(TIM_TypeDef* TIMx, uint16_t Compare3);
void TIM_SetCompare4(TIM_TypeDef* TIMx, uint16_t Compare4);
uint16_t TIM_GetCapture1(TIM_TypeDef* TIMx);              //得到定时器通道1的比较值
uint16_t TIM_GetCapture2(TIM_TypeDef* TIMx);
uint16_t TIM_GetCapture3(TIM_TypeDef* TIMx);
uint16_t TIM_GetCapture4(TIM_TypeDef* TIMx);
uint16_t TIM_GetCounter(TIM_TypeDef* TIMx);
uint16_t TIM_GetPrescaler(TIM_TypeDef* TIMx);		//得到当前定时器的预分频系数

Point 2

In the test questions of the Lanqiao Cup, you can use different channels of a timer to generate PWM waves of different frequencies. The above cannot be done. Because'TIM_TimeBaseInitStructure.TIM_Prescaler=psc;' this function has determined that the frequency is certain. In order to realize that the two channels generate PWM waves with different frequencies and duty cycles, the output comparison mode is required. For the knowledge of the output comparison mode, look at this big man .
Below is the code I wrote and the effect I saw with the logic analyzer:(I put the specific code here )

It can be seen that the frequency and duty cycle of the two channels are different.Insert picture description here

Precautions

According to the article of the big guy, I wrote some notes and basic concepts:
1. The preload register in the timer initialization is not enabled, which is different from the PWM initialization in point 1.

  TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Disable);
 TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Disable);//预装载寄存器不使能

2. Change the output mode of the timer to TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;, I will explain the meaning of this mode in detail
Insert picture description here
Insert picture description here
. From these two figures, we can see that the function of this mode is: when the value of the CCR register and the value of the CNT counter are equal The level is flipped.

3. Another concept is **TIM_ITConfig(TIM2,TIM_IT_CC2 | TIM_IT_CC3, ENABLE);**The meaning of the timer interrupt: When the value of the CCR register and the value of the counter are equal, the interrupt is entered.

to sum up

These two modes of generating PWM are very important, and it is best to master both.

Guess you like

Origin blog.csdn.net/qq_43690936/article/details/105162640
Recommended