I need help debugging a class I’m working on for this rotary encoder.
[url]http://www.sparkfun.com/commerce/product_info.php?products_id=9117[/url]
Here’s the datasheet:
https://docs.google.com/viewer?url=http://www.sparkfun.com/datasheets/Components/TW-700198.pdf
Using the diagram on bottom right of datasheet (note that off/on is backward), I watch for the rising edge of pin A. At which time, I read pin B, if it’s off then it’s clockwise, otherwise it’s counter clockwise. Simple enough.
It is working, but every 5 “clicks” or so, it would report the wrong direction! >:(
I’ve cranked up the glitch filter all the way to 500ms, I’ve turned off glitchfilter, same intermittent unreliability.
As far as wiring, I connect pin A to A0, pin B to A1, and 3.3V to middle pin C.
I am using analog pins because I’m reserving the digital pins for other use.
public class Program
{
public static void Main()
{
RotaryEncoder encoder = new RotaryEncoder(
FEZ_Pin.Interrupt.An0, FEZ_Pin.Digital.An1,
new RotaryEncoder.EncoderCallback(onEncoderEvent));
Thread.Sleep(Timeout.Infinite);
}
public static void onEncoderEvent(bool isClockwise)
{
Debug.Print( isClockwise ? "CLOCKWISE" : "counterclockwise");
}
}
public class RotaryEncoder
{
InterruptPort encoderA;
InputPort encoderB;
EncoderCallback callbackMethod;
public delegate void EncoderCallback(bool isClockwise);
public RotaryEncoder(FEZ_Pin.Interrupt pinA, FEZ_Pin.Digital pinB, EncoderCallback encoderCallback)
{
Microsoft.SPOT.Hardware.Cpu.GlitchFilterTime = new TimeSpan(0, 0, 0, 0, 200);
encoderA = new InterruptPort((Cpu.Pin)pinA, true, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeHigh);
encoderA.OnInterrupt += new NativeEventHandler(encoderA_OnInterrupt);
encoderB = new InputPort((Cpu.Pin)pinB, true, Port.ResistorMode.PullDown);
callbackMethod = encoderCallback;
}
void encoderA_OnInterrupt(uint data1, uint data2, DateTime time)
{
bool isClockwise = encoderB.Read();
callbackMethod(isClockwise);
}
}