TIM1输入捕获为什么进不了中断?

2019-07-21 08:53发布

原子大哥,请教一下,我在用TIM1的PWM输入捕获功能测周期和占空比的时候,进入不了定时器中断函数处理函数,这是什么原因啊? 其中PWM脉冲是由TIM3产生的10kHz脉冲。
程序如下:
void Capture(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;
 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 TIM_ICInitTypeDef TIM_ICInitStructure;

 GPIO_InitStructure.GPIO_Pin= GPIO_Pin_8;
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
 GPIO_Init(GPIOA,&GPIO_InitStructure);
 GPIO_ResetBits(GPIOA, GPIO_Pin_8);

 TIM_DeInit(TIM1);   //将外设TIM1寄存器重设为默认值
 /*设置时间基准*/
 TIM_TimeBaseStructure.TIM_Prescaler=0;  //时钟频率的预分频值
 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;  
 TIM_TimeBaseStructure.TIM_Period=7200-1;  //自动重装载寄存器周期值 
 TIM_TimeBaseStructure.TIM_ClockDivision=0x0; //时钟分割值
 TIM_TimeBaseStructure.TIM_RepetitionCounter=0x0;
 TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);  //初始化TIM1的时间基数数据
 TIM_ICInitStructure.TIM_Channel= TIM_Channel_1;//选择通道2
 TIM_ICInitStructure.TIM_ICPolarity= TIM_ICPolarity_Rising;//上升沿
 TIM_ICInitStructure.TIM_ICSelection= TIM_ICSelection_IndirectTI;//通道方向选择
 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;/*每次检测到捕获输入就触发一次捕获 */
 TIM_ICInitStructure.TIM_ICFilter=0x00;/* 对分频率没有影响 选择输入比较滤波器*/
 TIM_PWMIConfig(TIM1, &TIM_ICInitStructure);                 //根据参数配置TIM外设信息
 TIM_SelectInputTrigger(TIM1, TIM_TS_TI1FP1);                //选择IC1为始终触发源
  TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Reset);//TIM从模式:触发信号的上升沿重新初始化计数器和触发寄存器的更新事件
 TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable); //启动定时器的被动触发   
 TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);     //打开中断   TIM_Cmd(TIM1,ENABLE);
}


void TIM1_IRQHandler(void)
{  printf("进入TIM1_IRQHandler ");
 TIM_ClearFlag(TIM1, TIM_IT_CC1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
25条回答
正点原子
1楼-- · 2019-07-22 00:59
那就是了.
你在初始化的时候,清零一下SR先.
yanwuhen620
2楼-- · 2019-07-22 04:09
 精彩回答 2  元偷偷看……
tim4146
3楼-- · 2019-07-22 05:34
void TIM1_IRQHandler(void) 


 printf("进入TIM1_IRQHandler ");
 TIM_ClearFlag(TIM1, TIM_IT_CC1);


这里出了问题。
函数名不是这样的
资料说:
                EXPORT  TIM1_BRK_IRQHandler        [WEAK]
                EXPORT  TIM1_UP_IRQHandler         [WEAK]
                EXPORT  TIM1_TRG_COM_IRQHandler    [WEAK]
                EXPORT  TIM1_CC_IRQHandler         [WEAK]
                EXPORT  TIM2_IRQHandler            [WEAK]
                EXPORT  TIM3_IRQHandler            [WEAK]
                EXPORT  TIM4_IRQHandler            [WEAK]
显然TIM1的中断和其他不一样,所以中断函数改成:
void TIM1_CC_IRQHandler(void)
就没有问题了
hzno1
4楼-- · 2019-07-22 06:55
未使能时钟吧? RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
lazy_pig
5楼-- · 2019-07-22 08:05
回复【11楼】hzno1:
---------------------------------
我的时钟也使能啦,函数名也是TIM1_CC_IRQHandler(void) ,就是进不来捕捉中断,在中断里面把一个LED3熄灭,一直不成功,请大家指点下吧。
void TIM1_CC_Config( u16 arr,u16 psc )
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM1_ICInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd( RCC_APB2Periph_TIM1, ENABLE ); //????TIM1?±??
  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE, ENABLE );  //????GPIOE?±??

GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_13;  //PE13 ?????®?°?è??  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   //GPIO_Mode_IPD; //PE13 ????  
GPIO_Init( GPIOE, &GPIO_InitStructure );
GPIO_ResetBits( GPIOE,GPIO_Pin_13 ); //PE13????

//???????¨?±?÷1 TIM1  
TIM_TimeBaseStructure.TIM_Period = arr; //?è?¨?????÷×?????×°?? 
TIM_TimeBaseStructure.TIM_Prescaler =psc; //?¤·????÷   
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //?è???±??·???:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM?ò??????????
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseStructure ); //?ù??TIM_TimeBaseInitStruct?????¨????????????TIMx???±???ù??????
  
//??????TIM1????????????
TIM1_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 ?????????? IC1??????TI1??
  TIM1_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //??????????
  TIM1_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //??????TI1??
  TIM1_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //????????·???,??·??? 
  TIM1_ICInitStructure.TIM_ICFilter = 0x00;     //IC1F=0000 ???????????¨?÷ ?????¨
  TIM_ICInit( TIM1, &TIM1_ICInitStructure );

//????·?×é??????
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;  //TIM3????
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  //??????????2??
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //????????0??
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??±?????
NVIC_Init( &NVIC_InitStructure );  //?ù??NVIC_InitStruct?????¨???????????????èNVIC?????÷ 

TIM_ITConfig( TIM1,TIM_IT_Update | TIM_IT_CC3,ENABLE );//???í?ü?????? ,???íCC1IE????????

  TIM_Cmd( TIM1,ENABLE ); //?????¨?±?÷5
}
/************************************************************************************
**?? ?? ??: TIM1_CC_IRQHandler( void )
**??    ??: TIM1????·???????
**????????: ??
**????????: ??
************************************************************************************/
u8  TIM1CH1_CAPTURE_STA = 0; //????????×???    
u16 TIM1CH1_CAPTURE_VAL; //??????????
 
//?¨?±?÷5????·??????ò  
void TIM1_CC_IRQHandler( void )

  LED3_OFF( );
  if(( TIM1CH1_CAPTURE_STA & 0x80 ) == 0 )//????????????
{  
if( TIM_GetITStatus( TIM1, TIM_IT_Update ) != RESET )
{    
if( TIM1CH1_CAPTURE_STA & 0X40 )//??????????????????
{
if(( TIM1CH1_CAPTURE_STA & 0x3F ) == 0x3F )//?????????¤??
{
TIM1CH1_CAPTURE_STA |= 0x80;//±ê????????????????
TIM1CH1_CAPTURE_VAL = 0xFFFF;
}
else 
{
TIM1CH1_CAPTURE_STA++;
}
}  
}
 if( TIM_GetITStatus( TIM1, TIM_IT_CC1 ) != RESET )//????1·??ú????????
 {
 if( TIM1CH1_CAPTURE_STA & 0x40 ) //????????????????
 {  
 TIM1CH1_CAPTURE_STA |= 0x80; //±ê??????????????????????
 TIM1CH1_CAPTURE_VAL = TIM_GetCapture1( TIM1 );
   TIM_OC1PolarityConfig( TIM1,TIM_ICPolarity_Rising ); //CC1P=0 ?è??????????????
 }
 else   //????????,????????????????
 {
 TIM1CH1_CAPTURE_STA = 0; //????
 TIM1CH1_CAPTURE_VAL = 0;
 TIM_SetCounter( TIM1,0 );
 TIM1CH1_CAPTURE_STA |= 0x40; //±ê????????????????
   TIM_OC1PolarityConfig( TIM1,TIM_ICPolarity_Falling ); //CC1P=1 ?è??????????????
 }    
 }           
  }
  TIM_ClearITPendingBit( TIM1, TIM_IT_CC3 | TIM_IT_Update ); //????????±ê????
}



处理器是STM32F103VBT6,PE13
雨思小米粥
6楼-- · 2019-07-22 09:38
现在也是这个问题,我配置的也是PA8,tim1ch1,,,终端里面如果用UP的话可以读出数据,但是明显不对,如果用CC的话进进入不了中断!

一周热门 更多>