void TIM4_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOB ,ENABLE);//IO口使能设置
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//配置时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM3的CH3连接PB8管脚
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF;//复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period=arr;//10000; //1s
TIM_TimeBaseStructure.TIM_Prescaler=psc;//7199; //720分频
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1;//PWM1模式1
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable;//比较输出使能
TIM_OCInitStructure.TIM_Pulse = 2500; //设置占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出比较极性高
TIM_OC3Init(TIM4, & TIM_OCInitStructure);//初始化TIM4的CH3通道
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);//使能TIM4在CH3通道CCR3上的预装载寄存器
TIM_ARRPreloadConfig(TIM4,ENABLE);//使能TIM4在CH3通道ARR3上的预装载寄存器
TIM_Cmd(TIM4,ENABLE);//使能定时器4
|