Driver for the Microchip MCP432x series of ADCs.

Dependents:   AKDP-RevD7_014

Fork of MCP342x by Osamu Koizumi

Committer:
coisme
Date:
Fri Apr 29 16:38:37 2016 +0000
Revision:
0:78c512aa4d18
Child:
1:bc877c37027c
Interface is defined.

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 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 0:78c512aa4d18 86
coisme 0:78c512aa4d18 87 /**
coisme 0:78c512aa4d18 88 * Constructor.
coisme 0:78c512aa4d18 89 *
coisme 0:78c512aa4d18 90 * @param conn instance of I2C
coisme 0:78c512aa4d18 91 * @param addr slave address of the device
coisme 0:78c512aa4d18 92 */
coisme 0:78c512aa4d18 93 MCP342X(I2C *conn, SlaveAddress addr);
coisme 0:78c512aa4d18 94
coisme 0:78c512aa4d18 95 /**
coisme 0:78c512aa4d18 96 * Sets a ADC channel.
coisme 0:78c512aa4d18 97 * @param ch ADC channel which to be the input.
coisme 0:78c512aa4d18 98 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 99 */
coisme 0:78c512aa4d18 100 Status setChannel(AdcChannel ch);
coisme 0:78c512aa4d18 101
coisme 0:78c512aa4d18 102 /**
coisme 0:78c512aa4d18 103 * Gets the current selected ADC channel.
coisme 0:78c512aa4d18 104 * @return ADC channel currently set.
coisme 0:78c512aa4d18 105 */
coisme 0:78c512aa4d18 106 AdcChannel getChannel();
coisme 0:78c512aa4d18 107
coisme 0:78c512aa4d18 108 /**
coisme 0:78c512aa4d18 109 * Sets a conversion mode.
coisme 0:78c512aa4d18 110 * @param mode Conversion mode which to be set.
coisme 0:78c512aa4d18 111 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 112 */
coisme 0:78c512aa4d18 113 Status setConversionMode(ConversionMode mode);
coisme 0:78c512aa4d18 114
coisme 0:78c512aa4d18 115 /**
coisme 0:78c512aa4d18 116 * Gets the current conversion mode.
coisme 0:78c512aa4d18 117 * @return Current conversion mode.
coisme 0:78c512aa4d18 118 */
coisme 0:78c512aa4d18 119 ConversionMode getConversionMode();
coisme 0:78c512aa4d18 120
coisme 0:78c512aa4d18 121 /**
coisme 0:78c512aa4d18 122 * Sets sample setting.
coisme 0:78c512aa4d18 123 * @param s Sample setting to be set.
coisme 0:78c512aa4d18 124 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 125 */
coisme 0:78c512aa4d18 126 Status setSampleSetting(SampleSetting s);
coisme 0:78c512aa4d18 127
coisme 0:78c512aa4d18 128 /**
coisme 0:78c512aa4d18 129 * Gets the current sample setting.
coisme 0:78c512aa4d18 130 * @return Current sample setting.
coisme 0:78c512aa4d18 131 */
coisme 0:78c512aa4d18 132 SampleSetting getSampleSetting();
coisme 0:78c512aa4d18 133
coisme 0:78c512aa4d18 134 /**
coisme 0:78c512aa4d18 135 * Sets gain of Programmable Gain Amplifier (PGA).
coisme 0:78c512aa4d18 136 * @param s PGA seeting to be set.
coisme 0:78c512aa4d18 137 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 138 */
coisme 0:78c512aa4d18 139 Status setPgaSetting(PgaSetting s);
coisme 0:78c512aa4d18 140
coisme 0:78c512aa4d18 141 /**
coisme 0:78c512aa4d18 142 * Gets the current PGA setting.
coisme 0:78c512aa4d18 143 * @return Current PGA setting.
coisme 0:78c512aa4d18 144 */
coisme 0:78c512aa4d18 145 PgaSetting getPgaSetting();
coisme 0:78c512aa4d18 146
coisme 0:78c512aa4d18 147 /**
coisme 0:78c512aa4d18 148 * Checks availability of new data.
coisme 0:78c512aa4d18 149 * @return Returns true if new data is available. Returns false if no new data is available.
coisme 0:78c512aa4d18 150 */
coisme 0:78c512aa4d18 151 bool isNewDataAvailable();
coisme 0:78c512aa4d18 152
coisme 0:78c512aa4d18 153 /**
coisme 0:78c512aa4d18 154 * Gets the AD value.
coisme 0:78c512aa4d18 155 * @return AD value.
coisme 0:78c512aa4d18 156 */
coisme 0:78c512aa4d18 157 int16_t getValue();
coisme 0:78c512aa4d18 158
coisme 0:78c512aa4d18 159 /**
coisme 0:78c512aa4d18 160 * Trigger AD conversion.
coisme 0:78c512aa4d18 161 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 162 */
coisme 0:78c512aa4d18 163 Status trigger();
coisme 0:78c512aa4d18 164
coisme 0:78c512aa4d18 165 /**
coisme 0:78c512aa4d18 166 * Gets the data conversion time.
coisme 0:78c512aa4d18 167 * @return Conversion time based on the current sample setting in milli-second unit.
coisme 0:78c512aa4d18 168 */
coisme 0:78c512aa4d18 169 float getDataConversionTime();
coisme 0:78c512aa4d18 170
coisme 0:78c512aa4d18 171 private:
coisme 0:78c512aa4d18 172 typedef struct {
coisme 0:78c512aa4d18 173 DataStatus dataStatus;
coisme 0:78c512aa4d18 174 ConversionMode conversionMode;
coisme 0:78c512aa4d18 175 SampleSetting sampleSetting;
coisme 0:78c512aa4d18 176 AdcChannel adcChannel;
coisme 0:78c512aa4d18 177 PGASetting pgaSetting;
coisme 0:78c512aa4d18 178 } Config;
coisme 0:78c512aa4d18 179
coisme 0:78c512aa4d18 180
coisme 0:78c512aa4d18 181 I2C *connection; /**< Pointer to an I2C object. */
coisme 0:78c512aa4d18 182 uint8_t slaveAddress; /**< Slave address. */
coisme 0:78c512aa4d18 183 uint8_t dataBuffer[3]; /**< Data buffer including configuration register. */
coisme 0:78c512aa4d18 184 uint8_t Config config; /**< Stores the latest configuration. */
coisme 0:78c512aa4d18 185
coisme 0:78c512aa4d18 186 /**
coisme 0:78c512aa4d18 187 * Reads the data registers including the configuration register.
coisme 0:78c512aa4d18 188 * @param val Pointer to the buffer which stores ADC value.
coisme 0:78c512aa4d18 189 * @param currentConfig Pointer to the structure which stores the current configuration.
coisme 0:78c512aa4d18 190 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 191 */
coisme 0:78c512aa4d18 192 Status readData(int16_t *val, Config *currentConfig);
coisme 0:78c512aa4d18 193
coisme 0:78c512aa4d18 194 /**
coisme 0:78c512aa4d18 195 * Sets the configuration register.
coisme 0:78c512aa4d18 196 * @param Configuration to be set.
coisme 0:78c512aa4d18 197 * @return SUCCESS when succeeded. Other value will be returned when error.
coisme 0:78c512aa4d18 198 */
coisme 0:78c512aa4d18 199 Status setConfig(const Config *config);
coisme 0:78c512aa4d18 200 };
coisme 0:78c512aa4d18 201
coisme 0:78c512aa4d18 202 #endif