Assume I have a STM32F4 with a system clock 8MHz, and time(TIM3_PSC = 39). I'm interfacing the ultrasonic sensor HC-SR04
I'm doing timer interrupt and would like to calculate the distance and the MAX distance that can be calculated.
So my work is: 1/8MHz * TIM32_PSC = 0.000000125 * 40 = 5uS is the timer period.
assuming the following code:
float distance; // in m
…
void TIM3_IRQHandler(void)
{
static uint16_t left;
if (…)
{
left = TIM3->CCR1;
}
else
{
uint16_t right = TIM3->CCR1;
distance = ______________________________________________
}
TIM3->SR &= ~TIM_SR_CC1IF;
}
so distance should be = (right - left * 340/2)
is that right?
and for max distance it's MaxDistance = 340m/s / 5uS = 68Meters?
I still don't understand what do left
& rigth
stand for in your code but here is the the way how I would implement it:
capture[n]
.distance = (capture[n] * 5) * 10^-6 * 340
in meters.Dimensional analysis would help. I'm assuming that your 5us/tick scale is correct - I didn't check. You should be easily able to figure that one out. Hint: count ticks between a button press and button release, then hold down a button for one second:)
left
and right
are in timer tick units for one-way distance - you have to divide these values by 2 if they are the forward+reflected distances, Thus:
distance = (right-left) * 5us/tick * 340m/s
[ s m ]
[m] = [ (tick-tick) *------*---]
[ tick s ]
[ tick s m ]
= [------*------*---]
[ 1 tick s ]
[ m ]
= [---] = [m]
[ 1 ]
In C:
// floating point
float distance = (right - left) * 5E-6 * 340.0;
In integers, 1mm would be a reasonable output unit, as there's 1.7mm sound travel per tick. But internal computation needs to be done in dmm
(deca-millimeter = 0.1mm) units to retain accuracy.
// integer, in 1mm units
int distance = ((right - left) * (340*5E-6/1E-4) + 5) / 10;
Dimensional analysis:
[ m s / m ]
[ (tick - tick) *---*------/----- + dmm ]
[ s tick/ dmm ]
[mm] = [----------------------------------------]
[ dmm ]
[ ----- ]
[ mm ]
[ m dmm ]
[ tick *------*----- + dmm ]
[ tick m ]
= [--------------------------]
[ dmm ]
[ ----- ]
[ mm ]
[ mm ]
= [ (dmm + dmm)*----- ] = [mm]
[ dmm ]
Again, 1dmm = 0.1mm.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.