修改超声波测距模块,仅返回毫米距离
This commit is contained in:
parent
a26d865e06
commit
0bce1acabd
|
@ -3,7 +3,6 @@
|
||||||
|
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
int16_t sonar_mm(void);
|
uint16_t sonar(void);
|
||||||
float sonar(void);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,54 +1,28 @@
|
||||||
#include "hcsr04.h"
|
#include "hcsr04.h"
|
||||||
#include "tim.h"
|
#include "tim.h"
|
||||||
|
|
||||||
// time 的单位是 μs
|
// time 的单位是 1μs
|
||||||
static uint64_t time; // 声明变量,用来计时
|
static volatile uint64_t time; // 声明变量,用来计时
|
||||||
static uint64_t time_end; // 声明变量,存储回波信号时间
|
static uint64_t time_end; // 声明变量,存储回波信号时间
|
||||||
static uint8_t state = 1;
|
static uint8_t state = 0;
|
||||||
|
|
||||||
// HACK 临时的 delay 解决方法
|
// HACK 临时的 delay 解决方法
|
||||||
void delay_us(uint16_t us);
|
void delay_us(uint16_t us);
|
||||||
|
|
||||||
int16_t sonar_mm(void) // 测距并返回单位为毫米的距离结果
|
uint16_t sonar(void) // 测距并返回单位为毫米的距离结果
|
||||||
{
|
{
|
||||||
uint32_t distance, distance_mm = 0;
|
// 超时则返回5m距离
|
||||||
|
uint32_t distance = 5000;
|
||||||
HAL_GPIO_WritePin(SCL_C_GPIO_Port, SCL_C_Pin, GPIO_PIN_SET); // 输出高电平
|
HAL_GPIO_WritePin(SCL_C_GPIO_Port, SCL_C_Pin, GPIO_PIN_SET); // 输出高电平
|
||||||
delay_us(15); // 延时15微秒
|
delay_us(15); // 延时15微秒
|
||||||
HAL_GPIO_WritePin(SCL_C_GPIO_Port, SCL_C_Pin, GPIO_PIN_RESET); // 输出低电平
|
HAL_GPIO_WritePin(SCL_C_GPIO_Port, SCL_C_Pin, GPIO_PIN_RESET); // 输出低电平
|
||||||
|
|
||||||
while (state)
|
// HACK 考虑RTOS优化
|
||||||
;
|
HAL_Delay(15);
|
||||||
state = 1;
|
|
||||||
|
|
||||||
// BUG 现在 time 的单位是 1μs
|
if (state)
|
||||||
if (time_end / 100 < 38) // 判断是否小于38毫秒,大于38毫秒的就是超时,直接调到下面返回0
|
distance = time_end * 346 / 2000; // 计算距离,25°C空气中的音速为346m/s
|
||||||
{
|
return distance;
|
||||||
distance = (time_end * 346) / 2; // 计算距离,25°C空气中的音速为346m/s
|
|
||||||
distance_mm = distance / 100; // 因为上面的time_end的单位是10微秒,所以要得出单位为毫米的距离结果,还得除以100
|
|
||||||
}
|
|
||||||
return distance_mm; // 返回测距结果
|
|
||||||
}
|
|
||||||
|
|
||||||
float sonar(void) // 测距并返回单位为米的距离结果
|
|
||||||
{
|
|
||||||
uint32_t distance, distance_mm = 0;
|
|
||||||
float distance_m = 0;
|
|
||||||
HAL_GPIO_WritePin(SCL_C_GPIO_Port, SCL_C_Pin, GPIO_PIN_SET); // 输出高电平
|
|
||||||
delay_us(15); // 延时15微秒
|
|
||||||
HAL_GPIO_WritePin(SCL_C_GPIO_Port, SCL_C_Pin, GPIO_PIN_RESET); // 输出低电平
|
|
||||||
|
|
||||||
while (state)
|
|
||||||
;
|
|
||||||
state = 1;
|
|
||||||
|
|
||||||
// BUG 现在 time 的单位是 1μs
|
|
||||||
if (time_end / 100 < 38)
|
|
||||||
{
|
|
||||||
distance = (time_end * 346) / 2;
|
|
||||||
distance_mm = distance / 100;
|
|
||||||
distance_m = distance_mm / 1000;
|
|
||||||
}
|
|
||||||
return distance_m;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void delay_us(uint16_t us)
|
void delay_us(uint16_t us)
|
||||||
|
@ -72,14 +46,14 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||||
{
|
{
|
||||||
if (HAL_GPIO_ReadPin(SDA_C_EXTI12_GPIO_Port, SDA_C_EXTI12_Pin))
|
if (HAL_GPIO_ReadPin(SDA_C_EXTI12_GPIO_Port, SDA_C_EXTI12_Pin))
|
||||||
{
|
{
|
||||||
HAL_TIM_Base_Start_IT(&htim2);
|
|
||||||
time = 0;
|
time = 0;
|
||||||
|
HAL_TIM_Base_Start_IT(&htim2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
time_end = time;
|
time_end = time;
|
||||||
HAL_TIM_Base_Stop_IT(&htim2);
|
HAL_TIM_Base_Stop_IT(&htim2);
|
||||||
state = 0;
|
state = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user