两路超声波输入捕获,一路始终不好用

2019-10-16 01:13发布





下面是程序。大神帮我看看,在开发板原来的基础上改的两路,但是只有一路好用,只有通道一好用,通道二不好用,麻烦帮我看看我是不是哪里出错了,我觉得应该是定时器捕获中断那里出错了,大神帮忙看看呗,麻烦你们了,谢谢
















#include "timer.h"
#include "led.h"
#include "usart.h"



TIM_ICInitTypeDef    TIM5_ICInitStructure;
void TIM5_Cap_Init(u16 arr,u16 psc)
{  
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  

GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0;  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);     

/***************************************/
GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_1;  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_1);      



TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
  

TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1;
   TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
   TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
   TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
   TIM5_ICInitStructure.TIM_ICFilter = 0x00
   TIM_ICInit(TIM5, &TIM5_ICInitStructure);


/***************************************/
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_2;
   TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
   TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
   TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;  
   TIM5_ICInitStructure.TIM_ICFilter = 0x00;
   TIM_ICInit(TIM5, &TIM5_ICInitStructure);
  
  
  
//ÖжϷÖ×é³õʼ»¯
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;  
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
/***************************************/
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC2,ENABLE);/
    TIM_Cmd(TIM5,ENABLE );  
   

}
u8  TIM5CH1_CAPTURE_STA=0;        
u16 TIM5CH1_CAPTURE_VAL;

u8  TIM5CH2_CAPTURE_STA=0;      
u16 TIM5CH2_CAPTURE_VAL;

  
void TIM5_IRQHandler(void)
{
  if((TIM5CH1_CAPTURE_STA&0X80)==0)
{   
  if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
   
  {     
   if(TIM5CH1_CAPTURE_STA&0X40)
   {
    if((TIM5CH1_CAPTURE_STA&0X3F)==0X3F)
    {
     TIM5CH1_CAPTURE_STA|=0X80;
     TIM5CH1_CAPTURE_VAL=0XFFFF;
    }else TIM5CH1_CAPTURE_STA++;
   }  
  }
  
  /////////////////////*//////////////////////////*/
  else if((TIM5CH2_CAPTURE_STA&0X80)==0)
{   
  if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
   
  {     
   if(TIM5CH2_CAPTURE_STA&0X40)
   {
    if((TIM5CH2_CAPTURE_STA&0X3F)==0X3F)
    {
     TIM5CH2_CAPTURE_STA|=0X80;
     TIM5CH2_CAPTURE_VAL=0XFFFF;
    }else TIM5CH2_CAPTURE_STA++;
   }  
  }
///////////////////////////////////////////////////////
  
  if (TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)
  {
   if(TIM5CH1_CAPTURE_STA&0X40)   
   {      
    TIM5CH1_CAPTURE_STA|=0X80;  
    TIM5CH1_CAPTURE_VAL=TIM_GetCapture1(TIM5);
       TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising);
   }else        
   {
    TIM5CH1_CAPTURE_STA=0;
    TIM5CH1_CAPTURE_VAL=0;
     TIM_SetCounter(TIM5,0);
    TIM5CH1_CAPTURE_STA|=0X40;  
       TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Falling);  
   }      
  }                     
  }

/////////////////////////////////////////////////////////
else if (TIM_GetITStatus(TIM5, TIM_IT_CC2) != RESET)
  {
   if(TIM5CH2_CAPTURE_STA&0X40)   
   {      
    TIM5CH2_CAPTURE_STA|=0X80;  
    TIM5CH2_CAPTURE_VAL=TIM_GetCapture2(TIM5);
       TIM_OC2PolarityConfig(TIM5,TIM_ICPolarity_Rising);
   }else         
   {
    TIM5CH2_CAPTURE_STA=0;   
    TIM5CH2_CAPTURE_VAL=0;
     TIM_SetCounter(TIM5,0);
    TIM5CH2_CAPTURE_STA|=0X40;  
       TIM_OC2PolarityConfig(TIM5,TIM_ICPolarity_Falling);
   }      
  }                     
  }

  TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update);
TIM_ClearITPendingBit(TIM5, TIM_IT_CC2|TIM_IT_Update);


}


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