2012-04-03 11:12 AM
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.2012-04-03 11:45 AM
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.2012-04-03 01:50 PM
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 help2012-04-03 03:46 PM
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.2012-04-04 04:22 AM
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.
2012-04-05 11:36 PM
Hi ColdWeather, many thanks for your help, I solved with your suggestion.
best regards