Interrupt for reading encoder?
I'm new to the SX (coming from Basic Stamp) and don't understand what the interrupt is all about. Can someone explain it or direct me to some place that explains it?
I am using SX/B for something similar to the encoder example shown in the SX/B help. The example·uses an interrupt for reading·an encoder and updating a display. Why use an interrupt rather than include these functions in the main program?
Thanks for any help.
I am using SX/B for something similar to the encoder example shown in the SX/B help. The example·uses an interrupt for reading·an encoder and updating a display. Why use an interrupt rather than include these functions in the main program?
Thanks for any help.
Comments
If your encoder is very slow updating and the programm runs faster than the encoder you might not need to do this with an interrupt.· Example a 15 cpr encoder that turn over every 15seconds might not need an interrupt. Since the program should be able to handle this.· But wouldn't you like to go into an interrupt routine every time a new pulse occurs.· This way you make sure that you handle all of your counts from the encoder.
I am reading the HELP and I am just plain confused on how to use WKEN_B, WKED_B, and WKPND_B to trigger an interrupt. Does anyone have an example I could look at?
You really only need an interrupt on one of the encoder signals, and only on rising OR falling edge.
Let's say you have it on phase-A rising. In the interrupt you can simple sample phase-B to see if it is high or low. If it's high your moving in one direction, if low your moving in the other direction.
Now that doesn't allow you to monitor all four states, but normally you don't need to.
If you NEED to monitor both falling and rising edge, you can use two pins. Setup one for rising edge, and the other for falling edge. Or you could switch edge detects inside the interrupt routine.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheap used 4-digit LED display with driver IC·www.hc4led.com
Low power SD Data Logger www.sddatalogger.com
SX-Video Display Modules www.sxvm.com
Stuff I'm selling on ebay http://search.ebay.com/_W0QQsassZhittconsultingQQhtZ-1
"USA Today has come out with a new survey - apparently, three out of every four people make up 75% of the population." - David Letterman
Any time you use different parts of the waveform for forward and backward edge counting you risk a miscount when the direction changes. Imagine, if you will, an encoder stopped on an edge that's only "sensitive" to a low-high transition. Then imagine the consequences of vibration about that edge. You'd get continuous counts in one direction and none in the other, even though the net rotation is zero.
For the most accurate count, you really do need to interrupt on both edges of one channel. For example:
A channel rising, B channel high = count up
A channel falling, B channel high = count down
A channel rising or falling, B channel low = no change
In the SX, this can possibly be accomplished by inverting the WKED_B register during the ISR each time an interrupt occurs. I'm not sure, though, if this would lead to spurious or missed interrupts. That would depend on how the processor responds if the complementary edge arrives during the ISR but before WKED_B gets flipped. My guess is that the interrupt is really only sensitive to one edge and that the input pin is simply XORed with the corresponding bit in the WKED_B register. In this case flipping that bit would have the desired effect, without any glitches.
But if it is a problem, a solution would be to use the timer interrupt at a rate at least 8x the fastest encoder frequency (assuming symmetrical waveforms). This would allow the waveforms to be sampled often enough to account for all relevant edges.
-Phil