This was just a question I thought of as I started working through a design project. Let's say I have a hall effect sensor on an encoder motor. I need to catch every pulse from this encoder so I use the readings on an interrupt pin to trigger whenever the value from the hall sensor changes from high to low or low to high. The example code from the motor manufacturer has the ISR read the hall sensors pins to read whether it is now low or now high. My question is: is it possible in the time between the pin triggering the ISR for the pin to change values? For example, say it changed to low from high, triggering an interrupt, but in the time it takes for the ISR to store registers and jump and start the ISR, could a fluctuation or noise push the pin back up to the high voltage reading? I know that with most MCUs we're dealing with something in the range of 10s of nanoseconds but I didn't know how sensitive this system would be to a problem like this. Thanks for the responses
-
1\$\begingroup\$ You've asked a general question for which a general answer would fill several chapters, if not a book. You can see this general direction somewhat illustrated, but only partly, by a pretty good survey answer from @glen. I think you should select a concrete example (yes, that will take research work on your part to frame it) that includes a for example, this specific hall sensor, at the very least. Even that is going to require specifying the ring magnet, itself. \$\endgroup\$– jonkCommented Sep 19, 2021 at 19:21
2 Answers
ISR latency is often plenty short: under a microsecond. Unless your microcontroller is very slow, each edge interrupt should yield a proper logic level inside the interrupt routine. But that's not the whole story...
If you have other peripherals interrupting, you may have to wait for them to complete before your Hall sensor interrupt routine runs. Usually, interrupt routines take little time to complete, so a later Hall-level read (when the Hall interrupt is finally serviced) still won't be a problem.
If other interrupts cause service to be so slow that Hall level changes after its interrupt is scheduled, you may be able to prioritize interrupts, so that Hall interrupts are serviced promptly, while others are serviced only when Hall interrupts finish.
A different problem may occur at the slow-rotating end, when the Hall encoder sends out a just a few pulses each second. Logic edges may be unstable as they transit from low-to-high or from high-to-low, oscillating at a high frequency instead of providing a single, clean, sharp edge. A fast-responding interrupt routine may try to count each oscillation, resulting in many interrupts rather than the single-edge interrupt required.
This problem is similar to mechanical switches, which require edge "de-bouncing". A digital Hall sensor may include in its internal preamplifier some hysteresis to alleviate these unclean output edges.
In any case, check that very slow shaft rotation yields the proper edge-count. Also check that interrupt service captures every fast-rotating shaft logic edges.
"My question is: is it possible in the time between the pin triggering the ISR for the pin to change values?"
Yes in general it is possible. But then it becomes a misapplication of interrupts if the reason is because your encoder spins fast enough and has enough resolution to beat the ISR execution time. And if noise (or other signal instability) can cause the value to change like that then you have insufficient signal conditioning.
In general I really try to avoid using interrupts for encoder A and B channels.
-
\$\begingroup\$ What do you do then to keep track of the encoder A and B channels? Do you just use a small MCU dedicated entirely to tracking the pulses from the encoder? \$\endgroup\$– cEEaCommented Sep 19, 2021 at 19:44
-
1\$\begingroup\$ @cEEa Many microcntrollers have timers that support encoder modes so the processor core doesn't have to intervene to count pulses. It only intervenes if you need to actually do something with the count value. \$\endgroup\$– DKNguyenCommented Sep 19, 2021 at 19:46