找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2762|回复: 7
打印 上一主题 下一主题
收起左侧

为什么stm32f103不能驱动超声波模块hcsr04?

[复制链接]
跳转到指定楼层
楼主
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "hcsr04.h"


        

int main(void)
{        
         float length;
                 delay_init();                     //延时函数初始化         
         uart_init(115200);         //串口初始化为115200
         HCSR04_Init();
         printf("超声波初始化成功");//检测超声波是否初始化
   length=gettime();
         printf("距离为:%.3f cm\n",length);//打印总的距离        
        
        
}        
//以上是主函数模块
#include "hcsr04.h"
#include "delay.h"

u16 msHcCount =0;
void HCSR04_Init(void)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        GPIO_InitTypeDef  GPIO_InitStructure;
        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);         //使能PB端口时钟
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
        
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;                                 //PB.13 端口配置
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                  //推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                 //IO口速度为50MHz
        GPIO_Init(GPIOB, &GPIO_InitStructure);                                         //根据设定参数初始化GPIOB.13

                        
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;                                 //PB.14 端口配置
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;                  //下拉输入
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                 //IO口速度为50MHz
        GPIO_Init(GPIOB, &GPIO_InitStructure);                                         //根据设定参数初始化GPIOB.14

        
                //定时器TIM3初始化
        TIM_TimeBaseStructure.TIM_Period =10000-1; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值        
        TIM_TimeBaseStructure.TIM_Prescaler =7200-1; //设置用来作为TIMx时钟频率除数的预分频值
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位

        TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //使能指定的TIM3中断,允许更新中断

        //中断优先级NVIC设置
               
        
        TIM_ClearFlag(TIM3,TIM_FLAG_Update);
        TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);
        hcsr04_NVIC();
        TIM_Cmd(TIM3,ENABLE);
}

static void opentimerhc()
{
        TIM_SetCounter(TIM2,0);
        msHcCount=0;
        TIM_Cmd (TIM2,ENABLE);
}        
static void closeimerhc()
{
        TIM_Cmd(TIM2,DISABLE);
}
        
        
void hcsr04_NVIC(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置NVIC中断分组2:2位抢占优先级,2位响应优先级

//         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
        NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3中断
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;  //先占优先级1级
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  //从优先级0级
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
        NVIC_Init(&NVIC_InitStructure);  //初始化NVIC寄存器

}


void TIM3_IRQHandler(void)   //TIM3中断
{
        if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //检查TIM3更新中断发生与否
                {
                        TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //清除TIMx更新中断标志
                        msHcCount++;
                }
               
}
u32 gettime(void)//时间获取函数
{
u32 t=0;
        t=msHcCount*1000;
        t+=TIM_GetCounter(TIM2);
        TIM2->CNT=0;
        delay_ms(50);
}
float hcgetlength(void)
{
u32 t=0;
        int i=0;
        float lengthTemp=0;
        float sum=0;
        while(i!=5)
        {
                GPIO_ResetBits(GPIOB,GPIO_Pin_13);                //预拉低trig引脚
                delay_ms(20);
                GPIO_SetBits(GPIOB,GPIO_Pin_13);
                delay_ms(20);
                GPIO_ResetBits(GPIOB,GPIO_Pin_13);//发出脉冲
                while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==0)//等待信号发出,信号发出,计数器置0,同时变为高电平
                {
                        opentimerhc();
                        i=i+1;
                }        
                while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==1)//等待信号接收,信号发出时,引脚一直置1
                {
                        closeimerhc();
                        t=gettime();
                        lengthTemp=((float)t*170/1000);
                        sum=lengthTemp+sum;
                }        
        lengthTemp=sum/5.0;
                return lengthTemp;
        }
}
这是超声波模块


是程序写错了吗?
如果有人能指出错误,请私我
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:57657 发表于 2021-8-12 17:46 | 只看该作者
具体请用逻辑分析仪检测一下波形、时序是否正确。
回复

使用道具 举报

板凳
ID:592807 发表于 2021-8-13 13:38 | 只看该作者

   length=gettime();
  printf("距离为:%.3f cm\n",length);//打印总的距离

u32 gettime(void)//时间获取函数
{
u32 t=0;
        t=msHcCount*1000;
        t+=TIM_GetCounter(TIM2);
        TIM2->CNT=0;
        delay_ms(50);
}
没有renturn数值  你获取时间获取了个寂寞
回复

使用道具 举报

地板
ID:660005 发表于 2021-8-13 16:33 | 只看该作者
黄youhui 发表于 2021-8-13 13:38
length=gettime();
  printf("距离为:%.3f cm\n",length);//打印总的距离

就算加上return t,串口助手还是没得反应
回复

使用道具 举报

5#
ID:660005 发表于 2021-8-13 16:34 | 只看该作者
黄youhui 发表于 2021-8-13 13:38
length=gettime();
  printf("距离为:%.3f cm\n",length);//打印总的距离

这个连最开始的“超声波初始化”都没得!
回复

使用道具 举报

6#
ID:592807 发表于 2021-8-13 20:32 | 只看该作者
123689 发表于 2021-8-13 16:34
这个连最开始的“超声波初始化”都没得!

我找时间试一下。买了买还用

P10813-202936.jpg (2.46 MB, 下载次数: 114)

P10813-202936.jpg
回复

使用道具 举报

7#
ID:592807 发表于 2021-8-14 12:29 | 只看该作者


u8 combuff[2048];
u8 combuff1[2048];


int main(void)
{
        double cm_length = 0;
        u8 i = 0;
       
        delay_init();
        USART1_Init(9600);
        TIM3_Init(1,1);//1ms
        TIM16_Init(1,1);//1ms
        HC_RC04_Init();
        while(1)
        {
                delay_ms(1000);delay_ms(1000);delay_ms(1000);delay_ms(1000);delay_ms(1000);
                for(i = 0;i<5;i++)
                {
                        cm_length += (Start_HC_RC04()/5);
                }
//length * 2 /340
                cm_length = (cm_length / 170) * 100;
                u1_printf("距离 = %1f\r\n",cm_length);
                cm_length=0;
                delay_ms(1000);delay_ms(1000);delay_ms(1000);delay_ms(1000);delay_ms(1000);               
        }
}


/*
GPIO        初始化
VCC = 3.3V
GND = 0V
TRIG = PA7   
ECHO = PA6   
*/


void HC_RC04_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
       
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);  //使能GPIOA的时钟
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA, &GPIO_InitStructure);


        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_Init(GPIOA, &GPIO_InitStructure);


}


u16 msHC_RC04;


/*
1ms中断
*/
void TIM3_IRQHandler(void)
{
        u8 i;
       
  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  {
                msHC_RC04+=1;


    TIM_ClearFlag(TIM3, TIM_FLAG_Update);       
               
        }

}
/*
ECHO(PA6) 先确保        ECH处于低电平
TRIG(PA7) 先拉高20us以上、
ECHO (PA6)  电平被拉高后开启定时器
ECHO (PA6)  电平拉低后关闭定时器并且读取时间


测试距离 = (高电平时间(S) * 340M/S)/2
模块测距范围2-400cm
因此高电平时间范围0.1ms - 23ms


*/
u32 Start_HC_RC04(void)
{
        u16 time_out = 0;
        u32 usTime = 0;
       
        GPIO_ResetBits(GPIOA,GPIO_Pin_7);
        delay_us(20);
        while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) != 0)
                {
                        delay_ms(1);
                        time_out += 1;
                        if(time_out >= 1000)
                                u1_printf("超时\r\t");
                                return 0;               
                }
               
                GPIO_SetBits(GPIOA,GPIO_Pin_7);
                delay_us(20);
                GPIO_ResetBits(GPIOA,GPIO_Pin_7);//触发信号
                while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == 0)
                {
                        ;
                }
               
                msHC_RC04 = 0;
                TIM_Cmd(TIM3, ENABLE);
                while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6) == 1)
                {
                        ;
                }
                TIM_Cmd(TIM3, DISABLE);
                usTime = msHC_RC04 * 1000;
                usTime += (TIM_GetCounter(TIM3)*100);//时钟1S 10000节拍。1节拍0.1ms 100us
                TIM1->CNT = 0;
               
                return usTime;
               




}








时间和距离的转换没您好,也没怎么滤波。

51hei截图20210814122750.png (22.81 KB, 下载次数: 116)

51hei截图20210814122750.png
回复

使用道具 举报

8#
ID:160860 发表于 2021-8-17 12:09 | 只看该作者
我这里有一个工程你看一下


#include "wave.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"

#define Trig GPIO_Pin_7//超声发射引脚pb7
#define Echo GPIO_Pin_6//超声波接受引脚pb6

void Wave_SRD_Init(void)
{
        GPIO_InitTypeDef GPIO_InitStruct;
        EXTI_InitTypeDef EXTI_InitStruct;
        NVIC_InitTypeDef NVIC_InitStruct;
        //初始化超声波
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB,ENABLE);
       
        GPIO_InitStruct.GPIO_Pin=Trig;
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;
        GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_Init(GPIOB,&GPIO_InitStruct);
       
        GPIO_InitStruct.GPIO_Pin=Echo;
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPD;
        GPIO_Init(GPIOB,&GPIO_InitStruct);
        //外部中断与引脚的配置,中断与端口6的映射
        GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6);
        //外部中断配置
        EXTI_InitStruct.EXTI_Line=EXTI_Line6;
        EXTI_InitStruct.EXTI_LineCmd= ENABLE;
        EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt;
        EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Rising;
        EXTI_Init(&EXTI_InitStruct);
        //中断优先级管理
        NVIC_InitStruct.NVIC_IRQChannel=EXTI9_5_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority=0;
        NVIC_Init(&NVIC_InitStruct);
       
       
}
//外部中断服务函数
void EXTI9_5_IRQHandler(void)
{
        delay_us(10);
        if(EXTI_GetITStatus(EXTI_Line6)!=RESET)//Echo检测到高电平
        {
                TIM_SetCounter(TIM3,0);//开启定时器
                TIM_Cmd(TIM3,ENABLE);//使能定时器
                while(GPIO_ReadInputDataBit(GPIOB,Echo));//等待Echo出现低电平
               
                TIM_Cmd(TIM3,DISABLE);//关闭定时器
               
        EXTI_ClearITPendingBit(EXTI_Line6);//清除中断标志
  }
}
//超声波工作条件
//Trig产生一个不低于10us的高电平信号

u8 Wave_SRD_Strat(u8 time)
{
        if(time>10)
        {
        GPIO_SetBits(GPIOB,Trig);//设置Trig为高电平输出
        delay_us(time);
        GPIO_ResetBits(GPIOB,Trig);
        return 0;//产生高于10us高电平,返回0
        }
        else return 1;//为产生高于10us高电平返回1
}

主函数里
初始化:
        Wave_SRD_Init();//初始化超声波
        while(Wave_SRD_Strat(t))
        {
        }

while循环:
//距离计算
                Wave_SRD_Strat(t);                             //产生高于10Us的高电平与OLED上显示超声波所测距离
                Juli = TIM_GetCounter(TIM3)*340/200.0;
                printf("%f",Juli);
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表