2 Phase different signals rotational encoder library 2相位相差型ロータリーエンコーダ用ライブラリ。 絶対値パルス関数。相対値(前回読み出しからの差)パルス出力関数。秋月電子のEC12PLRGBSDVBF-D-25K-24-24C-61で動作確認した。http://akizukidenshi.com/catalog/g/gP-05773/

Dependents:   RotationalEncoder_Hello

example program

Import programRotationalEncoder_Hello

RotationalEncoder library's example program

Revision:
1:57c43aac7007
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RotationalEncoder.cpp	Sun Oct 23 02:18:28 2016 +0000
@@ -0,0 +1,78 @@
+/**
+ * rotational encoder Library
+ */
+
+#include "RotationalEncoder.h"
+
+/**
+ * 
+ */
+RotationalEncoder::RotationalEncoder(PinName channelA,
+                                     PinName channelB
+                                    ) : _channelA(channelA), _channelB(channelB)
+{
+
+    _absolutePulse = 0;
+    _relativePulse = 0;
+
+    _channelA.mode(PullUp);
+    _channelB.mode(PullUp);
+
+    _channelBLevelOfchannelALow = INITAL;
+
+    _channelA.rise(this, &RotationalEncoder::encoderAUp);
+    _channelA.fall(this, &RotationalEncoder::encoderADwon);
+
+
+}
+
+void RotationalEncoder::reset(void)
+{
+    _absolutePulse = 0;
+    _relativePulse = 0;
+    getRelativePulses();
+}
+
+
+int16_t RotationalEncoder::getAbsolutePulses(void)
+{
+
+    return _absolutePulse;
+
+}
+
+int16_t RotationalEncoder::getRelativePulses(void)
+{
+    static int16_t beforeAbsolutePulse = 0;
+    int16_t temp = _absolutePulse;
+    
+    _relativePulse = temp - beforeAbsolutePulse;
+    beforeAbsolutePulse = temp;    
+
+    return _relativePulse;
+
+}
+
+
+void RotationalEncoder::encoderAUp(void)
+{
+    if((_channelBLevelOfchannelALow == INITAL) ||
+       (_channelBLevelOfchannelALow == _channelB.read())
+       ) {
+        return;
+    }
+
+    if(_channelB.read() == 0) {
+        _absolutePulse++;
+
+    } else {
+        _absolutePulse--;
+
+    }
+
+}
+
+void RotationalEncoder::encoderADwon(void)
+{
+    _channelBLevelOfchannelALow = _channelB.read();
+}