Driver for the Microchip MCP432x series of ADCs.

Dependents:   AKDP-RevD7_014

Fork of MCP342x by Osamu Koizumi

Committer:
tkstreet
Date:
Wed May 02 20:01:57 2018 +0000
Revision:
8:6e88da4abc41
Parent:
7:356650b8b01e
Child:
10:9e1bb1e16e68
Updated comments and added verbose debugging statements.

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