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
RotationalEncoder.cpp
- Committer:
- suupen
- Date:
- 2016-10-23
- Revision:
- 2:0102de0ec7a7
- Parent:
- 1:57c43aac7007
File content as of revision 2:0102de0ec7a7:
/** * 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(); }