简体   繁体   中英

Calculating distance for STM32F Timer

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:

  1. Configure the timer in capture mode with 5 us ticks (or some other value). Don't start it. Enable capture and update (overflow) event interrupts.
  2. When you want to make a measurement, reset timer counter & start timer & trigger signal transfers (in case of multiple sensors, start all of them at the same time)
  3. Wait for capture event interrupts. This should work same for multiple channels (for multiple sensors).
  4. For each capture interrupt, get the corresponding CCR register value as capture[n] .
  5. Let the timer run. It will trigger the update (overflow) interrupt eventually.
  6. In the update (overflow) interrupt, look for the channels that you didn't get capture interrupts. The ones you didn't get interrupt are out of range. Stop the timer in update interrupt.
  7. For each captured channel, 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,
  • one tick unit is 5us,
  • speed of sound is 340m/s.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM