Heyyana 2022-11-21 10:09 采纳率: 66.7%
浏览 295
已结题

输入捕获的超声波测距一直为0

最近在搞一个rct6超声波测距有关的项目 结果调了一天 测出来的距离一直都是0 一开始是用外部中断 后来改成了输入捕获 结果还是输出0
这是定时器输入捕获的代码

void TIM3IC_Init()
{
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    
    GPIO_InitTypeDef  GPIO_InitStructure;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

    TIM_InternalClockConfig(TIM3);
    
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
    TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInitStructure.TIM_Period = 65535 - 1;
    TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1 ;
    TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
    
    TIM_ICInitTypeDef TIM_ICInitStructure;
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStructure.TIM_ICFilter = 0xF;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);
    
    
    TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
    TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
    
    TIM_ITConfig(TIM3,TIM_IT_CC2|TIM_IT_CC1,ENABLE);
    TIM_SetCounter(TIM2,0);
    
    
    TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);

    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);
    
  TIM_Cmd(TIM3,ENABLE);
    
}

void TIM3_IRQHandler()
{

    if(TIM_GetITStatus(TIM3,TIM_IT_CC2)!=RESET)
        {           
           Distance = (TIM_GetCapture2(TIM3)+1) * 340/20000.0; 
             Flag = 1;
       //printf("Distance = %f m\r\n",Distance);            
        }
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC2);
}

这是配置超声波引脚的代码

float Distance;
int Flag = 1,ICAP1value,ICAP2value;
 
 
void HC_SR04_init(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);     
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;                 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;         
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;         
    GPIO_Init(GPIOA, &GPIO_InitStructure);                                         
 
}
    

void HC_SR04_start(void)
{
    GPIO_SetBits(GPIOA,GPIO_Pin_4);
    Delay_us(20);
    GPIO_ResetBits(GPIOA,GPIO_Pin_4);
    Delay_ms(60);
}

输出结果一直为0 串口方面测试过是没问题的 引脚也可以正常输出 但不知道自己的逻辑错在哪了 希望带佬们指导一下

  • 写回答

3条回答 默认 最新

  • fengyuzhe13 嵌入式领域优质创作者 2022-11-22 15:59
    关注

    我的代码经测试没有问题,可以借鉴:

    #include "stm32f10x.h"    //在该头文件中默认定义系统时钟为72M
    #include "delay.h"
    #include "sys.h"
    #include "usart.h"
    #include "ultrasonic.h" 
        
    
    
     int main(void)
     {        
       RCC_Configuration();
        delay_init();             //延时函数初始化      
        NVIC_Configuration();      //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
        uart1_init(9600);     //串口初始化为9600
        Ultrasonic_GPIO_Conf();
        EXTI_Configuration();
        Timer_Configuration();
        init_Ultrasonic();
         while(1)
        {
    //        USART1_SendData('*');
    //      USART1_SendData('\r');
    //      USART1_SendData('\n');
    //        Showstring("Distance:");
    //      delay_ms(800);
    //        GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)0);
        }     
     }
    
    
    
    

    ultrasonic.c

    #include "stm32f10x.h"    //在该头文件中默认定义系统时钟为72M
    #include "delay.h"
    #include "sys.h"
    #include "usart.h"
    #include "ultrasonic.h"
    
    unsigned char SFlag=0;
    unsigned long Distance=0;
        
    void Ultrasonic_GPIO_Conf(void)           
    {
        GPIO_InitTypeDef GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//必须先开启复用时钟,然后才能重映射或者外中断
        GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);  // 改变指定管脚的映射 GPIO_Remap_SWJ_JTAGDisable ,JTAG-DP 禁用 + SW-DP 使能
        
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);          //发射引脚初始化
    
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOA, &GPIO_InitStructure);          //接收引脚初始化
        
    }
    void Timer_Configuration(void)
    {
      TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
      NVIC_InitTypeDef NVIC_InitStructure; 
        
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
        
      TIM_DeInit(TIM1);
      TIM_TimeBaseStructure.TIM_Period=20000;         //ARR的值, 一次计时200ms    最大值为34米
      TIM_TimeBaseStructure.TIM_Prescaler=719;         //719+1=720
      TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //采样分频
      TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
      TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
    
      TIM1->DIER|=1<<0;   //允许更新中断                
      TIM1->DIER|=1<<6;   //允许触发中断       
    
      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); 
     
      NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;       
      NVIC_Init(&NVIC_InitStructure);
    }
    //定时器溢出中断服务程序     
    void TIM1_UP_IRQHandler(void)
    {          
    //USART1_SendData('1');    
        if(TIM1->SR&0X0001)//溢出中断
        {    
          Showstring("Out of distance! ");    
          GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)1);//高电平触发 
            delay_us(20);//如果不加延时,系统不稳定
           GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)0);
          TIM_SetCounter(TIM1,0);
          TIM_Cmd(TIM1, ENABLE);
          SFlag=0;     //取消当前上升沿标志                                                                
        }                   
        TIM1->SR&=~(1<<0);//清除中断标志位         
    }
    
        unsigned long d=3400;
        unsigned char i=0;
    void EXTI15_10_IRQHandler(void)         //当前是设置的电平变化就进入中断
    {
    //    USART1_SendData('Q');
       if(EXTI_GetITStatus(EXTI_Line12) != RESET)
       {
             
         if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12)==1)      //接收到上升沿,开始计时
         {
               TIM_SetCounter(TIM1,0);
            TIM_Cmd(TIM1, ENABLE);
            SFlag=1;         //表示经过了上升沿
         }
         else if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_12)==0 && SFlag==1)
         {
                 TIM_Cmd(TIM1, DISABLE);
                 Distance=(int)(((float)TIM_GetCounter(TIM1))*1.7);    //如果测距是2.5m则需15ms
                 if(d>Distance)       //只要最小值
                     d=Distance;
                 i++;
                 if(i==3)                 //测量三次,只要最小值
                 {
                     i=0;
                     Showstring("Distance:");
                     Show_u16(d);
                     Showstring("mm  ");
                     USART1_SendData('\r');
                     USART1_SendData('\n');
                     delay_ms(300);
                     d=34000;
                    }
                    GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)1);//高电平触发 
                    delay_us(20);//如果不加延时,系统不稳定
                    GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)0);
                    TIM_SetCounter(TIM1,0);
                    TIM_Cmd(TIM1, ENABLE);
                    SFlag=0;
          }
          EXTI_ClearITPendingBit(EXTI_Line12);
       }
    }
    void init_Ultrasonic(void)
    {
         delay_ms(100); //为超声波稳定争取时间
        GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)0);//先设置成低电平
        delay_ms(100);//
        GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)1); //高电平触发
        delay_us(40);//高电平至少10us
        GPIO_WriteBit(GPIOA, GPIO_Pin_15, (BitAction)0);//降回低电平
        TIM_SetCounter(TIM1,0);
        TIM_Cmd(TIM1, ENABLE);
    }
    void EXTI_Configuration(void)
    {
      EXTI_InitTypeDef   EXTI_InitStructure;
      NVIC_InitTypeDef   NVIC_InitStructure;
    
      /* Enable GPIOB clock */
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    /* Enable AFIO clock */
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
      /* Configure PB.09 pin as input floating */
        
      /* Connect EXTI9 Line to PB.09 pin */
      GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource12);
      /* Configure EXTI9 line */
      EXTI_InitStructure.EXTI_Line = EXTI_Line12;
      EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
      EXTI_InitStructure.EXTI_Trigger =EXTI_Trigger_Rising_Falling ;//EXTI_Trigger_Falling  EXTI_Trigger_Rising_Falling
      EXTI_InitStructure.EXTI_LineCmd = ENABLE;
      EXTI_Init(&EXTI_InitStructure);
     
      /* Enable and set EXTI9_5 Interrupt to the lowest priority */
      NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
      NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;//最高优先级
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    
      NVIC_Init(&NVIC_InitStructure);
    }
    
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月30日
  • 已采纳回答 11月22日
  • 修改了问题 11月21日
  • 赞助了问题酬金20元 11月21日
  • 展开全部

悬赏问题

  • ¥15 请使用蚁群算法解决下列问题,并给出我完整的MATLAB代码
  • ¥20 关于php录入完成后,批量更新数据库
  • ¥15 请教往复密封润滑问题
  • ¥15 cocos creator发布ios包
  • ¥15 comsol压电材料数据
  • ¥35 用python实现除法算法中goldschmidt算法
  • ¥15 汇编代码转换成C代码
  • ¥15 除法算法中的归一化具体是怎么变的?
  • ¥20 集成电路的逻辑电路和晶体管简化图
  • ¥15 下载windows builder后的问题