cancel
Showing results for 
Search instead for 
Did you mean: 

Timer measuring time between state change on two digital inputs

Vvine
Associate II

I am using STM32F070 processor. I want to implement a function where I should measure the time difference between the change of input level on two digital IOs. There is no fixed sequence of the change in level on both the IOs. i.e. any one of the two IOs can change the state first and then the second one.

I want to know if I could use any conifguration of timer to achieve this? In other words, can I use any of the processor timer to start/record a time on first level change on one input and stop/record time on second level change on another input.

Thanks.

6 REPLIES 6
S.Ma
Principal

Define the longest time between toggle of both IOs. This will be the timer overflow period.

If you can, use 32 bit timer.

Then use channel 1 and 2 as input capture. The timer counts up freely and overflow at max value.

Once an edge is detected, a timestamp is captured from the timer and generate an interrupt.

There you can save this value (and previous ones) in a buffer. Same for the other channels.

Then you've got enough data to cook the delay between 2 edges.

To what level of accuracy?

At a simple level you could use the millisecond count from SysTick/HAL, and use a pin in EXTI mode to cause an interrupt, you'd log the time, and remember the prior time, and do a simple B-A computation of the delta time between events.

The TIM offer an Input Capture mode that functions similarly, where you'd pull the tick count from TIMx->CCR1 and delta against a previous one, or the value in the second channel in indirect mode capturing the opposite edge. The 16-bit TIM of the F0 will be limiting factors.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

Thanks. I might use the second part of your suggestion as the maximum delay allowed in this case is 25us, beyond which it is an error condition.

Vvine
Associate II

Thanks. Actually I dont want first interrupt after the first capture as the application has time contraints. But I will see what is possible with timer.

You might also want to look at the PWM Input methods, which pair CH1/CH2 to provide period and duty. ie time between two rising edges, and time between a rising and subsequent falling edge, or the converse.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..

A bit into the implementation, i noticed that the IO that I am talking about has to be configured in alternate function for TIMx in order to use as capture/PWM input. However, in my case, these pins are already configured as two SPI Rx. Infact I am measuring the difference between two SPI receptions. When I posted my query, I was thinking that may be the first toggle of the Rx pin could capture a timestamp in TIMx. Now this assumption seems wrong to me, as configuring two Alternate Functions seems to be not possible. Any suggestion how could I timestamp start of two SPI receptions?

Thanks.