driver for incremental encoder
Diff: decoder.cpp
- Revision:
- 4:9913990cfd78
- Parent:
- 2:b82d31306c4b
- Child:
- 5:1a5772466668
--- a/decoder.cpp Tue Mar 12 13:45:44 2019 +0000 +++ b/decoder.cpp Tue Mar 12 18:12:04 2019 +0000 @@ -12,7 +12,7 @@ decoder :: decoder(PinName channelA, PinName channelB):_channelA(channelA),_channelB(channelB) { _channelA.rise(this, &decoder::isr); - _oldCount = 0; + _count = 0; _direction = 0; @@ -31,21 +31,34 @@ void decoder :: isr(){ + uint8_t chA = _channelA.read(); + uint8_t chB = _channelB.read(); if(chA == 1){ - if(chB == 1 && _count != UPPER_LIMIT){_count++;} - if(chB == 0 && _count != LOWER_LIMIT){_count--;} + if(chB == 1){ + + if( _count != UPPER_LIMIT){ + _count++; + } + _direction = 1; + + } + + - } - - if(_oldCount < _count){_direction = 1;} - if(_oldCount > _count){_direction = -1;} - if(_oldCount == _count){_direction = 0;} - - _oldCount = _count; + if(chB == 0){ + + if(_count != LOWER_LIMIT){ + _count--; + } + _direction = 0; + + } + + } } @@ -93,7 +106,7 @@ void decoder :: reset(){ - _oldCount = 0; + _count = 0; _direction = 0;