程序附上
void TIM1_PWM_Init(u16 pFreq,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure; //输出通道配置
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; //死区和刹车配置
TimerPeriod = (SystemFrequency / pFreq) - 1; //自动重装载周期值
/* ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100 */
Channel1Pulse = (u16)((u32)(48 * (TimerPeriod - 1)) / 100 ); //占空比50%
Channel2Pulse = (u16)((u32)(51 * (TimerPeriod - 1)) / 100 ); //占空比50%
/* 使能TIM1,GPIOA,GPIOB,GPIOE */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE);
/*channel1 ,channel2 -->A.8,A.9*
*channel1N,channel2N-->B.13,B.14*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 初始化TIM1 */
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;//7199; //设置重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分频因子,仅与输入捕获有关(定时器与滤波器的频率比)
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //重复溢出中断
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //初始化定时器基本配置
/* Channel_1 TIM_OCMode_PWM1模式 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //PWM互补输出使能
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//50* 7200 / 100;; //占空比 = TIM_Pulse/TIM_Period;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补PWM极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //输出空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //PWM互补输出空闲状态
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据指定的参数初始化外设TIM1 OC1
/* Channel_2 TIM_OCMode_PWM1模式 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //在向上计数时,一旦TIMx_CNT>TIMx_CCR1时通道1为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //PWM互补输出使能
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;//50* 7200 / 100;; //占空比 = TIM_Pulse/TIM_Period;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //有效电平为高电平
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补PWM极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //输出空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; //PWM互补输出空闲状态
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //根据指定的参数初始化外设TIM1 OC2
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM3在CCR1上的预装载寄存器
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
/*死区和刹车功能配置*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM_BDTRInitStructure.TIM_DeadTime = 0X18; //设置TIM1_BDTR的DTG[7:0]
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_Cmd(TIM1, DISABLE); //使能TIM1
TIM_CtrlPWMOutputs(TIM1,DISABLE); //PWM输出使能
} |