void WSCH_PwmChannel(TIM_TypeDef *TIMx, uint16_t TIM_Channel_x, uint16_t TIM_PulseVal) { ? ? TIM_OCInitTypeDef ? ? ? ? TIM_OCInitStructure; ? ?? ? ? STM32_GPIO_Init(PIN_T38, GPIO_Mode_AF_PP);//引脚配置 ?? ? ? ? //独立配置-PWM引脚 ? ? TIM_OCInitStructure.TIM_Pulse = 0;//占空比=0,在下面的TIM_SetCompareX中设定 ? ? TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//当计时器值小于比较器设定值时则TIMX输出脚此时输出有效高电位。 ? ? TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;//关闭引脚输出 ?? ?TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable;//关闭引脚输出 ? ? TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//极性 ? ? TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//极性 ?? ? ?? ?//指定在空闲状态下的TIM输出比较引脚的状态。仅供TIM1和TIM8有效。 ? ? //TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;//收到刹车信号后,空闲状态,输出低 ? ? //TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;//收到刹车信号后,空闲状态,输出低 ?? ? ?? ?//配置以上参数 ? ? TIM_OC2Init(TIMx, &TIM_OCInitStructure);//TIM_OC1Init TIM_OC2Init TIM_OC3Init TIM_OC4Init ?? ? ?? ?//其它配置 ? ? TIM_OC2PreloadConfig(TIMx, TIM_OCPreload_Enable);//【独立配置】重装,最好开启,可以屏蔽 ? ? TIM_ITConfig(TIMx, TIM_IT_CC2, DISABLE);//在定时器CC2溢出中断里面更新PWM,必须要 ?? ? ?? ?//设定占空比 ? ? TIM_SetCompare2(TIMx, TIM_PulseVal); //TIM_OCInitStructure.TIM_Pulse或TIMx->CCRx ?? ? ?? ?//开启那个引脚TIMx->CCER = 0x00000010; ? ? TIM_CCxCmd(TIMx, TIM_Channel_x, TIM_CCx_Enable);//TIM_OutputState ? ? TIM_CCxNCmd(TIMx, TIM_Channel_x, TIM_CCxN_Disable);//TIM_OutputNState }
void WSCH_PwmInit(TIM_TypeDef *TIMx, uint16_t TIM_Prescaler, uint16_t TIM_Period) { ? ? TIM_TimeBaseInitTypeDef ? TIM_TimeBaseInitStructure; ?? ? ? ? TIM_DeInit(TIMx);
? ? //全局配置-定时器 ? ? TIM_TimeBaseInitStructure.TIM_Prescaler = TIM_Prescaler; ? ? TIM_TimeBaseInitStructure.TIM_Period = TIM_Period; ? ? TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//【固定TIM_CounterMode_Up】 ? ? TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;//【固定TIM_CKD_DIV1】 ? ? TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//【固定0】重复多少次来一次中断! ? ? TIM_TimeBaseInit(TIMx, &TIM_TimeBaseInitStructure); ? ? TIM_ARRPreloadConfig(TIMx, ENABLE); ? ?? ? ? //TIM_CtrlPWMOutputs(TIMx, ENABLE);//【TIM1/8高级定时器才需要这句话】
? ? TIM_Cmd(TIMx, ENABLE); }
|