Kevin Watson posted an encoder example on his web site
kevin.org/frc. The output of the encoder is called Gray Code and is set up so that only one channel changes at a time. Channel A leads channel B by 90 degrees. Thats just a fancy way of saying that channel B won't see a state change until halfway through a state change on channel A. Here is the order the signals will change as the shaft rotates:
Code:
A B
-- --
0 0
1 0
1 1
0 1
0 0
Looking at Channel A, there are two rising edges (transition from 0 to 1). In one direction, from top to bottom, Channel B is 0. In the other direction Channel B is 1. That means that an interrupt triggered on the rising edge of Channel A will give you both distance and direction. You will connect Channel A to an interrupt set up to look for a rising edge and Channel B to a digital input. Inside the ISR, if Channel B is high you're moving in one direction and if its low you're moving in the other.