有个问题咨询大佬们:设置了STM32F103R8T6 的定时器1 为比较输出 产生PWM ,现在设置好后,在定时器初始化函数中一开起定时器1就一直进入TIM1_CC_IRQHandler中断中,利用串口打印一直在这个中断里 ,没有进入while。关闭了TIM1通道也没有用,不知道为何?烦请大佬们看下问题原因在哪里
函数源代码如下:
定时器初始化函数:
void TIM1_Int_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
TIM_DeInit(TIM1); //将TIM1定时器初始化位复位值
TIM_InternalClockConfig(TIM1);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; //TIM1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器
bsp_InitMOTO();
//设置该引脚为复用输出功能,输出TIM1 CH1的PWM脉冲波形 GPIOA.8
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM1_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //TIM_CH4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
//初始化TIM1
TIM_TimeBaseStructure.TIM_Period = STEPMOTOR_TIM_PERIOD; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =STEPMOTOR_TIM_PRESCALER; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1|TIM_IT_CC4);
TIM_ClearFlag(TIM1, TIM_FLAG_CC1|TIM_FLAG_CC4);
//初始化TIM1 Channe1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; // 比较输出模式:反转输出
TIM_OCInitStructure.TIM_Pulse = 0xFFFF; // 脉冲数
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // 输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; // 互补通道输出极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; // 空闲电平
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; // 互补通道空闲电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 CH1
// TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; // 比较输出模式:反转输出
TIM_OCInitStructure.TIM_Pulse = 0xFFFF; // 脉冲数
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // 输出极性
// TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; // 互补通道输出极性
// TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; // 空闲电平
// TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset; // 互补通道空闲电平
// TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Disable); //使能TIM1在CCR1上的预装载寄存器
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);
TIM_CCxCmd(TIM1, TIM_Channel_4, TIM_CCx_Disable);
TIM_ClearITPendingBit(TIM1,TIM_IT_CC1|TIM_IT_CC4);
TIM_ClearFlag(TIM1, TIM_FLAG_CC1|TIM_FLAG_CC4);
TIM_ITConfig(TIM1,TIM_IT_CC1,ENABLE ); //使能指定的TIM3中断,允许更新中断
TIM_ITConfig(TIM1,TIM_IT_CC4,ENABLE ); //使能指定的TIM3中断,允许更新中断
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
}
中断处理函数:
if(TIM_GetITStatus(TIM1 , TIM_IT_CC1) == SET)
{
// 清楚定时器中断
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
TIM_ClearFlag(TIM1,TIM_FLAG_CC1);
// 设置比较值
tim_count=TIM_GetCounter(TIM1);
TIM_SetCompare1(TIM1,tim_count+srd.step_delay);
i++; // 定时器中断次数计数值
if(i==2) // 2次,说明已经输出一个完整脉冲
{
i=0; // 清零定时器中断次数计数值
switch(srd.run_state) // 加减速曲线阶段
{
case STOP_M:
step_count = 0; // 清零步数计数器
rest = 0; // 清零余值
last_accel_delay=0;
// 关闭通道
TIM_CCxCmd(TIM1, TIM_Channel_1, TIM_CCx_Disable);
TIM_ClearFlag(TIM1,TIM_FLAG_CC1 );
// //TIM_ITConfig(TIM1,TIM_IT_CC1,DISABLE ); //使能指定的TIM3中断,允许更新中断
// //TIM_CtrlPWMOutputs(TIM1,DISABLE);
TIM_Cmd(TIM1, DISABLE);
STEPMOTOR_OUTPUT_DISABLE();
MotionStatus = 0; // 电机为停止状态
Motor42=0;
Xzhuan=0;
printf("MOTOR STOP!!!!!!!!!!!!!!!!!!!!!!\r\n");
break;
默认state为STOP_M 关闭定时器。
主函数:
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
Stm32_Clock_Init(9);
delay_init();
USART2_Init();
TIM1_Int_Init();
while(1)
{
delay_ms(1000);
printf("IN while\r\n");
}
串口助手开机打印信息:
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
MOTOR STOP!!!!!!!!!!!!!!!!!!!!!!
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
IN TIM1_IRQ
显示的是一直在TIM1中断函数中 没有进入到WHILE
跪求大佬们指导