Convert ultrasonic flight time to distance value
This commit is contained in:
parent
b6b6932c08
commit
9cdd0008a5
@ -13,6 +13,7 @@
|
|||||||
#include "ultrasonic_driver.h"
|
#include "ultrasonic_driver.h"
|
||||||
|
|
||||||
extern uint32_t capture_value;
|
extern uint32_t capture_value;
|
||||||
|
uint16_t distance_uint16;
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief main function
|
\brief main function
|
||||||
@ -52,6 +53,8 @@ int main(void)
|
|||||||
// printf("Send ultra sonic driver signal!\r\n");
|
// printf("Send ultra sonic driver signal!\r\n");
|
||||||
delay_ms(2);
|
delay_ms(2);
|
||||||
printf("cap_val:%ld\n", capture_value);
|
printf("cap_val:%ld\n", capture_value);
|
||||||
|
distance_uint16 = calculate_distance(capture_value);
|
||||||
|
printf("Distance:%d\n", distance_uint16);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -177,3 +177,15 @@ void ultrasonic_echo_timer_config(void)
|
|||||||
timer_icinitpara.icfilter = 0x03; // 设置滤波器
|
timer_icinitpara.icfilter = 0x03; // 设置滤波器
|
||||||
timer_input_capture_config(US_ECHO_TIMER, US_ECHO_CH, &timer_icinitpara);
|
timer_input_capture_config(US_ECHO_TIMER, US_ECHO_CH, &timer_icinitpara);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t calculate_distance(uint32_t us_value) {
|
||||||
|
uint16_t distace = (TIME_CORRECTION_US + us_value) * 17;
|
||||||
|
/*
|
||||||
|
* (TIME_CORRECTION_US + us_value) * 340 m/s
|
||||||
|
* -----------------------------------------
|
||||||
|
* 1000 000
|
||||||
|
* ----------------------------------------------
|
||||||
|
* 2
|
||||||
|
*/
|
||||||
|
return distace;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user