cancel
Showing results for 
Search instead for 
Did you mean: 

delay from two signals

momede
Associate II
Posted on April 03, 2012 at 20:12

Hello at all,

I have to mesaure the delay between 2 signals that I have in input to the microcontroller.

I saw the example of the PWM input.

I supposed to use an OR gate to have both front to the same pin but I think that there is a better solution using the timer in the microcontroller.

Can someone suggest a better idea using the timer to calculate the delay between two signal?

Many thanks for your help.

best regards.
5 REPLIES 5
Posted on April 03, 2012 at 20:45

What kind of frequency and precision are we looking for?

Couldn't you latch the timebase counter into two of the input capture channels, and then compare the delta in counter clock ticks?

It might take some head scratching, and review of the TRM diagrams, but would appear to be within the capabilities of the TIM peripheral.

ST needs some much better, and more complete, TIM examples.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
momede
Associate II
Posted on April 03, 2012 at 22:50

Hi Clive, thanks for your reply.

The question that I posed  isn't right.

My real situation is that I have to mesaure the delay from 2 sensors to calculate the speed of a vehicle triggered from the wheels.

the minimum time is 1 ms.

many thanks for your help

Posted on April 04, 2012 at 00:46

Still sounds awfully much like an A to B measurement against two input signals. Can you diagram it?

If the measurement can be fairly course, one could time stamp two interrupts using the core's cycle counter, but to be honest it makes more since to let a hardware TIMer latch the timebase.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ColdWeather
Senior
Posted on April 04, 2012 at 13:22

My real situation is that I have to mesaure the delay from 2 sensors to calculate the speed of a vehicle triggered from the wheels.

 

the minimum time is 1 ms.

 

 

The solution can be:

- you take for instance TIM2 and set its TIM2_CNT register as a free running up-counter being clocked by CK_CNT (let's say 72MHz) divided by the prescaler value of 719 giving you a 10us (100kHz) base that provides 1% measurement mistake for your shortest pulse distance of 1ms. The maximal possible pulse distance would be about 10us * 65535 = 655ms.

- you confugure the timer channels #1 and #2 for the capture of the external signals - your pulse #1 and #2. Select the capture edge (falling/rising) according to your signal polarity.

- you write an interrupt processing routine that does the following:

  - if it was the channel #1 (first pulse), set a software flag, clear CC1IF bit.

  - if it was the channel #2 (second pulse), and the flag above has been set, compute the pulse

distance as (TIM2_CCR2 - TIM2_CCR1)*10 = microseconds, reset the software flag but set a ready flag for your main loop to inform it about the valid measured value to be processed.

momede
Associate II
Posted on April 06, 2012 at 08:36

Hi ColdWeather, many thanks for your help, I solved with your suggestion.

best regards