原来是没有给tim1加中断优先级配置,但是随后问题又出来了,如果要产生互补的pwm波,不改变占空比前是正常互补的,在中断中改变了通道1的占空比后,互补通道没有输出了?
/**********************************
* [url=home.php?mod=space&uid=247401]@brief[/url] TIM_PWM_Config
* @param None
* @retval 配置pwm 参数
**********************************/
void TIM1_PWM_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel=TIM1_CC_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//预定标的值TIM1_Period = (time定时器频率 / pwm的频率) - 1
TimerPeriod = (SystemCoreClock / 50000 ) - 1;
//Channel1Pulse的值可以按照下面的公式进行计算:
//ChannelxPulse = DutyCycle * (TIM1_Period - 1) / 100
//DutyCycle/100为占空比的值
//计算CCR1 跳转值 在占空比为50%时
/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
//Time 定时基础设置
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//Time 定时设置为上升沿计算模式
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_UpdateRequestConfig(TIM1,TIM_UpdateSource_Regular);
//频道1的PWM 模式设置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;//TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//选择输出比较状态
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//选择互补输出比较状态
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;//设置互补输出极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
//装载比较寄存器,判断什么时候发生PWM翻转
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;//使能频道1配置
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
//使能TIM1 CCR2寄存器上的 载入
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1);
//装载比较寄存器,判断什么时候发生PWM翻转
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;//使能频道2配置
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
//使能TIM1 CCR2寄存器上的 载入
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1);
//使能ARR寄存器 加载
TIM_ARRPreloadConfig(TIM1,ENABLE);
//死区设置
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //运行模式下输出
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //空闲模式下输出选择
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //锁定设置,锁定级别1
TIM_BDTRInitStructure.TIM_DeadTime = 0x0F; //死区时间
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //刹车功能使能
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low; //刹车输入极性,即刹车控制引脚接GND时,PWM停止
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //自动输出使能
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
TIM_Cmd(TIM1, ENABLE);
TIM_ITConfig(TIM1,TIM_IT_Update ,ENABLE);
TIM_ITConfig(TIM1,TIM_IT_CC1,ENABLE);
//TIM1 主输出使能
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
中断部分
void TIM1_CC_IRQHandler(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
if (TIM_GetITStatus(TIM1, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
if(Num_tim1==500000)
{
Num_tim1=0;
if(PWM_num==5)
{
PWM_num=1;
SETPwmPulse(PWM_num);
//TIM_SetCompare1();
}
else
{
SETPwmPulse(PWM_num);
PWM_num++;
}
}
else
{
Num_tim1++;
}
}
else if (TIM_GetITStatus(TIM1, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}
}
|