Driver for the Microchip MCP432x series of ADCs.

Dependents:   AKDP-RevD7_014

Fork of MCP342x by Osamu Koizumi

Committer:
tkstreet
Date:
Fri Aug 10 17:32:01 2018 +0000
Revision:
10:9e1bb1e16e68
Parent:
8:6e88da4abc41
Removed calls to Debug library.

Who changed what in which revision?

UserRevisionLine numberNew 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 2:96d9bfe25b03 6 /**
coisme 2:96d9bfe25b03 7 * Device driver for MCP3426, MCP3427, and MCP3428.
coisme 2:96d9bfe25b03 8 * @note MCP342x is Analog-to-Digital Converter (ADC) IC with I2C interface.
coisme 2:96d9bfe25b03 9 *
coisme 2:96d9bfe25b03 10 * Example:
coisme 2:96d9bfe25b03 11 * @code
coisme 2:96d9bfe25b03 12 * #include "mbed.h"
coisme 2:96d9bfe25b03 13 * #include "mcp342x.h"
coisme 2:96d9bfe25b03 14 *
coisme 2:96d9bfe25b03 15 * #define I2C_SPEED_100KHZ 100000
coisme 2:96d9bfe25b03 16 * #define I2C_SPEED_400KHZ 400000
coisme 2:96d9bfe25b03 17 * #define PIN_SERIAL_TX P0_4
coisme 2:96d9bfe25b03 18 * #define PIN_SERIAL_RX P0_5
coisme 2:96d9bfe25b03 19 *
coisme 2:96d9bfe25b03 20 * int16_t getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) {
coisme 2:96d9bfe25b03 21 * // Configure channel and trigger.
coisme 2:96d9bfe25b03 22 * mcp3428->setChannel(ch);
coisme 2:96d9bfe25b03 23 * mcp3428->setSampleSetting(s);
coisme 2:96d9bfe25b03 24 * mcp3428->trigger();
coisme 2:96d9bfe25b03 25 *
coisme 2:96d9bfe25b03 26 * // polling data
coisme 2:96d9bfe25b03 27 * MCP342X::Data data;
coisme 2:96d9bfe25b03 28 * do {
coisme 2:96d9bfe25b03 29 * wait_ms(WAIT_ADC_MS);
coisme 2:96d9bfe25b03 30 * mcp3428->getData(&data);
coisme 2:96d9bfe25b03 31 * } while(data.st == MCP342X::DATA_NOT_UPDATED);
coisme 2:96d9bfe25b03 32 *
coisme 2:96d9bfe25b03 33 * return data.value;
coisme 2:96d9bfe25b03 34 * }
coisme 2:96d9bfe25b03 35 *
coisme 2:96d9bfe25b03 36 * int main(void) {
coisme 2:96d9bfe25b03 37 * // Instanciate I2C
coisme 2:96d9bfe25b03 38 * I2C i2c(I2C_SDA0, I2C_SCL0);
coisme 2:96d9bfe25b03 39 * i2c.frequency(I2C_SPEED_400KHZ);
coisme 2:96d9bfe25b03 40 *
coisme 2:96d9bfe25b03 41 * // Serial output for debug. (optional)
coisme 2:96d9bfe25b03 42 * Serial serial(PIN_SERIAL_TX, PIN_SERIAL_RX);
coisme 2:96d9bfe25b03 43 *
coisme 2:96d9bfe25b03 44 * // Instanciate MCP342x
coisme 2:96d9bfe25b03 45 * // Suppose that the slave address of MCP342x on your board is .
coisme 2:96d9bfe25b03 46 * MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_68H);
coisme 2:96d9bfe25b03 47 *
coisme 2:96d9bfe25b03 48 * // Sets MCP342x one-shot measurement mode.
coisme 2:96d9bfe25b03 49 * mcp342x.setSampleSetting(MCP342X::ONE_SHOT);
coisme 2:96d9bfe25b03 50 *
coisme 2:96d9bfe25b03 51 * while(true) {
coisme 2:96d9bfe25b03 52 * // Supposes that the device is MCP3428, which has 4 channels.
coisme 2:96d9bfe25b03 53 * const uint8_t CHANNEL_NUM = 4;
coisme 2:96d9bfe25b03 54 * // Sampling setting. Ch1 is 12-bit, Ch2 is 14-bit, Ch3 is 16-bit, Ch4 is 16-bit.
coisme 2:96d9bfe25b03 55 * const MCP342X::SampleSetting sampleSetting[CHANNEL_NUM] =
coisme 2:96d9bfe25b03 56 * {MCP342X::SAMPLE_240HZ_12BIT, MCP342X::SAMPLE_60HZ_14BIT,
coisme 2:96d9bfe25b03 57 * MCP342X::SAMPLE_15HZ_16BIT, MCP342X::SAMPLE_15HZ_16BIT};
coisme 2:96d9bfe25b03 58 * // Data buffer.
coisme 2:96d9bfe25b03 59 * int16_t data[CHANNEL_NUM];
coisme 2:96d9bfe25b03 60 * // Measures each channel.
coisme 2:96d9bfe25b03 61 * for (int i=0; i < CHANNEL_NUM; i++) {
coisme 2:96d9bfe25b03 62 * mcp342x.getAdcData(&data[i], (MCP342X::AdcChannel)i, sampleSetting[i]);
coisme 2:96d9bfe25b03 63 * }
coisme 2:96d9bfe25b03 64 * // Prints out the ADC results.
coisme 2:96d9bfe25b03 65 * serial.printf("%d, %d, %d, %d\r\n", data[0], data[1], data[2], data[3]);
coisme 2:96d9bfe25b03 66 * }
coisme 2:96d9bfe25b03 67 * }
coisme 2:96d9bfe25b03 68 * @endcode
coisme 2:96d9bfe25b03 69 */
coisme 0:78c512aa4d18 70 class MCP342X
coisme 0:78c512aa4d18 71 {
coisme 0:78c512aa4d18 72 public:
coisme 0:78c512aa4d18 73 /**
coisme 0:78c512aa4d18 74 * Slave addresses.
coisme 0:78c512aa4d18 75 */
coisme 0:78c512aa4d18 76 typedef enum {
coisme 2:96d9bfe25b03 77 SLAVE_ADDRESS_68H = 0x68, /**< When Adr0 pin = L and Adr1 pin = L, or Adr0 pin = float and Adr1 pin = float. */
coisme 2:96d9bfe25b03 78 SLAVE_ADDRESS_69H = 0x69, /**< When Adr0 pin = L and Adr1 pin = float. */
coisme 2:96d9bfe25b03 79 SLAVE_ADDRESS_6AH = 0x6A, /**< When Adr0 pin = L and Adr1 pin = H. */
coisme 2:96d9bfe25b03 80 SLAVE_ADDRESS_6CH = 0x6C, /**< When Adr0 pin = H and Adr1 pin = L. */
coisme 2:96d9bfe25b03 81 SLAVE_ADDRESS_6DH = 0x6D, /**< When Adr0 pin = H and Adr1 pin = float. */
coisme 2:96d9bfe25b03 82 SLAVE_ADDRESS_6EH = 0x6E, /**< When Adr0 pin = H and Adr1 pin = H. */
coisme 2:96d9bfe25b03 83 SLAVE_ADDRESS_6BH = 0x6B, /**< When Adr0 pin = float and Adr1 pin = L. */
coisme 2:96d9bfe25b03 84 SLAVE_ADDRESS_6FH = 0x6F, /**< When Adr0 pin = float and Adr1 pin = H. */
coisme 0:78c512aa4d18 85 } SlaveAddress;
coisme 0:78c512aa4d18 86
coisme 0:78c512aa4d18 87 /**
coisme 0:78c512aa4d18 88 * Status of function.
coisme 0:78c512aa4d18 89 */
coisme 0:78c512aa4d18 90 typedef enum {
coisme 0:78c512aa4d18 91 SUCCESS, /**< The function processed successfully. */
coisme 0:78c512aa4d18 92 ERROR_I2C_READ, /**< Error related to I2C read. */
coisme 0:78c512aa4d18 93 ERROR_I2C_WRITE, /**< Error related to I2C write. */
coisme 0:78c512aa4d18 94 ERROR, /**< General Error */
coisme 0:78c512aa4d18 95 } Status;
coisme 0:78c512aa4d18 96
coisme 0:78c512aa4d18 97 /**
coisme 0:78c512aa4d18 98 * Conversion mode setting.
coisme 0:78c512aa4d18 99 */
coisme 0:78c512aa4d18 100 typedef enum {
coisme 0:78c512aa4d18 101 CONTINUOUS, /**< Continuous conversion mode. Default. */
coisme 0:78c512aa4d18 102 ONE_SHOT, /**< One-shot conversion mode. */
coisme 0:78c512aa4d18 103 } ConversionMode;
coisme 0:78c512aa4d18 104
coisme 0:78c512aa4d18 105 /**
coisme 0:78c512aa4d18 106 * Data ready status.
coisme 0:78c512aa4d18 107 */
coisme 0:78c512aa4d18 108 typedef enum {
coisme 0:78c512aa4d18 109 DATA_NOT_UPDATED, /**< Output register has not been updated. */
coisme 0:78c512aa4d18 110 DATA_UPDATED, /**< Output register has been updated with the latest conversion result. */
coisme 0:78c512aa4d18 111 } DataStatus;
coisme 0:78c512aa4d18 112
coisme 0:78c512aa4d18 113 /**
coisme 0:78c512aa4d18 114 * Measurement trigger command.
coisme 0:78c512aa4d18 115 */
coisme 0:78c512aa4d18 116 typedef enum {
coisme 0:78c512aa4d18 117 TRIGGER, /**< Initiate a new conversion. */
coisme 0:78c512aa4d18 118 NONE, /**< No effect. */
coisme 0:78c512aa4d18 119 } MeasurementTrigger;
coisme 0:78c512aa4d18 120
coisme 0:78c512aa4d18 121 /**
coisme 0:78c512aa4d18 122 * Sample rate and resolution setting.
coisme 0:78c512aa4d18 123 */
coisme 0:78c512aa4d18 124 typedef enum {
coisme 0:78c512aa4d18 125 SAMPLE_240HZ_12BIT, /**< 240 sample per second with 12 bit data. Default. */
coisme 0:78c512aa4d18 126 SAMPLE_60HZ_14BIT, /**< 60 sample per second with 14 bit data. */
coisme 0:78c512aa4d18 127 SAMPLE_15HZ_16BIT, /**< 15 sample per second with 16 bit data. */
coisme 0:78c512aa4d18 128 } SampleSetting;
coisme 0:78c512aa4d18 129
coisme 0:78c512aa4d18 130
coisme 0:78c512aa4d18 131 /**
coisme 0:78c512aa4d18 132 * ADC channel selection.
coisme 0:78c512aa4d18 133 */
coisme 0:78c512aa4d18 134 typedef enum {
coisme 2:96d9bfe25b03 135 ADC_CH1 = 0, /**< Channel 1, default. */
coisme 2:96d9bfe25b03 136 ADC_CH2 = 1, /**< Channel 2 */
coisme 2:96d9bfe25b03 137 ADC_CH3 = 2, /**< Channel 3, MCP3428 only, treated as channel 1 by the MCP3426/MCP3427. */
coisme 2:96d9bfe25b03 138 ADC_CH4 = 3, /**< Channel 4, MCP3428 only, treated as channel 2 by the MCP3426/MCP3427. */
coisme 0:78c512aa4d18 139 } AdcChannel;
coisme 0:78c512aa4d18 140
coisme 0:78c512aa4d18 141 /**
coisme 0:78c512aa4d18 142 * Programmable Gain Amplifier setting.
coisme 0:78c512aa4d18 143 */
coisme 0:78c512aa4d18 144 typedef enum {
coisme 0:78c512aa4d18 145 PGA_1X, /**< Gain 1x, Default. */
coisme 0:78c512aa4d18 146 PGA_2X, /**< Gain 2x. */
coisme 0:78c512aa4d18 147 PGA_4X, /**< Gain 4x. */
coisme 0:78c512aa4d18 148 PGA_8X, /**< Gain 8x. */
coisme 0:78c512aa4d18 149 } PgaSetting;
coisme 1:bc877c37027c 150
coisme 1:bc877c37027c 151 /**
coisme 2:96d9bfe25b03 152 * ADC result.
coisme 1:bc877c37027c 153 */
coisme 1:bc877c37027c 154 typedef struct {
coisme 1:bc877c37027c 155 DataStatus st;
tkstreet 8:6e88da4abc41 156 int16_t value; /**< ADC value. The value takes from -2^(N-1) to 2^(N-1) - 1 for N-bit sampling. */
coisme 1:bc877c37027c 157 } Data;
coisme 1:bc877c37027c 158
coisme 0:78c512aa4d18 159 /**
coisme 0:78c512aa4d18 160 * Constructor.
coisme 0:78c512aa4d18 161 *
coisme 2:96d9bfe25b03 162 * @param conn Pointer to an instance of I2C.
coisme 2:96d9bfe25b03 163 * @param addr Slave address of the device.
coisme 0:78c512aa4d18 164 */
coisme 0:78c512aa4d18 165 MCP342X(I2C *conn, SlaveAddress addr);
coisme 0:78c512aa4d18 166
coisme 0:78c512aa4d18 167 /**
masahikofukasawa 4:6215f50b3297 168 * Destructor.
masahikofukasawa 4:6215f50b3297 169 *
masahikofukasawa 4:6215f50b3297 170 */
masahikofukasawa 4:6215f50b3297 171 // ~MCP342X();
masahikofukasawa 4:6215f50b3297 172
masahikofukasawa 4:6215f50b3297 173 /**
coisme 0:78c512aa4d18 174 * Sets a ADC channel.
coisme 0:78c512aa4d18 175 * @param ch ADC channel which to be the input.
coisme 0:78c512aa4d18 176 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 177 */
coisme 0:78c512aa4d18 178 Status setChannel(AdcChannel ch);
coisme 0:78c512aa4d18 179
coisme 0:78c512aa4d18 180 /**
coisme 0:78c512aa4d18 181 * Gets the current selected ADC channel.
coisme 0:78c512aa4d18 182 * @return ADC channel currently set.
coisme 0:78c512aa4d18 183 */
coisme 0:78c512aa4d18 184 AdcChannel getChannel();
coisme 0:78c512aa4d18 185
coisme 0:78c512aa4d18 186 /**
coisme 0:78c512aa4d18 187 * Sets a conversion mode.
coisme 0:78c512aa4d18 188 * @param mode Conversion mode which to be set.
coisme 0:78c512aa4d18 189 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 190 */
coisme 0:78c512aa4d18 191 Status setConversionMode(ConversionMode mode);
coisme 0:78c512aa4d18 192
coisme 0:78c512aa4d18 193 /**
coisme 0:78c512aa4d18 194 * Gets the current conversion mode.
coisme 0:78c512aa4d18 195 * @return Current conversion mode.
coisme 0:78c512aa4d18 196 */
coisme 0:78c512aa4d18 197 ConversionMode getConversionMode();
coisme 0:78c512aa4d18 198
coisme 0:78c512aa4d18 199 /**
coisme 2:96d9bfe25b03 200 * Sets sample setting, i.e. sampling frequency and resolution bits.
coisme 0:78c512aa4d18 201 * @param s Sample setting to be set.
coisme 0:78c512aa4d18 202 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 203 */
coisme 0:78c512aa4d18 204 Status setSampleSetting(SampleSetting s);
coisme 0:78c512aa4d18 205
coisme 0:78c512aa4d18 206 /**
coisme 0:78c512aa4d18 207 * Gets the current sample setting.
coisme 0:78c512aa4d18 208 * @return Current sample setting.
coisme 0:78c512aa4d18 209 */
coisme 0:78c512aa4d18 210 SampleSetting getSampleSetting();
coisme 0:78c512aa4d18 211
coisme 0:78c512aa4d18 212 /**
coisme 2:96d9bfe25b03 213 * Sets the gain of Programmable Gain Amplifier (PGA).
coisme 0:78c512aa4d18 214 * @param s PGA seeting to be set.
coisme 0:78c512aa4d18 215 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 216 */
coisme 0:78c512aa4d18 217 Status setPgaSetting(PgaSetting s);
coisme 0:78c512aa4d18 218
coisme 0:78c512aa4d18 219 /**
coisme 2:96d9bfe25b03 220 * Gets the current Programmable Gain Amplifier (PGA) setting.
coisme 0:78c512aa4d18 221 * @return Current PGA setting.
coisme 0:78c512aa4d18 222 */
coisme 0:78c512aa4d18 223 PgaSetting getPgaSetting();
coisme 0:78c512aa4d18 224
coisme 0:78c512aa4d18 225 /**
coisme 0:78c512aa4d18 226 * Gets the AD value.
coisme 0:78c512aa4d18 227 * @return AD value.
coisme 0:78c512aa4d18 228 */
coisme 1:bc877c37027c 229 Status getData(Data *pt);
coisme 0:78c512aa4d18 230
coisme 0:78c512aa4d18 231 /**
coisme 2:96d9bfe25b03 232 * Trigger AD conversion. In continuous measurement mode, this function has no effect.
coisme 0:78c512aa4d18 233 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 234 */
coisme 0:78c512aa4d18 235 Status trigger();
coisme 0:78c512aa4d18 236
coisme 0:78c512aa4d18 237 private:
coisme 0:78c512aa4d18 238 typedef struct {
coisme 1:bc877c37027c 239 MeasurementTrigger measurementTrigger;
coisme 1:bc877c37027c 240 DataStatus dataStatus;
coisme 1:bc877c37027c 241 ConversionMode conversionMode;
coisme 1:bc877c37027c 242 SampleSetting sampleSetting;
coisme 1:bc877c37027c 243 AdcChannel adcChannel;
coisme 1:bc877c37027c 244 PgaSetting pgaSetting;
coisme 0:78c512aa4d18 245 } Config;
coisme 0:78c512aa4d18 246
coisme 0:78c512aa4d18 247
coisme 0:78c512aa4d18 248 I2C *connection; /**< Pointer to an I2C object. */
coisme 0:78c512aa4d18 249 uint8_t slaveAddress; /**< Slave address. */
coisme 1:bc877c37027c 250 Config currentConfig; /**< Stores the latest configuration. */
coisme 1:bc877c37027c 251
coisme 1:bc877c37027c 252 /**
coisme 1:bc877c37027c 253 * Initialize this device.
coisme 1:bc877c37027c 254 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 1:bc877c37027c 255 */
coisme 1:bc877c37027c 256 Status init();
coisme 0:78c512aa4d18 257
coisme 0:78c512aa4d18 258 /**
coisme 0:78c512aa4d18 259 * Reads the data registers including the configuration register.
coisme 0:78c512aa4d18 260 * @param val Pointer to the buffer which stores ADC value.
coisme 0:78c512aa4d18 261 * @param currentConfig Pointer to the structure which stores the current configuration.
coisme 0:78c512aa4d18 262 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 263 */
coisme 1:bc877c37027c 264 Status readData(int16_t *val, Config *config);
coisme 0:78c512aa4d18 265
coisme 0:78c512aa4d18 266 /**
coisme 0:78c512aa4d18 267 * Sets the configuration register.
coisme 0:78c512aa4d18 268 * @param Configuration to be set.
coisme 0:78c512aa4d18 269 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 270 */
coisme 0:78c512aa4d18 271 Status setConfig(const Config *config);
coisme 1:bc877c37027c 272
coisme 1:bc877c37027c 273 /**
coisme 1:bc877c37027c 274 * Decodes a configuration register value and put them into the specified Config structure.
coisme 1:bc877c37027c 275 * @param config Pointer to a Config structure to store the result.
coisme 1:bc877c37027c 276 * @param regVal Register value of the configuration register.
coisme 1:bc877c37027c 277 */
coisme 1:bc877c37027c 278 void decodeConfigurationRegister(Config *config, uint8_t regVal);
coisme 0:78c512aa4d18 279 };
coisme 0:78c512aa4d18 280
coisme 0:78c512aa4d18 281 #endif