Osamu Koizumi / MCP342x
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mcp342x.h Source File

mcp342x.h

00001 #ifndef __MCP342X_H__
00002 #define __MCP342X_H__
00003 
00004 #include "mbed.h"
00005 
00006 /**
00007  * Device driver for MCP3426, MCP3427, and MCP3428.
00008  * @note MCP342x is Analog-to-Digital Converter (ADC) IC with I2C interface.
00009  *
00010  * Example:
00011  * @code
00012  * #include "mbed.h"
00013  * #include "mcp342x.h"
00014  *
00015  * #define I2C_SPEED_100KHZ    100000
00016  * #define I2C_SPEED_400KHZ    400000
00017  * #define PIN_SERIAL_TX   P0_4
00018  * #define PIN_SERIAL_RX   P0_5 
00019  *
00020  * int16_t getAdcData(MCP342X *mcp3428, MCP342X::AdcChannel ch, MCP342X::SampleSetting s) {
00021  *     // Configure channel and trigger.
00022  *     mcp3428->setChannel(ch);
00023  *     mcp3428->setSampleSetting(s);
00024  *     mcp3428->trigger();
00025  *
00026  *     // polling data
00027  *     MCP342X::Data data;
00028  *     do {
00029  *         wait_ms(WAIT_ADC_MS);
00030  *         mcp3428->getData(&data);
00031  *     } while(data.st == MCP342X::DATA_NOT_UPDATED);
00032  *
00033  *     return data.value;
00034  * }
00035  *
00036  * int main(void) {
00037  *     // Instanciate I2C
00038  *     I2C i2c(I2C_SDA0, I2C_SCL0);
00039  *     i2c.frequency(I2C_SPEED_400KHZ);
00040  *
00041  *     // Serial output for debug. (optional)
00042  *     Serial serial(PIN_SERIAL_TX, PIN_SERIAL_RX);
00043  *
00044  *     // Instanciate MCP342x
00045  *     // Suppose that the slave address of MCP342x on your board is .
00046  *     MCP342X mcp342x(&i2c, MCP342X::SLAVE_ADDRESS_68H);
00047  *
00048  *     // Sets MCP342x one-shot measurement mode.
00049  *     mcp342x.setSampleSetting(MCP342X::ONE_SHOT); 
00050  *
00051  *     while(true) {
00052  *         // Supposes that the device is MCP3428, which has 4 channels.
00053  *         const uint8_t CHANNEL_NUM = 4;
00054  *         // Sampling setting. Ch1 is 12-bit, Ch2 is 14-bit, Ch3 is 16-bit, Ch4 is 16-bit.
00055  *         const MCP342X::SampleSetting sampleSetting[CHANNEL_NUM] = 
00056  *                 {MCP342X::SAMPLE_240HZ_12BIT, MCP342X::SAMPLE_60HZ_14BIT,
00057  *                  MCP342X::SAMPLE_15HZ_16BIT, MCP342X::SAMPLE_15HZ_16BIT};
00058  *         // Data buffer.
00059  *         int16_t data[CHANNEL_NUM];
00060  *         // Measures each channel.
00061  *         for (int i=0; i < CHANNEL_NUM; i++) {
00062  *             mcp342x.getAdcData(&data[i], (MCP342X::AdcChannel)i, sampleSetting[i]);
00063  *         }
00064  *         // Prints out the ADC results.
00065  *         serial.printf("%d, %d, %d, %d\r\n", data[0], data[1], data[2], data[3]);
00066  *     }
00067  * }
00068  * @endcode
00069  */
00070 class MCP342X
00071 {
00072 public:
00073     /**
00074      * Slave addresses.
00075      */
00076     typedef enum {
00077         SLAVE_ADDRESS_68H = 0x68,     /**< When Adr0 pin = L and Adr1 pin = L, or Adr0 pin = float and Adr1 pin = float. */
00078         SLAVE_ADDRESS_69H = 0x69,     /**< When Adr0 pin = L and Adr1 pin = float. */
00079         SLAVE_ADDRESS_6AH = 0x6A,     /**< When Adr0 pin = L and Adr1 pin = H. */
00080         SLAVE_ADDRESS_6CH = 0x6C,     /**< When Adr0 pin = H and Adr1 pin = L. */
00081         SLAVE_ADDRESS_6DH = 0x6D,     /**< When Adr0 pin = H and Adr1 pin = float. */
00082         SLAVE_ADDRESS_6EH = 0x6E,     /**< When Adr0 pin = H and Adr1 pin = H. */
00083         SLAVE_ADDRESS_6BH = 0x6B,     /**< When Adr0 pin = float and Adr1 pin = L. */
00084         SLAVE_ADDRESS_6FH = 0x6F,     /**< When Adr0 pin = float and Adr1 pin = H. */
00085     } SlaveAddress;
00086     
00087     /**
00088      * Status of function. 
00089      */
00090     typedef enum {
00091         SUCCESS,               /**< The function processed successfully. */
00092         ERROR_I2C_READ,        /**< Error related to I2C read. */
00093         ERROR_I2C_WRITE,       /**< Error related to I2C write. */
00094         ERROR,                 /**< General Error */
00095     } Status;
00096 
00097     /**
00098      * Conversion mode setting.
00099      */
00100     typedef enum {
00101         CONTINUOUS,            /**< Continuous conversion mode. Default. */
00102         ONE_SHOT,              /**< One-shot conversion mode. */
00103     } ConversionMode;
00104 
00105     /**
00106      * Data ready status.
00107      */
00108     typedef enum {
00109         DATA_NOT_UPDATED,       /**< Output register has not been updated. */
00110         DATA_UPDATED,           /**< Output register has been updated with the latest conversion result. */
00111     } DataStatus;
00112 
00113     /**
00114      * Measurement trigger command.
00115      */
00116     typedef enum {
00117         TRIGGER,              /**< Initiate a new conversion. */
00118         NONE,                 /**< No effect. */
00119     } MeasurementTrigger;
00120     
00121     /**
00122      * Sample rate and resolution setting.
00123      */
00124     typedef enum {
00125         SAMPLE_240HZ_12BIT,       /**< 240 sample per second with 12 bit data. Default. */
00126         SAMPLE_60HZ_14BIT,        /**< 60 sample per second with 14 bit data. */
00127         SAMPLE_15HZ_16BIT,        /**< 15 sample per second with 16 bit data. */
00128     } SampleSetting;
00129 
00130     
00131     /**
00132      * ADC channel selection.
00133      */
00134     typedef enum {
00135         ADC_CH1 = 0,                 /**< Channel 1, default. */
00136         ADC_CH2 = 1,                 /**< Channel 2 */
00137         ADC_CH3 = 2,                 /**< Channel 3, MCP3428 only, treated as channel 1 by the MCP3426/MCP3427. */
00138         ADC_CH4 = 3,                 /**< Channel 4, MCP3428 only, treated as channel 2 by the MCP3426/MCP3427. */
00139     } AdcChannel;
00140     
00141     /**
00142      * Programmable Gain Amplifier setting.
00143      */
00144     typedef enum {
00145         PGA_1X,                  /**< Gain 1x, Default. */
00146         PGA_2X,                  /**< Gain 2x. */
00147         PGA_4X,                  /**< Gain 4x. */
00148         PGA_8X,                  /**< Gain 8x. */
00149     } PgaSetting;
00150     
00151     /**
00152      * ADC result.
00153      */
00154     typedef struct {
00155         DataStatus st;
00156         int16_t value;  /**< ADC value. The value takes from -2^11 to (2^11 - 1) when 12 bit sample mode, from -2^13 to (2^13 - 1) when 14 bit sample mode, from -2^15 to (2^15 - 1) when 16bit sample mode. */
00157     } Data;
00158     
00159     /**
00160      * Constructor.
00161      *
00162      * @param conn Pointer to an instance of I2C.
00163      * @param addr Slave address of the device.
00164      */
00165     MCP342X(I2C *conn, SlaveAddress addr);
00166 
00167     /**
00168      * Sets a ADC channel.
00169      * @param ch ADC channel which to be the input.
00170      * @return SUCCESS when succeeded. Other value will be returned when error.
00171      */
00172     Status setChannel(AdcChannel ch);
00173     
00174     /**
00175      * Gets the current selected ADC channel.
00176      * @return ADC channel currently set.
00177      */
00178     AdcChannel getChannel();
00179     
00180     /**
00181      * Sets a conversion mode.
00182      * @param mode Conversion mode which to be set.
00183      * @return SUCCESS when succeeded. Other value will be returned when error.
00184      */
00185     Status setConversionMode(ConversionMode mode);
00186 
00187     /**
00188      * Gets the current conversion mode.
00189      * @return Current conversion mode.
00190      */
00191     ConversionMode getConversionMode();
00192 
00193     /**
00194      * Sets sample setting, i.e. sampling frequency and resolution bits.
00195      * @param s Sample setting to be set.
00196      * @return SUCCESS when succeeded. Other value will be returned when error.
00197      */
00198     Status setSampleSetting(SampleSetting s);
00199 
00200     /**
00201      * Gets the current sample setting.
00202      * @return Current sample setting.
00203      */
00204     SampleSetting getSampleSetting();
00205     
00206     /**
00207      * Sets the gain of Programmable Gain Amplifier (PGA).
00208      * @param s PGA seeting to be set.
00209      * @return SUCCESS when succeeded. Other value will be returned when error.
00210      */
00211     Status setPgaSetting(PgaSetting s);
00212     
00213     /**
00214      * Gets the current Programmable Gain Amplifier (PGA) setting.
00215      * @return Current PGA setting.
00216      */
00217     PgaSetting getPgaSetting();
00218 
00219     /**
00220      * Gets the AD value.
00221      * @return AD value.
00222      */
00223     Status getData(Data *pt);
00224     
00225     /**
00226      * Trigger AD conversion. In continuous measurement mode, this function has no effect.
00227      * @return SUCCESS when succeeded. Other value will be returned when error.
00228      */
00229     Status trigger();
00230     
00231 private:
00232     typedef struct {
00233         MeasurementTrigger   measurementTrigger;
00234         DataStatus           dataStatus;
00235         ConversionMode       conversionMode;
00236         SampleSetting        sampleSetting;
00237         AdcChannel           adcChannel;
00238         PgaSetting           pgaSetting;
00239     } Config;
00240 
00241 
00242     I2C *connection;        /**< Pointer to an I2C object. */
00243     uint8_t slaveAddress;   /**< Slave address. */
00244     Config currentConfig;  /**< Stores the latest configuration. */
00245 
00246     /**
00247      * Initialize this device.
00248      * @return SUCCESS when succeeded. Other value will be returned when error.
00249      */
00250     Status init();
00251 
00252     /**
00253      * Reads the data registers including the configuration register.
00254      * @param val Pointer to the buffer which stores ADC value.
00255      * @param currentConfig Pointer to the structure which stores the current configuration.
00256      * @return SUCCESS when succeeded. Other value will be returned when error.
00257      */
00258     Status readData(int16_t *val, Config *config);
00259     
00260     /**
00261      * Sets the configuration register.
00262      * @param Configuration to be set.
00263      * @return SUCCESS when succeeded. Other value will be returned when error.
00264      */
00265     Status setConfig(const Config *config);
00266     
00267     /**
00268      * Decodes a configuration register value and put them into the specified Config structure.
00269      * @param config Pointer to a Config structure to store the result.
00270      * @param regVal Register value of the configuration register.
00271      */
00272     void decodeConfigurationRegister(Config *config, uint8_t regVal);
00273 };
00274 
00275 #endif