Josh Koffman wrote: > Hi all. I've been working on a new project using mechanical rotary > encoders. I wrote a new encoder reading routine based on the info I > found in this document: > http://www.tufts.edu/programs/mma/emid/RotaryEncoder.pdf. The method > is to compare the LSB of the current reading to the MSB of the last > reading, XOR them together, and from there you will be able to > determine an up or down motion. I didn't follow the link, but your description makes no sense assuming a normal quadrature encoder. There is no LSB and MSB, only A and B quadrature lines. The trick for solid quadrature decoding is to decode all of the 4 states. Don't try to sleaze by with looking at the polarity of one line during a particular edge of the other, for example, even though you see that sort of thing a lot on the 'net. Keep the 4x count in firmware and update it every individual state change. You always know which state you're in, so you increment or decrement the count by one when you move to one of the two adjacent states. If you jump to the fourth state, then you know something went wrong, probably the encoder was moved too fast for the firmware. Assuming one state was missed, the counter should either be incremented or decrmented by 2, but just by looking at the old and new state you don't know which. I usually keep a direction flag that is set whenever a +1 or -1 transition is decoded, then assume the encoder kept going in the same direction for the jump of 2. > For each movement of the encoder I'm getting too many events in my > code. From my understanding of the datasheet it should be one detent = > one pulse. For each detent I currently get about 4 events. Exactly as expected. One detent should be one complete quadrature cycle, which is 4 total state transitions. Decode as described above. Then you can ignore the 2 least significant bits if you just want to know whole detents. You still need those bits to properly decode all the transitions. ******************************************************************** Embed Inc, Littleton Massachusetts, http://www.embedinc.com/products (978) 742-9014. Gold level PIC consultants since 2000. -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist