STM32F407输入捕获问题

2019-07-20 05:44发布

我按照例程改的,用的TIM4的通道2,捕获不到数,求指导
int main(void)

long temp=0;  
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(168);  
uart_init(115200);

  PWM_TIM1_Init(105,100,50);        
  TIM4_CH2_Cap_Init(0XFFFF,84-1); 
    while(1)
{
      delay_ms(10); 
       if(TIM4CH2_CAPTURE_STA&0X80)       
{
temp=TIM4CH2_CAPTURE_STA&0X3F; 
temp*=0XFFFF;             
temp+=TIM4CH2_CAPTURE_VAL; 
printf("HIGH:%ld us ",temp); 
TIM4CH2_CAPTURE_STA=0;        
}
}
}

void TIM4_CH2_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);   
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; 
GPIO_Init(GPIOD,&GPIO_InitStructure);

GPIO_PinAFConfig(GPIOD,GPIO_PinSource13,GPIO_AF_TIM4); 
  
 
TIM_TimeBaseStructure.TIM_Prescaler=psc;  
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; 
TIM_TimeBaseStructure.TIM_Period=arr; 
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; 

TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);


TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; 
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; 
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; 
  TIM_ICInitStructure.TIM_ICFilter = 0x00;
  TIM_ICInit(TIM4, &TIM_ICInitStructure);

TIM_ITConfig(TIM4,TIM_IT_Update|TIM_IT_CC2,ENABLE);

  TIM_Cmd(TIM4,ENABLE );

 
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); 


}

u8  TIM4CH2_CAPTURE_STA=0; 
u16 TIM4CH2_CAPTURE_VAL; 

void TIM4_IRQHandler(void)
{    

  if((TIM4CH2_CAPTURE_STA&0X80)==0)
{
if(TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)
{     
if(TIM4CH2_CAPTURE_STA&0X40)
{
if((TIM4CH2_CAPTURE_STA&0X3F)==0X3F)
{
TIM4CH2_CAPTURE_STA|=0X80; 
TIM4CH2_CAPTURE_VAL=0XFFFF;
}else TIM4CH2_CAPTURE_STA++;
}  
}
if(TIM_GetITStatus(TIM4, TIM_IT_CC2) != RESET)
{
if(TIM4CH2_CAPTURE_STA&0X40) 
{  
TIM4CH2_CAPTURE_STA|=0X80; 
 TIM4CH2_CAPTURE_VAL=TIM_GetCapture1(TIM4);
TIM_OC2PolarityConfig(TIM4,TIM_ICPolarity_Rising); 
}else  
{
TIM4CH2_CAPTURE_STA=0; 
TIM4CH2_CAPTURE_VAL=0;
TIM4CH2_CAPTURE_STA|=0X40; 
TIM_Cmd(TIM4,DISABLE ); 
TIM_SetCounter(TIM4,0);
TIM_OC1PolarityConfig(TIM4,TIM_ICPolarity_Falling);
TIM_Cmd(TIM4,ENABLE );
}    
}           
  }
TIM_ClearITPendingBit(TIM4, TIM_IT_CC2|TIM_IT_Update); 
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。