main函数跑下面代码就行
懂的就懂,不讲原理,自己看
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; //定时器2htim2.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; //模式选择PWM1TIM2_CH1Handler.Pulse=0; //设置比较值,此值用来确定占空比,默认0TIM2_CH1Handler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低 cc1p为0HAL_TIM_PWM_ConfigChannel(&htim2,&TIM2_CH1Handler,TIM_CHANNEL_1);//配置TIM2通道1HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);//开启PWM通道2GPIO_InitTypeDef GPIO_Initure;GPIO_Initure.Pin=GPIO_PIN_0; //PA0GPIO_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_CH1HAL_GPIO_Init(GPIOA,&GPIO_Initure);//---------------------------------------------------------------------- TIM_OC_InitTypeDef TIM2_CH2Handler; TIM2_CH2Handler.OCMode=TIM_OCMODE_PWM1; //模式选择PWM1TIM2_CH2Handler.Pulse=0; //设置比较值,此值用来确定占空比,默认0TIM2_CH2Handler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低 cc1p为0HAL_TIM_PWM_ConfigChannel(&htim2,&TIM2_CH2Handler,TIM_CHANNEL_2);//配置TIM2通道2HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_2);//开启PWM通道2GPIO_Initure.Pin=GPIO_PIN_1; //PA1GPIO_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_CH2HAL_GPIO_Init(GPIOA,&GPIO_Initure);//------------------------------------------------------------TIM_OC_InitTypeDef TIM2_CH3Handler; TIM2_CH3Handler.OCMode=TIM_OCMODE_PWM1; //模式选择PWM1TIM2_CH3Handler.Pulse=0; //设置比较值,此值用来确定占空比,默认比较值为自动重装载值的一半,即占空比为50%TIM2_CH3Handler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低 cc1p为0HAL_TIM_PWM_ConfigChannel(&htim2,&TIM2_CH3Handler,TIM_CHANNEL_3);//配置TIM2通道3HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_3);//开启PWM通道3GPIO_Initure.Pin=GPIO_PIN_2; //PA2GPIO_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_CH3HAL_GPIO_Init(GPIOA,&GPIO_Initure);
//----------------------------------------------------TIM_OC_InitTypeDef TIM2_CH4Handler; TIM2_CH4Handler.OCMode=TIM_OCMODE_PWM1; //模式选择PWM1TIM2_CH4Handler.Pulse=0; //设置比较值,此值用来确定占空比,默认比较值为自动重装载值的一半,即占空比为50%TIM2_CH4Handler.OCPolarity=TIM_OCPOLARITY_LOW; //输出比较极性为低 cc1p为0HAL_TIM_PWM_ConfigChannel(&htim2,&TIM2_CH4Handler,TIM_CHANNEL_4);//配置TIM2通道4HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_4);//开启PWM通道4GPIO_Initure.Pin=GPIO_PIN_3; //PA3GPIO_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_CH4HAL_GPIO_Init(GPIOA,&GPIO_Initure);}