Device driver for AD converters MCP3426, MCP3427, and MCP3428.
mcp342x.h@1:bc877c37027c, 2016-04-29 (annotated)
- Committer:
- coisme
- Date:
- Fri Apr 29 23:53:18 2016 +0000
- Revision:
- 1:bc877c37027c
- Parent:
- 0:78c512aa4d18
- Child:
- 2:96d9bfe25b03
Implementation completed.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
coisme | 0:78c512aa4d18 | 1 | #ifndef __MCP342X_H__ |
coisme | 0:78c512aa4d18 | 2 | #define __MCP342X_H__ |
coisme | 0:78c512aa4d18 | 3 | |
coisme | 0:78c512aa4d18 | 4 | #include "mbed.h" |
coisme | 0:78c512aa4d18 | 5 | |
coisme | 0:78c512aa4d18 | 6 | class MCP342X |
coisme | 0:78c512aa4d18 | 7 | { |
coisme | 0:78c512aa4d18 | 8 | public: |
coisme | 0:78c512aa4d18 | 9 | /** |
coisme | 0:78c512aa4d18 | 10 | * Slave addresses. |
coisme | 0:78c512aa4d18 | 11 | */ |
coisme | 0:78c512aa4d18 | 12 | typedef enum { |
coisme | 0:78c512aa4d18 | 13 | SLAVE_ADDRESS_68H, /**< When Adr0 pin = L and Adr1 pin = L, or Adr0 pin = float and Adr1 pin = float. */ |
coisme | 0:78c512aa4d18 | 14 | SLAVE_ADDRESS_69H, /**< When Adr0 pin = L and Adr1 pin = float. */ |
coisme | 0:78c512aa4d18 | 15 | SLAVE_ADDRESS_6AH, /**< When Adr0 pin = L and Adr1 pin = H. */ |
coisme | 0:78c512aa4d18 | 16 | SLAVE_ADDRESS_6CH, /**< When Adr0 pin = H and Adr1 pin = L. */ |
coisme | 0:78c512aa4d18 | 17 | SLAVE_ADDRESS_6DH, /**< When Adr0 pin = H and Adr1 pin = float. */ |
coisme | 0:78c512aa4d18 | 18 | SLAVE_ADDRESS_6EH, /**< When Adr0 pin = H and Adr1 pin = H. */ |
coisme | 0:78c512aa4d18 | 19 | SLAVE_ADDRESS_6BH, /**< When Adr0 pin = float and Adr1 pin = L. */ |
coisme | 0:78c512aa4d18 | 20 | SLAVE_ADDRESS_6FH, /**< When Adr0 pin = float and Adr1 pin = H. */ |
coisme | 0:78c512aa4d18 | 21 | } SlaveAddress; |
coisme | 0:78c512aa4d18 | 22 | |
coisme | 0:78c512aa4d18 | 23 | /** |
coisme | 0:78c512aa4d18 | 24 | * Status of function. |
coisme | 0:78c512aa4d18 | 25 | */ |
coisme | 0:78c512aa4d18 | 26 | typedef enum { |
coisme | 0:78c512aa4d18 | 27 | SUCCESS, /**< The function processed successfully. */ |
coisme | 0:78c512aa4d18 | 28 | ERROR_I2C_READ, /**< Error related to I2C read. */ |
coisme | 0:78c512aa4d18 | 29 | ERROR_I2C_WRITE, /**< Error related to I2C write. */ |
coisme | 0:78c512aa4d18 | 30 | ERROR, /**< General Error */ |
coisme | 0:78c512aa4d18 | 31 | } Status; |
coisme | 0:78c512aa4d18 | 32 | |
coisme | 0:78c512aa4d18 | 33 | /** |
coisme | 0:78c512aa4d18 | 34 | * Conversion mode setting. |
coisme | 0:78c512aa4d18 | 35 | */ |
coisme | 0:78c512aa4d18 | 36 | typedef enum { |
coisme | 0:78c512aa4d18 | 37 | CONTINUOUS, /**< Continuous conversion mode. Default. */ |
coisme | 0:78c512aa4d18 | 38 | ONE_SHOT, /**< One-shot conversion mode. */ |
coisme | 0:78c512aa4d18 | 39 | } ConversionMode; |
coisme | 0:78c512aa4d18 | 40 | |
coisme | 0:78c512aa4d18 | 41 | /** |
coisme | 0:78c512aa4d18 | 42 | * Data ready status. |
coisme | 0:78c512aa4d18 | 43 | */ |
coisme | 0:78c512aa4d18 | 44 | typedef enum { |
coisme | 0:78c512aa4d18 | 45 | DATA_NOT_UPDATED, /**< Output register has not been updated. */ |
coisme | 0:78c512aa4d18 | 46 | DATA_UPDATED, /**< Output register has been updated with the latest conversion result. */ |
coisme | 0:78c512aa4d18 | 47 | } DataStatus; |
coisme | 0:78c512aa4d18 | 48 | |
coisme | 0:78c512aa4d18 | 49 | /** |
coisme | 0:78c512aa4d18 | 50 | * Measurement trigger command. |
coisme | 0:78c512aa4d18 | 51 | */ |
coisme | 0:78c512aa4d18 | 52 | typedef enum { |
coisme | 0:78c512aa4d18 | 53 | TRIGGER, /**< Initiate a new conversion. */ |
coisme | 0:78c512aa4d18 | 54 | NONE, /**< No effect. */ |
coisme | 0:78c512aa4d18 | 55 | } MeasurementTrigger; |
coisme | 0:78c512aa4d18 | 56 | |
coisme | 0:78c512aa4d18 | 57 | /** |
coisme | 0:78c512aa4d18 | 58 | * Sample rate and resolution setting. |
coisme | 0:78c512aa4d18 | 59 | */ |
coisme | 0:78c512aa4d18 | 60 | typedef enum { |
coisme | 0:78c512aa4d18 | 61 | SAMPLE_240HZ_12BIT, /**< 240 sample per second with 12 bit data. Default. */ |
coisme | 0:78c512aa4d18 | 62 | SAMPLE_60HZ_14BIT, /**< 60 sample per second with 14 bit data. */ |
coisme | 0:78c512aa4d18 | 63 | SAMPLE_15HZ_16BIT, /**< 15 sample per second with 16 bit data. */ |
coisme | 0:78c512aa4d18 | 64 | } SampleSetting; |
coisme | 0:78c512aa4d18 | 65 | |
coisme | 0:78c512aa4d18 | 66 | |
coisme | 0:78c512aa4d18 | 67 | /** |
coisme | 0:78c512aa4d18 | 68 | * ADC channel selection. |
coisme | 0:78c512aa4d18 | 69 | */ |
coisme | 0:78c512aa4d18 | 70 | typedef enum { |
coisme | 0:78c512aa4d18 | 71 | ADC_CH1, /**< Channel 1, default. */ |
coisme | 0:78c512aa4d18 | 72 | ADC_CH2, /**< Channel 2 */ |
coisme | 0:78c512aa4d18 | 73 | ADC_CH3, /**< Channel 3, MCP3428 only, treated as channel 1 by the MCP3426/MCP3427. */ |
coisme | 0:78c512aa4d18 | 74 | ADC_CH4, /**< Channel 4, MCP3428 only, treated as channel 2 by the MCP3426/MCP3427. */ |
coisme | 0:78c512aa4d18 | 75 | } AdcChannel; |
coisme | 0:78c512aa4d18 | 76 | |
coisme | 0:78c512aa4d18 | 77 | /** |
coisme | 0:78c512aa4d18 | 78 | * Programmable Gain Amplifier setting. |
coisme | 0:78c512aa4d18 | 79 | */ |
coisme | 0:78c512aa4d18 | 80 | typedef enum { |
coisme | 0:78c512aa4d18 | 81 | PGA_1X, /**< Gain 1x, Default. */ |
coisme | 0:78c512aa4d18 | 82 | PGA_2X, /**< Gain 2x. */ |
coisme | 0:78c512aa4d18 | 83 | PGA_4X, /**< Gain 4x. */ |
coisme | 0:78c512aa4d18 | 84 | PGA_8X, /**< Gain 8x. */ |
coisme | 0:78c512aa4d18 | 85 | } PgaSetting; |
coisme | 1:bc877c37027c | 86 | |
coisme | 1:bc877c37027c | 87 | /** |
coisme | 1:bc877c37027c | 88 | * AD result data. |
coisme | 1:bc877c37027c | 89 | */ |
coisme | 1:bc877c37027c | 90 | typedef struct { |
coisme | 1:bc877c37027c | 91 | DataStatus st; |
coisme | 1:bc877c37027c | 92 | int16_t value; |
coisme | 1:bc877c37027c | 93 | } Data; |
coisme | 1:bc877c37027c | 94 | |
coisme | 0:78c512aa4d18 | 95 | /** |
coisme | 0:78c512aa4d18 | 96 | * Constructor. |
coisme | 0:78c512aa4d18 | 97 | * |
coisme | 0:78c512aa4d18 | 98 | * @param conn instance of I2C |
coisme | 0:78c512aa4d18 | 99 | * @param addr slave address of the device |
coisme | 0:78c512aa4d18 | 100 | */ |
coisme | 0:78c512aa4d18 | 101 | MCP342X(I2C *conn, SlaveAddress addr); |
coisme | 0:78c512aa4d18 | 102 | |
coisme | 0:78c512aa4d18 | 103 | /** |
coisme | 0:78c512aa4d18 | 104 | * Sets a ADC channel. |
coisme | 0:78c512aa4d18 | 105 | * @param ch ADC channel which to be the input. |
coisme | 0:78c512aa4d18 | 106 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:78c512aa4d18 | 107 | */ |
coisme | 0:78c512aa4d18 | 108 | Status setChannel(AdcChannel ch); |
coisme | 0:78c512aa4d18 | 109 | |
coisme | 0:78c512aa4d18 | 110 | /** |
coisme | 0:78c512aa4d18 | 111 | * Gets the current selected ADC channel. |
coisme | 0:78c512aa4d18 | 112 | * @return ADC channel currently set. |
coisme | 0:78c512aa4d18 | 113 | */ |
coisme | 0:78c512aa4d18 | 114 | AdcChannel getChannel(); |
coisme | 0:78c512aa4d18 | 115 | |
coisme | 0:78c512aa4d18 | 116 | /** |
coisme | 0:78c512aa4d18 | 117 | * Sets a conversion mode. |
coisme | 0:78c512aa4d18 | 118 | * @param mode Conversion mode which to be set. |
coisme | 0:78c512aa4d18 | 119 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:78c512aa4d18 | 120 | */ |
coisme | 0:78c512aa4d18 | 121 | Status setConversionMode(ConversionMode mode); |
coisme | 0:78c512aa4d18 | 122 | |
coisme | 0:78c512aa4d18 | 123 | /** |
coisme | 0:78c512aa4d18 | 124 | * Gets the current conversion mode. |
coisme | 0:78c512aa4d18 | 125 | * @return Current conversion mode. |
coisme | 0:78c512aa4d18 | 126 | */ |
coisme | 0:78c512aa4d18 | 127 | ConversionMode getConversionMode(); |
coisme | 0:78c512aa4d18 | 128 | |
coisme | 0:78c512aa4d18 | 129 | /** |
coisme | 0:78c512aa4d18 | 130 | * Sets sample setting. |
coisme | 0:78c512aa4d18 | 131 | * @param s Sample setting to be set. |
coisme | 0:78c512aa4d18 | 132 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:78c512aa4d18 | 133 | */ |
coisme | 0:78c512aa4d18 | 134 | Status setSampleSetting(SampleSetting s); |
coisme | 0:78c512aa4d18 | 135 | |
coisme | 0:78c512aa4d18 | 136 | /** |
coisme | 0:78c512aa4d18 | 137 | * Gets the current sample setting. |
coisme | 0:78c512aa4d18 | 138 | * @return Current sample setting. |
coisme | 0:78c512aa4d18 | 139 | */ |
coisme | 0:78c512aa4d18 | 140 | SampleSetting getSampleSetting(); |
coisme | 0:78c512aa4d18 | 141 | |
coisme | 0:78c512aa4d18 | 142 | /** |
coisme | 0:78c512aa4d18 | 143 | * Sets gain of Programmable Gain Amplifier (PGA). |
coisme | 0:78c512aa4d18 | 144 | * @param s PGA seeting to be set. |
coisme | 0:78c512aa4d18 | 145 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:78c512aa4d18 | 146 | */ |
coisme | 0:78c512aa4d18 | 147 | Status setPgaSetting(PgaSetting s); |
coisme | 0:78c512aa4d18 | 148 | |
coisme | 0:78c512aa4d18 | 149 | /** |
coisme | 0:78c512aa4d18 | 150 | * Gets the current PGA setting. |
coisme | 0:78c512aa4d18 | 151 | * @return Current PGA setting. |
coisme | 0:78c512aa4d18 | 152 | */ |
coisme | 0:78c512aa4d18 | 153 | PgaSetting getPgaSetting(); |
coisme | 0:78c512aa4d18 | 154 | |
coisme | 0:78c512aa4d18 | 155 | /** |
coisme | 0:78c512aa4d18 | 156 | * Gets the AD value. |
coisme | 0:78c512aa4d18 | 157 | * @return AD value. |
coisme | 0:78c512aa4d18 | 158 | */ |
coisme | 1:bc877c37027c | 159 | Status getData(Data *pt); |
coisme | 0:78c512aa4d18 | 160 | |
coisme | 0:78c512aa4d18 | 161 | /** |
coisme | 0:78c512aa4d18 | 162 | * Trigger AD conversion. |
coisme | 0:78c512aa4d18 | 163 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:78c512aa4d18 | 164 | */ |
coisme | 0:78c512aa4d18 | 165 | Status trigger(); |
coisme | 0:78c512aa4d18 | 166 | |
coisme | 0:78c512aa4d18 | 167 | /** |
coisme | 0:78c512aa4d18 | 168 | * Gets the data conversion time. |
coisme | 0:78c512aa4d18 | 169 | * @return Conversion time based on the current sample setting in milli-second unit. |
coisme | 0:78c512aa4d18 | 170 | */ |
coisme | 0:78c512aa4d18 | 171 | float getDataConversionTime(); |
coisme | 0:78c512aa4d18 | 172 | |
coisme | 0:78c512aa4d18 | 173 | private: |
coisme | 0:78c512aa4d18 | 174 | typedef struct { |
coisme | 1:bc877c37027c | 175 | MeasurementTrigger measurementTrigger; |
coisme | 1:bc877c37027c | 176 | DataStatus dataStatus; |
coisme | 1:bc877c37027c | 177 | ConversionMode conversionMode; |
coisme | 1:bc877c37027c | 178 | SampleSetting sampleSetting; |
coisme | 1:bc877c37027c | 179 | AdcChannel adcChannel; |
coisme | 1:bc877c37027c | 180 | PgaSetting pgaSetting; |
coisme | 0:78c512aa4d18 | 181 | } Config; |
coisme | 0:78c512aa4d18 | 182 | |
coisme | 0:78c512aa4d18 | 183 | |
coisme | 0:78c512aa4d18 | 184 | I2C *connection; /**< Pointer to an I2C object. */ |
coisme | 0:78c512aa4d18 | 185 | uint8_t slaveAddress; /**< Slave address. */ |
coisme | 1:bc877c37027c | 186 | Config currentConfig; /**< Stores the latest configuration. */ |
coisme | 1:bc877c37027c | 187 | |
coisme | 1:bc877c37027c | 188 | /** |
coisme | 1:bc877c37027c | 189 | * Initialize this device. |
coisme | 1:bc877c37027c | 190 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 1:bc877c37027c | 191 | */ |
coisme | 1:bc877c37027c | 192 | Status init(); |
coisme | 0:78c512aa4d18 | 193 | |
coisme | 0:78c512aa4d18 | 194 | /** |
coisme | 0:78c512aa4d18 | 195 | * Reads the data registers including the configuration register. |
coisme | 0:78c512aa4d18 | 196 | * @param val Pointer to the buffer which stores ADC value. |
coisme | 0:78c512aa4d18 | 197 | * @param currentConfig Pointer to the structure which stores the current configuration. |
coisme | 0:78c512aa4d18 | 198 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:78c512aa4d18 | 199 | */ |
coisme | 1:bc877c37027c | 200 | Status readData(int16_t *val, Config *config); |
coisme | 0:78c512aa4d18 | 201 | |
coisme | 0:78c512aa4d18 | 202 | /** |
coisme | 0:78c512aa4d18 | 203 | * Sets the configuration register. |
coisme | 0:78c512aa4d18 | 204 | * @param Configuration to be set. |
coisme | 0:78c512aa4d18 | 205 | * @return SUCCESS when succeeded. Other value will be returned when error. |
coisme | 0:78c512aa4d18 | 206 | */ |
coisme | 0:78c512aa4d18 | 207 | Status setConfig(const Config *config); |
coisme | 1:bc877c37027c | 208 | |
coisme | 1:bc877c37027c | 209 | /** |
coisme | 1:bc877c37027c | 210 | * Decodes a configuration register value and put them into the specified Config structure. |
coisme | 1:bc877c37027c | 211 | * @param config Pointer to a Config structure to store the result. |
coisme | 1:bc877c37027c | 212 | * @param regVal Register value of the configuration register. |
coisme | 1:bc877c37027c | 213 | */ |
coisme | 1:bc877c37027c | 214 | void decodeConfigurationRegister(Config *config, uint8_t regVal); |
coisme | 0:78c512aa4d18 | 215 | }; |
coisme | 0:78c512aa4d18 | 216 | |
coisme | 0:78c512aa4d18 | 217 | #endif |