driver for incremental encoder
decoder.cpp@5:1a5772466668, 2019-03-14 (annotated)
- Committer:
- ianaherne
- Date:
- Thu Mar 14 16:05:28 2019 +0000
- Revision:
- 5:1a5772466668
- Parent:
- 4:9913990cfd78
- Child:
- 6:2fdce74d2a7b
addressed encoder issues and streamlined code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ianaherne | 0:c3a05fe15b84 | 1 | #include "decoder.h" |
ianaherne | 0:c3a05fe15b84 | 2 | |
ianaherne | 5:1a5772466668 | 3 | |
ianaherne | 5:1a5772466668 | 4 | |
ianaherne | 0:c3a05fe15b84 | 5 | /** |
ianaherne | 0:c3a05fe15b84 | 6 | * @brief constructor |
ianaherne | 0:c3a05fe15b84 | 7 | * @details initializes interrupt on channel A, and initialises internal variables |
ianaherne | 0:c3a05fe15b84 | 8 | * @param NA |
ianaherne | 0:c3a05fe15b84 | 9 | * @return NA |
ianaherne | 0:c3a05fe15b84 | 10 | * @warning interrupt functionallity on channel B is not used as it is not required. |
ianaherne | 0:c3a05fe15b84 | 11 | * |
ianaherne | 0:c3a05fe15b84 | 12 | */ |
ianaherne | 0:c3a05fe15b84 | 13 | |
ianaherne | 0:c3a05fe15b84 | 14 | decoder :: decoder(PinName channelA, PinName channelB):_channelA(channelA),_channelB(channelB) { |
ianaherne | 0:c3a05fe15b84 | 15 | |
ianaherne | 0:c3a05fe15b84 | 16 | _channelA.rise(this, &decoder::isr); |
ianaherne | 4:9913990cfd78 | 17 | |
ianaherne | 0:c3a05fe15b84 | 18 | _count = 0; |
ianaherne | 0:c3a05fe15b84 | 19 | _direction = 0; |
ianaherne | 0:c3a05fe15b84 | 20 | |
ianaherne | 0:c3a05fe15b84 | 21 | } |
ianaherne | 0:c3a05fe15b84 | 22 | |
ianaherne | 0:c3a05fe15b84 | 23 | /** |
ianaherne | 0:c3a05fe15b84 | 24 | * @brief isr |
ianaherne | 0:c3a05fe15b84 | 25 | * @details interrupt servive routine to decode the encoder output |
ianaherne | 0:c3a05fe15b84 | 26 | * @param NA |
ianaherne | 0:c3a05fe15b84 | 27 | * @return NA |
ianaherne | 0:c3a05fe15b84 | 28 | * @warning |
ianaherne | 0:c3a05fe15b84 | 29 | * |
ianaherne | 0:c3a05fe15b84 | 30 | */ |
ianaherne | 0:c3a05fe15b84 | 31 | |
ianaherne | 0:c3a05fe15b84 | 32 | |
ianaherne | 0:c3a05fe15b84 | 33 | |
ianaherne | 0:c3a05fe15b84 | 34 | void decoder :: isr(){ |
ianaherne | 5:1a5772466668 | 35 | //confirmed as x1 decoding y analysing waveform |
ianaherne | 4:9913990cfd78 | 36 | |
ianaherne | 0:c3a05fe15b84 | 37 | uint8_t chA = _channelA.read(); |
ianaherne | 4:9913990cfd78 | 38 | |
ianaherne | 0:c3a05fe15b84 | 39 | uint8_t chB = _channelB.read(); |
ianaherne | 0:c3a05fe15b84 | 40 | |
ianaherne | 0:c3a05fe15b84 | 41 | if(chA == 1){ |
ianaherne | 0:c3a05fe15b84 | 42 | |
ianaherne | 4:9913990cfd78 | 43 | if(chB == 1){ |
ianaherne | 4:9913990cfd78 | 44 | |
ianaherne | 4:9913990cfd78 | 45 | if( _count != UPPER_LIMIT){ |
ianaherne | 4:9913990cfd78 | 46 | _count++; |
ianaherne | 4:9913990cfd78 | 47 | } |
ianaherne | 4:9913990cfd78 | 48 | _direction = 1; |
ianaherne | 5:1a5772466668 | 49 | |
ianaherne | 5:1a5772466668 | 50 | //pc.printf(" 1 %d\n\n",_count);//debug |
ianaherne | 4:9913990cfd78 | 51 | |
ianaherne | 4:9913990cfd78 | 52 | } |
ianaherne | 4:9913990cfd78 | 53 | |
ianaherne | 4:9913990cfd78 | 54 | |
ianaherne | 0:c3a05fe15b84 | 55 | |
ianaherne | 4:9913990cfd78 | 56 | if(chB == 0){ |
ianaherne | 4:9913990cfd78 | 57 | |
ianaherne | 4:9913990cfd78 | 58 | if(_count != LOWER_LIMIT){ |
ianaherne | 4:9913990cfd78 | 59 | _count--; |
ianaherne | 4:9913990cfd78 | 60 | } |
ianaherne | 4:9913990cfd78 | 61 | _direction = 0; |
ianaherne | 4:9913990cfd78 | 62 | |
ianaherne | 5:1a5772466668 | 63 | //pc.printf(" 0 %d\n\n",_count);//debug |
ianaherne | 5:1a5772466668 | 64 | |
ianaherne | 4:9913990cfd78 | 65 | } |
ianaherne | 4:9913990cfd78 | 66 | |
ianaherne | 4:9913990cfd78 | 67 | } |
ianaherne | 0:c3a05fe15b84 | 68 | |
ianaherne | 0:c3a05fe15b84 | 69 | } |
ianaherne | 0:c3a05fe15b84 | 70 | |
ianaherne | 0:c3a05fe15b84 | 71 | /** |
ianaherne | 0:c3a05fe15b84 | 72 | * @brief getDirection() |
ianaherne | 0:c3a05fe15b84 | 73 | * @details function to return the class private variable _direction |
ianaherne | 0:c3a05fe15b84 | 74 | * @param NA |
ianaherne | 0:c3a05fe15b84 | 75 | * @return the direction of encoder |
ianaherne | 0:c3a05fe15b84 | 76 | * @warning |
ianaherne | 0:c3a05fe15b84 | 77 | * |
ianaherne | 0:c3a05fe15b84 | 78 | */ |
ianaherne | 0:c3a05fe15b84 | 79 | |
ianaherne | 0:c3a05fe15b84 | 80 | int8_t decoder :: getDirection(){ |
ianaherne | 0:c3a05fe15b84 | 81 | |
ianaherne | 0:c3a05fe15b84 | 82 | return _direction; |
ianaherne | 0:c3a05fe15b84 | 83 | |
ianaherne | 0:c3a05fe15b84 | 84 | |
ianaherne | 0:c3a05fe15b84 | 85 | |
ianaherne | 0:c3a05fe15b84 | 86 | } |
ianaherne | 0:c3a05fe15b84 | 87 | |
ianaherne | 0:c3a05fe15b84 | 88 | /** |
ianaherne | 0:c3a05fe15b84 | 89 | * @brief getCount |
ianaherne | 0:c3a05fe15b84 | 90 | * @details function to return the class private variable _count |
ianaherne | 0:c3a05fe15b84 | 91 | * @param NA |
ianaherne | 0:c3a05fe15b84 | 92 | * @return the number of turns of encoder +/- |
ianaherne | 0:c3a05fe15b84 | 93 | * @warning |
ianaherne | 0:c3a05fe15b84 | 94 | * |
ianaherne | 0:c3a05fe15b84 | 95 | */ |
ianaherne | 0:c3a05fe15b84 | 96 | |
ianaherne | 0:c3a05fe15b84 | 97 | int16_t decoder :: getCount(){ |
ianaherne | 0:c3a05fe15b84 | 98 | |
ianaherne | 0:c3a05fe15b84 | 99 | |
ianaherne | 0:c3a05fe15b84 | 100 | return _count; |
ianaherne | 0:c3a05fe15b84 | 101 | |
ianaherne | 0:c3a05fe15b84 | 102 | } |
ianaherne | 0:c3a05fe15b84 | 103 | |
ianaherne | 1:017efde7ab29 | 104 | /** |
ianaherne | 1:017efde7ab29 | 105 | * @brief reset() |
ianaherne | 1:017efde7ab29 | 106 | * @details function to reset all internal counting and directional variables |
ianaherne | 1:017efde7ab29 | 107 | * @param NA |
ianaherne | 1:017efde7ab29 | 108 | * @return |
ianaherne | 1:017efde7ab29 | 109 | * @warning |
ianaherne | 1:017efde7ab29 | 110 | * |
ianaherne | 1:017efde7ab29 | 111 | */ |
ianaherne | 1:017efde7ab29 | 112 | |
ianaherne | 1:017efde7ab29 | 113 | void decoder :: reset(){ |
ianaherne | 1:017efde7ab29 | 114 | |
ianaherne | 4:9913990cfd78 | 115 | |
ianaherne | 1:017efde7ab29 | 116 | _count = 0; |
ianaherne | 1:017efde7ab29 | 117 | _direction = 0; |
ianaherne | 1:017efde7ab29 | 118 | |
ianaherne | 1:017efde7ab29 | 119 | } |
ianaherne | 1:017efde7ab29 | 120 |