#define TRIG_PORT GPIOA //TRIG #define ECHO_PORT GPIOA //ECHO #define TRIG_PIN GPIO_Pin_7 //TRIG #define ECHO_PIN GPIO_Pin_6 //ECHO unsigned short int UltrasonicWave_Distance; void DelayTime_us(int Time) { unsigned char i; for ( ; Time>0; Time--) for ( i = 0; i < 72; i++ ); } void UltrasonicWave_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = TRIG_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(TRIG_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = ECHO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(ECHO_PORT,&GPIO_InitStructure); } void UltrasonicWave_CalculateTime(void) { UltrasonicWave_Distance=TIM_GetCounter(TIM2)*5*34/2000; } int UltrasonicWave_StartMeasure(void) { GPIO_SetBits(TRIG_PORT,TRIG_PIN); delay_us(15); GPIO_ResetBits(TRIG_PORT,TRIG_PIN); while(!GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); TIM_Cmd(TIM2, ENABLE); while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); TIM_Cmd(TIM2, DISABLE); UltrasonicWave_CalculateTime(); TIM_SetCounter(TIM2,0); return (int)UltrasonicWave_Distance/256*100+UltrasonicWave_Distance%256; }解读
时间: 2023-06-27 11:06:25 浏览: 27
这段代码是用于驱动超声波测距模块的。其中TRIG_PIN和ECHO_PIN分别连接超声波模块的发射引脚和接收引脚。在初始化函数中,将这两个引脚设置为输出和输入模式。然后,通过调用UltrasonicWave_StartMeasure函数,发出一个15us的高电平脉冲,触发超声波模块发送一次声波,同时启动计时器TIM2。当声波被物体反射回来到达模块时,引脚ECHO_PIN会输出高电平,此时停止计时器并计算声波的往返时间。最终通过一定的计算,得到物体与超声波模块之间的距离。
相关问题
#define TRIG_PORT GPIOA //TRIG #define ECHO_PORT GPIOA //ECHO #define TRIG_PIN GPIO_Pin_7 //TRIG #define ECHO_PIN GPIO_Pin_6 //ECHO unsigned short int UltrasonicWave_Distance; void DelayTime_us(int Time) { unsigned char i; for ( ; Time>0; Time--) for ( i = 0; i < 72; i++ ); } void UltrasonicWave_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = TRIG_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(TRIG_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = ECHO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(ECHO_PORT,&GPIO_InitStructure); } void UltrasonicWave_CalculateTime(void) { UltrasonicWave_Distance=TIM_GetCounter(TIM2) * 5 * 34/2000; } int UltrasonicWave_StartMeasure(void) { GPIO_SetBits(TRIG_PORT,TRIG_PIN); delay_us(15); GPIO_ResetBits(TRIG_PORT,TRIG_PIN); while(!GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); TIM_Cmd(TIM2, ENABLE); while(GPIO_ReadInputDataBit(ECHO_PORT,ECHO_PIN)); TIM_Cmd(TIM2, DISABLE); UltrasonicWave_CalculateTime(); TIM_SetCounter(TIM2,0); return (int)UltrasonicWave_Distance/256*100+UltrasonicWave_Distance%256; }详细解读各个函数作用
1. `DelayTime_us(int Time)` 函数是用来进行微秒级延时的,其中 Time 是需要延时的时间,具体实现是通过 for 循环来进行计数实现的。
2. `UltrasonicWave_Configuration(void)` 函数是用来配置超声波测距的 GPIO 引脚的,其中 TRIG_PORT 和 ECHO_PORT 分别是触发引脚和回波引脚所在的 GPIO 端口,TRIG_PIN 和 ECHO_PIN 则是对应的引脚号。这个函数会先使能 GPIOA 的时钟,然后根据 TRIG_PIN 和 ECHO_PIN 的设置,将它们分别配置为输出和输入模式。
3. `UltrasonicWave_CalculateTime(void)` 函数是用来计算超声波测距的时间的,这个函数会根据计数器 TIM2 的计数值,通过一定的公式计算出超声波从发送到接收所经历的时间。具体实现是先通过 `TIM_GetCounter(TIM2)` 获取计数器的计数值,然后经过一些计算得到超声波测距的时间。
4. `UltrasonicWave_StartMeasure(void)` 函数是用来开始一次超声波测距的,这个函数会按照超声波测距的规则,先向触发引脚 TRIG_PIN 发送一个 15 微秒的高电平,然后等待回波引脚 ECHO_PIN 的电平变化,从而计算出超声波的时间,最后通过一定的公式得到距离值。具体实现中,这个函数会在发送完触发信号后启动计数器 TIM2,然后等待回波引脚电平变化,再停止计数器,最后调用 `UltrasonicWave_CalculateTime(void)` 函数计算超声波测距的时间,并根据一定的公式将距离值转换为实际距离。
使用stm32f103c8t6,pa6作为trig,pa7作为echo,利用hcsr04测距
好的,针对您的问题,我可以给您提供以下代码作为参考:
```c++
#include "stm32f10x.h"
#include "delay.h"
#define Trig_Pin GPIO_Pin_6
#define Echo_Pin GPIO_Pin_7
#define Trig_Port GPIOA
#define Echo_Port GPIOA
void HC_SR04_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = Trig_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(Trig_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Echo_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(Echo_Port, &GPIO_InitStructure);
}
float HC_SR04_GetDistance(void)
{
uint32_t cnt = 0;
float distance = 0;
GPIO_ResetBits(Trig_Port, Trig_Pin);
Delay_us(2);
GPIO_SetBits(Trig_Port, Trig_Pin);
Delay_us(10);
GPIO_ResetBits(Trig_Port, Trig_Pin);
while(GPIO_ReadInputDataBit(Echo_Port, Echo_Pin) == RESET);
while(GPIO_ReadInputDataBit(Echo_Port, Echo_Pin) == SET)
{
cnt++;
Delay_us(1);
}
distance = cnt * 0.017;
return distance;
}
```
注:以上代码是基于STM32标准库的,需要先进行库文件的导入和配置。同时,需要自行添加头文件delay.h,并实现其中的Delay_us()函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)