Lukasz Uszko / MAX9611
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers max9611.h Source File

max9611.h

00001 /*
00002   @file MAX9611.h
00003 
00004   @brief MAX9611 High-Side, Current-Sense Amplifiers with
00005         12-Bit ADC and Op Amp/Comparator- Breakout I2C Library
00006 
00007   @Author lukasz uszko(luszko@op.pl)
00008 
00009   Tested on FRDM-KL46Z and FRDM-KL25Z
00010 
00011   Copyright (c) 2014 luszko
00012   Released under the MIT License (see http://mbed.org/license/mit)
00013 
00014   Documentation regarding the MAX9611 might be found here:
00015   http://datasheets.maximintegrated.com/en/ds/MAX9611-MAX9612.pdf
00016 */
00017 
00018 //DEMO - HOW TO USE:
00019 // NOTE before first use, do not forget to set mCsaCurrentValueOffset in constructor, it is a value that you can obtain from method: uint16_t readRawCSAOutValue(void),
00020 // when a current value flowing through shunt resistor equals 0.
00021 /*
00022 ---------------------------------------- DEMO: 1 version ----------------------------------------
00023 #include "mbed.h"
00024 #include "max9611.h"
00025 
00026 #define MAX9611_PIN_SDA PTC9    // I2C0    //for example
00027 #define MAX9611_PIN_SCL PTC8
00028 int main()
00029 {
00030     MAX9611 max9611(MAX9611_PIN_SDA, MAX9611_PIN_SCL);
00031     Serial debug(USBTX, USBRX);
00032     debug.baud(115200);
00033     while(1) {
00034         if(!max9611.readCSAOutputValue()){
00035             debug.printf("MAX9611_CSA_Reading ERROR!- check all connections\r\n");
00036         }
00037         else{
00038             debug.printf("MAX9611_CSA  %5.2f [mA]\r\n", max9611.getCSAOutput());
00039         }
00040 
00041         if(!max9611.readTemp()){
00042             debug.printf("MAX9611_TEMP_Reading ERROR!- check all connections\r\n");
00043         }
00044         else{
00045             debug.printf("MAX9611_TEMP:  %5.2f [C]\r\n", max9611.getTemp());
00046         }
00047 
00048 
00049     }
00050     return 0;
00051 }
00052 */
00053 
00054 
00055 
00056 
00057 
00058 #ifndef MAX9611_H
00059 #define MAX9611_H
00060 
00061 #include "mbed.h"
00062 
00063 #define MAX9611_I2C_ADDRESS 0xE1   //A0 and A1 PIN are conected to GND , Write address 0xE0, Read Address 0xE1
00064 
00065 
00066 
00067 
00068 
00069 
00070 class MAX9611
00071 {
00072 
00073 
00074     /**********private members and methods********************************/
00075 private:
00076 
00077     typedef enum {
00078         CHANNEL_A_0=0,  /*Read current-sense amplifier output from ADC, gain = 1x*/
00079         CHANNEL_A_1,    /*Read current-sense amplifier output from ADC, gain = 4x*/
00080         CHANNEL_A_2,    /*Read current-sense amplifier output from ADC, gain = 8x*/
00081         CHANNEL_B,      /*Read average voltage of RS+ (input common-mode voltage) from ADC*/
00082         CHANNEL_C,      /*Read voltage of OUT from ADC*/
00083         CHANNEL_D,      /*Read voltage of SET from ADC*/
00084         CHANNEL_E,      /*Read internal die temperature from ADC*/
00085         ALL_CHANNELS    /*Read all channels in fast-read mode, sequentially every 2ms. Uses last gain setting.*/
00086     } eCtrlReg1MUX;
00087 
00088     typedef enum {
00089         NORMAL_OPERATION_SHDN=0,
00090         SHUTDOWN_MODE
00091     } eCtrlReg1SHDN;
00092 
00093     typedef enum {
00094         NORMAL_OPERATION_LR=0,
00095         RESET
00096     } eCtrlReg1LR;
00097 
00098     typedef enum {
00099         NORMAL_OPERATION_MODE=0,
00100         COMPARATOR_MODE=7,
00101         OPAMP_MODE=3
00102     } eCtrlReg1MODE;
00103 
00104 //watchdog delay time
00105     typedef enum {
00106         _1MS=0,
00107         _100US=1
00108     } eCtrlReg2DTIM;
00109 
00110 //watchdog retry delay time
00111     typedef enum {
00112         _50MS=0,
00113         _10MS=1
00114     } eCtrlReg2RTIM;
00115 
00116     //watchdog retry delay time
00117     typedef enum {
00118         CSA_DATA_BYTE_MSB_ADRR= 0x00,
00119         CSA_DATA_BYTE_LSB_ADRR= 0x01,
00120         RS_DATA_BYTE_MSB_ADRR= 0x02,
00121         RS_DATA_BYTE_LSB_ADRR= 0x03,
00122         OUT_DATA_BYTE_MSB_ADRR= 0x04,
00123         OUT_DATA_BYTE_LSB_ADRR= 0x05,
00124         SET_DATA_BYTE_MSB_ADRR= 0x06,
00125         SET_DATA_BYTE_LSB_ADRR= 0x07,
00126         TEMP_DATA_BYTE_MSB_ADRR= 0x08,
00127         TEMP_DATA_BYTE_LSB_ADRR= 0x09,
00128         CONTROL_REGISTER_1_ADRR= 0x0A,
00129         CONTROL_REGISTER_2_ADRR= 0x0B
00130     } eRegAddresses;
00131 
00132 
00133 
00134     /** Write data to the given register
00135      *
00136      * @returns
00137      *   1 on success,
00138      *   0 on error
00139      */
00140     bool write(uint8_t regAddress, uint8_t* data,int dataLength);
00141 
00142     /** Write data to the given register
00143      * @param register Address
00144      * @param data to read
00145      * @param length of data to read
00146      * @returns
00147      *   1 on success,
00148      *   0 on error
00149      */
00150     bool read(uint8_t regAddress, uint8_t* data,int length);
00151 
00152 
00153     /** Make 12 bit data from 2 bytes received from thr device data read from Data regiters of Max9611/9612 are laid in the following way :
00154      *  Byte 1:  bit7-MSB12........bit0-MSB05 ;  Byte 2: bit7-LSB04.... bit4-LSB00
00155      * @param MSB byte
00156      * @param 4 bits of LSB bytes
00157      * @returns 1 2bit data
00158      *
00159      */
00160     inline uint16_t get12BitData(uint8_t msbByte,uint8_t lsbByte) {
00161         uint16_t data12Bit= (msbByte<<4)|((lsbByte>>4)&0x0F);
00162         return data12Bit;
00163     }
00164 
00165 
00166     inline uint16_t get9BitData(uint8_t msbByte,uint8_t lsbByte) {
00167         uint16_t data9Bit= (msbByte<<1)|((lsbByte>>6)&0x01);
00168         return data9Bit;
00169     }
00170 
00171 
00172     /** Compute a value of current coefficient to be used to mulitiple by rawData obained from CSA output in order to getting real current value in [mA]
00173      * @param empty
00174      * @returns coefficient that you can used to get real value of measured current depending on muxReg value
00175      *
00176      */
00177     inline float getCSACurrentCoeffmA(void) {
00178         float coeff=1;
00179         switch(mMuxReg) {
00180             case CHANNEL_A_0:    /*gain = 1x*/
00181                 coeff=1.075;
00182                 break;           
00183             case CHANNEL_A_1:    /*gain = 4x*/
00184                 coeff=0.269;
00185                 break;
00186             case CHANNEL_A_2:    /*gain = 8x*/
00187                 coeff=0.134;
00188                 break;
00189             default:                
00190                 break;
00191         }
00192         return coeff;
00193     }
00194 
00195     /**********protected members********************************/
00196 protected:
00197 
00198     I2C mI2c;
00199     int mI2cAddr;
00200     float mTemperature;
00201     float mCurrentSenseAmplifierOutput;
00202     uint16_t mCsaCurrentValueOffset;  //this parameter depends on your sensor
00203     uint8_t mMuxReg;//
00204 
00205 
00206     /**********public methods********************************/
00207 public:
00208 
00209     /** Create an MAX9611 instance
00210      * @param sda pin
00211      * @param scl pin
00212      * @param address: I2C slave address
00213      */
00214     MAX9611(PinName sda, PinName scl,int i2cFrequency=100000,int address = MAX9611_I2C_ADDRESS);
00215 
00216 
00217     /** Create a MAX9611 instance
00218      * @param i2c object
00219      * @param address: I2C slave address
00220      */
00221     MAX9611(I2C& i2c, int address = MAX9611_I2C_ADDRESS);
00222 
00223 
00224     /** Initialization: set member values and configuration registers, ought to be invoked in the body of constructor
00225      * @returns
00226      *    true on success,
00227      *    false on error
00228      */
00229     bool initMax9611(eCtrlReg1MUX mux= CHANNEL_A_1,
00230                      eCtrlReg1SHDN shdn= NORMAL_OPERATION_SHDN,
00231                      eCtrlReg1LR lr=NORMAL_OPERATION_LR,
00232                      eCtrlReg1MODE mode= NORMAL_OPERATION_MODE,
00233                      eCtrlReg2DTIM watchdogDelay= _1MS,
00234                      eCtrlReg2RTIM watchdogRetryDelay=_50MS);
00235 
00236 
00237     /** Read temperature from the MAX9611.
00238      * @param none
00239      * @returns
00240      *   1 on success,
00241      *   0 on error
00242      */
00243     bool readTemp(void);
00244 
00245 
00246     /** Get temperature from the last measurement
00247      *
00248      * @returns
00249      *   temperature (C)
00250      */
00251     inline float getTemp(void) {
00252         return mTemperature;
00253     }
00254 
00255 
00256     /** Read CSA output value from the MAX9611.
00257       * @param none
00258       * @returns
00259       *   1 on success,
00260       *   0 on error
00261       */
00262     bool readCSAOutputValue(void);
00263 
00264 
00265     /** Get value of CSA output from the last measurement
00266       *
00267       * @returns
00268       *   Current Value [mA]
00269       */
00270     inline float getCSAOutput(void) {
00271         return mCurrentSenseAmplifierOutput;
00272     }
00273 
00274     //DEBUG
00275     uint16_t mRawInt;
00276     uint16_t readRawControl(void);
00277     uint16_t readRawCSAOutValue(void);
00278     uint16_t readRawRsValue(void);
00279     uint16_t readRawOutValue(void);
00280 
00281 
00282 
00283 };
00284 
00285 #endif