回复【2楼】ST-FAE-SALE:
---------------------------------
memset(&TIM_OCInitStructure,0,sizeof(TIM_OCInitStructure));
memset(&TIM_BaseInitStructure,0,sizeof(TIM_BaseInitStructure));
/*外设使能*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_DeInit(TIM1);
//中断NVIC设置:允许中断,设置优先级
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//预分频设置,以及模式设置
TIM_BaseInitStructure.TIM_Period =1000;//周期设定,电机启动时会修改
//设定定时器预分频,
TIM_BaseInitStructure.TIM_Prescaler =CPU_FREQUENCE/F2TIME_PARA-1;
//PWM模式的设置
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM2模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //信号输出到对应的输出引脚
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //互补信号输出到对应的输出引脚
TIM_OCInitStructure.TIM_Pulse =50; //脉冲宽度
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //互补输出高电平有效
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补输出高电平有效
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //输出空闲状态为1
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //互补输出空闲状态为0
TIM_OC1Init(TIM1,&TIM_OCInitStructure); //OC1通道初始化
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
//清中断,以免一启用中断后立即产生中断
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);//使能TIM1中断源
TIM_Cmd(TIM1, DISABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE); //使能PWM输出