????????懂的就懂,不讲原理,自己看
MX_TIM2_Init(7,999);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,500);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_2,500);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_3,500);
__HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_4,500);
void MX_TIM2_Init(u16 Pres,u16 Peri){
__HAL_RCC_TIM2_CLK_ENABLE(); //使能定时器2
__HAL_RCC_GPIOA_CLK_ENABLE(); //开启GPIOA时钟
htim2.Instance=TIM2; //定时器2
htim2.Init.Prescaler=Pres; //定时器分频
htim2.Init.CounterMode=TIM_COUNTERMODE_UP;//向上计数模式
htim2.Init.Period=Peri; //自动重装载值
HAL_TIM_PWM_Init(&htim2); //初始化PWM
//-------------------------------------------------------------------
TIM_OC_InitTypeDef TIM2_CH1Handler;
TIM2_CH1Handler.OCMode=TIM_OCMODE_PWM1; //模式选择PWM1
TIM2_CH1Handler.Pulse=0; //设置比较值,此值用来确定占空比,默认0
TIM2_CH1Handler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低 cc1p为0
HAL_TIM_PWM_ConfigChannel(&htim2,&TIM2_CH1Handler,TIM_CHANNEL_1);//配置TIM2通道1
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);//开启PWM通道2
GPIO_InitTypeDef GPIO_Initure;
GPIO_Initure.Pin=GPIO_PIN_0; //PA0
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH; //高速
GPIO_Initure.Alternate= GPIO_AF2_TIM2; //PA1复用为TIM2_CH1
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
//----------------------------------------------------------------------
TIM_OC_InitTypeDef TIM2_CH2Handler;
TIM2_CH2Handler.OCMode=TIM_OCMODE_PWM1; //模式选择PWM1
TIM2_CH2Handler.Pulse=0; //设置比较值,此值用来确定占空比,默认0
TIM2_CH2Handler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低 cc1p为0
HAL_TIM_PWM_ConfigChannel(&htim2,&TIM2_CH2Handler,TIM_CHANNEL_2);//配置TIM2通道2
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);//开启PWM通道2
GPIO_Initure.Pin=GPIO_PIN_1; //PA1
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH; //高速
GPIO_Initure.Alternate= GPIO_AF2_TIM2; //PA1复用为TIM2_CH2
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
//------------------------------------------------------------
TIM_OC_InitTypeDef TIM2_CH3Handler;
TIM2_CH3Handler.OCMode=TIM_OCMODE_PWM1; //模式选择PWM1
TIM2_CH3Handler.Pulse=0; //设置比较值,此值用来确定占空比,默认比较值为自动重装载值的一半,即占空比为50%
TIM2_CH3Handler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低 cc1p为0
HAL_TIM_PWM_ConfigChannel(&htim2,&TIM2_CH3Handler,TIM_CHANNEL_3);//配置TIM2通道3
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);//开启PWM通道3
GPIO_Initure.Pin=GPIO_PIN_2; //PA2
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH; //高速
GPIO_Initure.Alternate= GPIO_AF2_TIM2; //PA2复用为TIM2_CH3
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
//----------------------------------------------------
TIM_OC_InitTypeDef TIM2_CH4Handler;
TIM2_CH4Handler.OCMode=TIM_OCMODE_PWM1; //模式选择PWM1
TIM2_CH4Handler.Pulse=0; //设置比较值,此值用来确定占空比,默认比较值为自动重装载值的一半,即占空比为50%
TIM2_CH4Handler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低 cc1p为0
HAL_TIM_PWM_ConfigChannel(&htim2,&TIM2_CH4Handler,TIM_CHANNEL_4);//配置TIM2通道4
HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_4);//开启PWM通道4
GPIO_Initure.Pin=GPIO_PIN_3; //PA3
GPIO_Initure.Mode=GPIO_MODE_AF_PP; //复用推挽输出
GPIO_Initure.Pull=GPIO_PULLUP; //上拉
GPIO_Initure.Speed=GPIO_SPEED_FREQ_HIGH; //高速
GPIO_Initure.Alternate= GPIO_AF2_TIM2; //PA3复用为TIM2_CH4
HAL_GPIO_Init(GPIOA,&GPIO_Initure);
}