driver for incremental encoder

Committer:
ianaherne
Date:
Thu Mar 28 16:28:55 2019 +0000
Revision:
6:2fdce74d2a7b
Parent:
5:1a5772466668
added function to change limits

Who changed what in which revision?

UserRevisionLine numberNew 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 6:2fdce74d2a7b 19 _direction = 0;
ianaherne 6:2fdce74d2a7b 20
ianaherne 6:2fdce74d2a7b 21 _lowerLimit = 0;
ianaherne 6:2fdce74d2a7b 22 _upperLimit = 1; // set as default function must be called to change these values
ianaherne 0:c3a05fe15b84 23
ianaherne 0:c3a05fe15b84 24 }
ianaherne 0:c3a05fe15b84 25
ianaherne 0:c3a05fe15b84 26 /**
ianaherne 0:c3a05fe15b84 27 * @brief isr
ianaherne 0:c3a05fe15b84 28 * @details interrupt servive routine to decode the encoder output
ianaherne 0:c3a05fe15b84 29 * @param NA
ianaherne 0:c3a05fe15b84 30 * @return NA
ianaherne 0:c3a05fe15b84 31 * @warning
ianaherne 0:c3a05fe15b84 32 *
ianaherne 0:c3a05fe15b84 33 */
ianaherne 0:c3a05fe15b84 34
ianaherne 0:c3a05fe15b84 35
ianaherne 0:c3a05fe15b84 36
ianaherne 0:c3a05fe15b84 37 void decoder :: isr(){
ianaherne 5:1a5772466668 38 //confirmed as x1 decoding y analysing waveform
ianaherne 4:9913990cfd78 39
ianaherne 0:c3a05fe15b84 40 uint8_t chA = _channelA.read();
ianaherne 4:9913990cfd78 41
ianaherne 0:c3a05fe15b84 42 uint8_t chB = _channelB.read();
ianaherne 0:c3a05fe15b84 43
ianaherne 0:c3a05fe15b84 44 if(chA == 1){
ianaherne 0:c3a05fe15b84 45
ianaherne 4:9913990cfd78 46 if(chB == 1){
ianaherne 4:9913990cfd78 47
ianaherne 6:2fdce74d2a7b 48 if( _count != _upperLimit){
ianaherne 4:9913990cfd78 49 _count++;
ianaherne 4:9913990cfd78 50 }
ianaherne 4:9913990cfd78 51 _direction = 1;
ianaherne 5:1a5772466668 52
ianaherne 5:1a5772466668 53 //pc.printf(" 1 %d\n\n",_count);//debug
ianaherne 4:9913990cfd78 54
ianaherne 4:9913990cfd78 55 }
ianaherne 4:9913990cfd78 56
ianaherne 4:9913990cfd78 57
ianaherne 0:c3a05fe15b84 58
ianaherne 4:9913990cfd78 59 if(chB == 0){
ianaherne 4:9913990cfd78 60
ianaherne 6:2fdce74d2a7b 61 if(_count != _lowerLimit){
ianaherne 4:9913990cfd78 62 _count--;
ianaherne 4:9913990cfd78 63 }
ianaherne 4:9913990cfd78 64 _direction = 0;
ianaherne 4:9913990cfd78 65
ianaherne 5:1a5772466668 66 //pc.printf(" 0 %d\n\n",_count);//debug
ianaherne 5:1a5772466668 67
ianaherne 4:9913990cfd78 68 }
ianaherne 4:9913990cfd78 69
ianaherne 4:9913990cfd78 70 }
ianaherne 0:c3a05fe15b84 71
ianaherne 0:c3a05fe15b84 72 }
ianaherne 0:c3a05fe15b84 73
ianaherne 0:c3a05fe15b84 74 /**
ianaherne 0:c3a05fe15b84 75 * @brief getDirection()
ianaherne 0:c3a05fe15b84 76 * @details function to return the class private variable _direction
ianaherne 0:c3a05fe15b84 77 * @param NA
ianaherne 0:c3a05fe15b84 78 * @return the direction of encoder
ianaherne 0:c3a05fe15b84 79 * @warning
ianaherne 0:c3a05fe15b84 80 *
ianaherne 0:c3a05fe15b84 81 */
ianaherne 0:c3a05fe15b84 82
ianaherne 0:c3a05fe15b84 83 int8_t decoder :: getDirection(){
ianaherne 0:c3a05fe15b84 84
ianaherne 0:c3a05fe15b84 85 return _direction;
ianaherne 0:c3a05fe15b84 86
ianaherne 0:c3a05fe15b84 87
ianaherne 0:c3a05fe15b84 88
ianaherne 0:c3a05fe15b84 89 }
ianaherne 0:c3a05fe15b84 90
ianaherne 0:c3a05fe15b84 91 /**
ianaherne 0:c3a05fe15b84 92 * @brief getCount
ianaherne 0:c3a05fe15b84 93 * @details function to return the class private variable _count
ianaherne 0:c3a05fe15b84 94 * @param NA
ianaherne 0:c3a05fe15b84 95 * @return the number of turns of encoder +/-
ianaherne 0:c3a05fe15b84 96 * @warning
ianaherne 0:c3a05fe15b84 97 *
ianaherne 0:c3a05fe15b84 98 */
ianaherne 0:c3a05fe15b84 99
ianaherne 0:c3a05fe15b84 100 int16_t decoder :: getCount(){
ianaherne 0:c3a05fe15b84 101
ianaherne 0:c3a05fe15b84 102
ianaherne 0:c3a05fe15b84 103 return _count;
ianaherne 0:c3a05fe15b84 104
ianaherne 0:c3a05fe15b84 105 }
ianaherne 0:c3a05fe15b84 106
ianaherne 1:017efde7ab29 107 /**
ianaherne 6:2fdce74d2a7b 108 * @brief setLimits()
ianaherne 6:2fdce74d2a7b 109 * @details sets the limits of the encoder counting
ianaherne 6:2fdce74d2a7b 110 * @param lower limit and upper limit
ianaherne 6:2fdce74d2a7b 111 * @return
ianaherne 6:2fdce74d2a7b 112 * @warning set to 0 and 1 by default by constructor user must change as required
ianaherne 6:2fdce74d2a7b 113 *
ianaherne 6:2fdce74d2a7b 114 */
ianaherne 6:2fdce74d2a7b 115
ianaherne 6:2fdce74d2a7b 116
ianaherne 6:2fdce74d2a7b 117
ianaherne 6:2fdce74d2a7b 118 void decoder :: setLimits(int8_t lower, int8_t upper){
ianaherne 6:2fdce74d2a7b 119
ianaherne 6:2fdce74d2a7b 120 _lowerLimit = lower;
ianaherne 6:2fdce74d2a7b 121 _upperLimit = upper;
ianaherne 6:2fdce74d2a7b 122
ianaherne 6:2fdce74d2a7b 123
ianaherne 6:2fdce74d2a7b 124 }
ianaherne 6:2fdce74d2a7b 125
ianaherne 6:2fdce74d2a7b 126 /**
ianaherne 1:017efde7ab29 127 * @brief reset()
ianaherne 1:017efde7ab29 128 * @details function to reset all internal counting and directional variables
ianaherne 1:017efde7ab29 129 * @param NA
ianaherne 1:017efde7ab29 130 * @return
ianaherne 1:017efde7ab29 131 * @warning
ianaherne 1:017efde7ab29 132 *
ianaherne 1:017efde7ab29 133 */
ianaherne 1:017efde7ab29 134
ianaherne 1:017efde7ab29 135 void decoder :: reset(){
ianaherne 1:017efde7ab29 136
ianaherne 4:9913990cfd78 137
ianaherne 1:017efde7ab29 138 _count = 0;
ianaherne 1:017efde7ab29 139 _direction = 0;
ianaherne 1:017efde7ab29 140
ianaherne 1:017efde7ab29 141 }
ianaherne 1:017efde7ab29 142