void TIM1_PWM_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
//设置该引脚为复用输出功能,输出TIM1的 CH1 CH1N的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //TIM_CH1N
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB
//Deinitializes the TIM1 peripheral registers to their default reset values
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
/*************************** 通道1 ********************************/
/*********占空比固定,为50%**********/
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //
TIM_OCInitStructure.TIM_Pulse = arr/2; //写跳变值(占空比
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //PWM功能使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //跳变前默认高
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //PWM双路输出使能
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //跳变前(正极)默认高
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//死区设置 1us
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //运行模式下“关闭状态”使能
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //关闭模式下“关闭状态”使能
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //锁定关闭
TIM_BDTRInitStructure.TIM_DeadTime = 0x10; //这里调整死区大小0-0xff 死区时间大概为1us
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //刹车功能使能
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //刹车输入高电平有效
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //自动输出使能
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //
TIM_OCInitStructure.TIM_Pulse = arr/2; //写跳变值(占空比
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //PWM功能使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //跳变前默认高
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; //PWM双路输出使能
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //跳变前(正极)默认高
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//
TIM_SelectOCxM(TIM1, TIM_Channel_1, TIM_ForcedAction_Active);
TIM_CCxCmd(TIM1, TIM_Channel_1 , TIM_CCx_Enable);
TIM_CCxNCmd(TIM1, TIM_Channel_1 , TIM_CCxN_Enable);
TIM_Cmd(TIM1, ENABLE); //使能TIM1
TIM_CtrlPWMOutputs(TIM1, ENABLE); //TIM1_OC 通道输出PWM(一定要加)//可以等同于下面这句话
//TIM1->BDTR |= 1<<15;
//Chen:!!!!!!!!!!!!!!!!!!TIM1想要输出PWM必须配置BDTR寄存
} |