Quadrature Encoder Switch (Alps EC11)
EncoderSwitch.cpp@1:7d24b4076453, 2015-04-01 (annotated)
- Committer:
- michaelruck
- Date:
- Wed Apr 01 05:43:10 2015 +0000
- Revision:
- 1:7d24b4076453
- Parent:
- 0:1330a261101d
Added Timeout to prevent isr chattering
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
michaelruck | 0:1330a261101d | 1 | /* michael@ruck.com märz 2015 v0.1*/ |
michaelruck | 0:1330a261101d | 2 | // Important! Two different EXTI Lines from 0 to 4! |
michaelruck | 0:1330a261101d | 3 | // Encoder Alps EC11 |
michaelruck | 0:1330a261101d | 4 | |
michaelruck | 0:1330a261101d | 5 | #include "mbed.h" |
michaelruck | 0:1330a261101d | 6 | #include "EncoderSwitch.h" |
michaelruck | 0:1330a261101d | 7 | |
michaelruck | 0:1330a261101d | 8 | EncoderSwitch::EncoderSwitch(PinName pinA, PinName pinB, PinMode pull) : _pinA(pinA), _pinB(pinB) |
michaelruck | 0:1330a261101d | 9 | { |
michaelruck | 0:1330a261101d | 10 | _pinA.mode(pull); |
michaelruck | 0:1330a261101d | 11 | _pinB.mode(pull); |
michaelruck | 0:1330a261101d | 12 | |
michaelruck | 0:1330a261101d | 13 | _pinA.fall(this, &EncoderSwitch::isra); |
michaelruck | 0:1330a261101d | 14 | _pinA.rise(this, &EncoderSwitch::isra); |
michaelruck | 0:1330a261101d | 15 | |
michaelruck | 0:1330a261101d | 16 | _pinB.fall(this, &EncoderSwitch::isrb); |
michaelruck | 0:1330a261101d | 17 | _pinB.rise(this, &EncoderSwitch::isrb); |
michaelruck | 1:7d24b4076453 | 18 | |
michaelruck | 0:1330a261101d | 19 | _limit_down=0; |
michaelruck | 0:1330a261101d | 20 | _limit_up=9; |
michaelruck | 0:1330a261101d | 21 | _increment=0; |
michaelruck | 0:1330a261101d | 22 | } |
michaelruck | 0:1330a261101d | 23 | |
michaelruck | 0:1330a261101d | 24 | EncoderSwitch::~EncoderSwitch() {/*none*/} |
michaelruck | 0:1330a261101d | 25 | |
michaelruck | 1:7d24b4076453 | 26 | void EncoderSwitch::_enable_isr() |
michaelruck | 1:7d24b4076453 | 27 | { |
michaelruck | 1:7d24b4076453 | 28 | _pinA.enable_irq(); |
michaelruck | 1:7d24b4076453 | 29 | _pinB.enable_irq(); |
michaelruck | 1:7d24b4076453 | 30 | } |
michaelruck | 1:7d24b4076453 | 31 | |
michaelruck | 0:1330a261101d | 32 | void EncoderSwitch::isra() |
michaelruck | 0:1330a261101d | 33 | { |
michaelruck | 1:7d24b4076453 | 34 | _pinA.disable_irq(); |
michaelruck | 1:7d24b4076453 | 35 | _pinB.disable_irq(); |
michaelruck | 1:7d24b4076453 | 36 | _timeout.attach_us(this, &EncoderSwitch::_enable_isr, 500); |
michaelruck | 0:1330a261101d | 37 | int8_t state_a = _pinA.read(); |
michaelruck | 0:1330a261101d | 38 | int8_t state_b = _pinB.read(); |
michaelruck | 0:1330a261101d | 39 | if(state_a != state_b) _increment = 1; |
michaelruck | 0:1330a261101d | 40 | if((state_a == state_b) && (_increment == -1)) { |
michaelruck | 0:1330a261101d | 41 | counter(_increment); |
michaelruck | 0:1330a261101d | 42 | _increment = 0; |
michaelruck | 0:1330a261101d | 43 | } |
michaelruck | 0:1330a261101d | 44 | } |
michaelruck | 0:1330a261101d | 45 | |
michaelruck | 0:1330a261101d | 46 | void EncoderSwitch::isrb() |
michaelruck | 0:1330a261101d | 47 | { |
michaelruck | 1:7d24b4076453 | 48 | _pinA.disable_irq(); |
michaelruck | 1:7d24b4076453 | 49 | _pinB.disable_irq(); |
michaelruck | 1:7d24b4076453 | 50 | _timeout.attach_us(this, &EncoderSwitch::_enable_isr, 500); |
michaelruck | 0:1330a261101d | 51 | int8_t state_a = _pinA.read(); |
michaelruck | 0:1330a261101d | 52 | int8_t state_b = _pinB.read(); |
michaelruck | 0:1330a261101d | 53 | if(state_a != state_b) _increment = -1; |
michaelruck | 0:1330a261101d | 54 | if((state_a == state_b) && (_increment == 1)) { |
michaelruck | 0:1330a261101d | 55 | counter(_increment); |
michaelruck | 0:1330a261101d | 56 | _increment = 0; |
michaelruck | 0:1330a261101d | 57 | } |
michaelruck | 0:1330a261101d | 58 | } |
michaelruck | 0:1330a261101d | 59 | |
michaelruck | 0:1330a261101d | 60 | |
michaelruck | 0:1330a261101d | 61 | |
michaelruck | 0:1330a261101d | 62 | void EncoderSwitch::setLimits(int16_t limit_down, int16_t limit_up) |
michaelruck | 0:1330a261101d | 63 | { |
michaelruck | 0:1330a261101d | 64 | _limit_down=limit_down; |
michaelruck | 0:1330a261101d | 65 | _limit_up=limit_up; |
michaelruck | 0:1330a261101d | 66 | } |
michaelruck | 0:1330a261101d | 67 | |
michaelruck | 0:1330a261101d | 68 | void EncoderSwitch::counter(int16_t increment) |
michaelruck | 0:1330a261101d | 69 | { |
michaelruck | 0:1330a261101d | 70 | if(increment != 0) { |
michaelruck | 0:1330a261101d | 71 | if((count+increment <= _limit_up) & (count+increment >= _limit_down)) count = count+increment; |
michaelruck | 0:1330a261101d | 72 | else if(count+increment > _limit_up) count = _limit_down; |
michaelruck | 0:1330a261101d | 73 | else if(count+increment < _limit_down) count = _limit_up; |
michaelruck | 0:1330a261101d | 74 | } |
michaelruck | 0:1330a261101d | 75 | } |