2
\$\begingroup\$

I am trying to measure the speed of a DC motor using IR sensor. My motor had a disc attached to it with only 1/4th part of it reflecting. I wanted to enable the interrupt at both falling and rising edge so the interrupt routine will be called twice in that 1/4th reflecting region whose distance I already know.

My ISR will enable the timer the first time around and disable it the next time the interrupt is called. The problem (which I found by studying the behavior of a variable) is that when I make the sensor react quickly, my interrupt is generated only once and not when the reflecting surface disappears. But, when I move something slowly in front of it, then both occur perfectly.

So, my conclusion is that my interrupt is not reacting fast enough.What do you think is the problem?

\$\endgroup\$
1
  • \$\begingroup\$ You need to see the input signal waveform first before blaming the speed of interrupt routine. Then you need to check the speed of interrupt by applying known good input, and set another GPIO from inside your interrupt processing routine, and look at timing of signal edges. Which oscilloscope model do you use when debugging your code? \$\endgroup\$ Oct 27, 2017 at 5:35

1 Answer 1

2
\$\begingroup\$

My ISR will enable the timer the first time around and disable it the next time the interrupt is called.

This is not the correct method to measure fast time signals. The microcontroller has timers, and these timers have capture mode.
In capture mode, a rising/falling edge of the input causes the actual timer value to be stored in the capture register. You will have until the next edge to read the timestamp of the edge.
When you have two timestamps you can calculate the time in between.

This way you are not dependent on the interrupt latency and jitter.

The problem (which I found by studying the behavior of a variable) is that when I make the sensor react quickly, my interrupt is generated only once and not when the reflecting surface disappears.

You might have an electrical problem here. Verify if the edges are correct and there is no bouncing.
You could eliminate some electrical problems with the basic digital input filters in ST's timers. But it's better to have a filter in the first place.

\$\endgroup\$
3
  • \$\begingroup\$ There is another problem as well. I bet he uses HAL and the interrupt latency is not the issue (unless it the interrupt triggers come in the fraction of us). Using HAL library functions adds hundreds (sometimes thousands) of clock cycles on top of it. \$\endgroup\$ Oct 27, 2017 at 13:10
  • \$\begingroup\$ @PeterJ_01 Yeah, anything you build with lego will be clumsy. \$\endgroup\$
    – Jeroen3
    Oct 28, 2017 at 13:19
  • \$\begingroup\$ But I see many questions like: pin toggle very slow. \$\endgroup\$ Oct 28, 2017 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.