Skip to main content
Vvine
Associate II
March 27, 2019
Question

Timer measuring time between state change on two digital inputs

  • March 27, 2019
  • 2 replies
  • 2004 views

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.

This topic has been closed for replies.

2 replies

S.Ma
Principal
March 27, 2019

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.

Vvine
VvineAuthor
Associate II
March 27, 2019

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.

Tesla DeLorean
Guru
March 27, 2019

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 VenmoUp vote any posts that you find helpful, it shows what's working..
Vvine
VvineAuthor
Associate II
March 27, 2019

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.

Tesla DeLorean
Guru
March 27, 2019

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 VenmoUp vote any posts that you find helpful, it shows what's working..