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 ?