INA219 (Texas Instruments) High-Side Measurement,Bi-Directional CURRENT/POWER MONITOR with I2C Interface

Dependents:   BratRobot

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers INA219.h Source File

INA219.h

00001 /*
00002  * mbed library program
00003  *  INA219 High-Side Measurement,Bi-Directional CURRENT/POWER MONITOR with I2C Interface
00004  *  by Texas Instruments
00005  *
00006  * Copyright (c) 2015,'17 Kenji Arai / JH1PJL
00007  *  http://www.page.sannet.ne.jp/kenjia/index.html
00008  *  http://mbed.org/users/kenjiArai/
00009  *      Created: January    25th, 2015
00010  *      Revised: August     23rd, 2017
00011  */
00012 /*
00013  *---------------- REFERENCE ----------------------------------------------------------------------
00014  * Original Information
00015  *  http://www.ti.com/product/INA219/description
00016  *  http://www.ti.com/lit/ds/sbos448f/sbos448f.pdf
00017  * Device kit
00018  *  https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/overview
00019  *  http://akizukidenshi.com/catalog/g/gM-08221/
00020  */
00021 
00022 #ifndef        MBED_INA219
00023 #define        MBED_INA219
00024 
00025 /////////// ADDRESS /////////////////////////////
00026 //  7bit address = 0b1000000(0x40)
00027 //  G=GND, V=VS+, A=SDA, L=SCL
00028 //  e.g. _VG: A1=VS+, A0=GND
00029 //    -> Please make sure your H/W configuration
00030 // Set data into "addr"
00031 #define INA219_ADDR_GG             (0x40 << 1)
00032 #define INA219_ADDR_GV             (0x41 << 1)
00033 #define INA219_ADDR_GA             (0x42 << 1)
00034 #define INA219_ADDR_GL             (0x43 << 1)
00035 #define INA219_ADDR_VG             (0x44 << 1)
00036 #define INA219_ADDR_VV             (0x45 << 1)
00037 #define INA219_ADDR_VA             (0x46 << 1)
00038 #define INA219_ADDR_VL             (0x47 << 1)
00039 #define INA219_ADDR_AG             (0x48 << 1)
00040 #define INA219_ADDR_AV             (0x49 << 1)
00041 #define INA219_ADDR_AA             (0x4a << 1)
00042 #define INA219_ADDR_AL             (0x4b << 1)
00043 #define INA219_ADDR_LG             (0x4c << 1)
00044 #define INA219_ADDR_LV             (0x4d << 1)
00045 #define INA219_ADDR_LA             (0x4e << 1)
00046 #define INA219_ADDR_LL             (0x4f << 1)
00047 
00048 /////////// REGISTER DEFINITION /////////////////
00049 #define INA219_CONFIG              0x00
00050 #define INA219_SHUNT_V             0x01
00051 #define INA219_BUS_VOLT            0x02
00052 #define INA219_POWER               0x03
00053 #define INA219_CURRENT             0x04
00054 #define INA219_CALBLATION          0x05
00055 
00056 /////////// PARAMETER SETTING ///////////////////
00057 // Set data into "shunt_register"
00058 #define INA219_PAR_R_100MOHM       100
00059 #define INA219_PAR_R_200MORM       200
00060 #define INA219_PAR_R_MORM(x)       (x)
00061 // Set data into "v_max"
00062 #define INA219_PAR_V_16V           0
00063 #define INA219_PAR_V_32V           1    // Default
00064 // Set data into "gain"
00065 #define INA219_PAR_G_40MV          0    // 400[mA] max if R=0.1[Ohm]
00066 #define INA219_PAR_G_80MV          1
00067 #define INA219_PAR_G_160MV         2
00068 #define INA219_PAR_G_320MV         3    // Default
00069 // Set data into "bus_adc_resolution"
00070 #define INA219_PAR_B_9B_X1_84US    0x0
00071 #define INA219_PAR_B_10B_X1_148US  0x1
00072 #define INA219_PAR_B_11B_X1_276US  0x2
00073 #define INA219_PAR_B_12B_X1_532US  0x3  // Default
00074 #define INA219_PAR_B_12B_X2_1R06MS 0x9
00075 #define INA219_PAR_B_12B_X4_2R13MS 0xa
00076 #define INA219_PAR_B_12B_X8_4R26MS 0xb
00077 #define INA219_PAR_B_12B_X16_8MS   0xc
00078 #define INA219_PAR_B_12B_X32_17MS  0xd
00079 #define INA219_PAR_B_12B_X64_34MS  0xe
00080 #define INA219_PAR_B_12B_X128_68MS 0xf
00081 // Set data into "shunt_adc_resolution"
00082 #define INA219_PAR_S_9B_X1_84US    0x0
00083 #define INA219_PAR_S_10B_X1_148US  0x1
00084 #define INA219_PAR_S_11B_X1_276US  0x2
00085 #define INA219_PAR_S_12B_X1_532US  0x3  // Default
00086 #define INA219_PAR_S_12B_X2_1R06MS 0x9
00087 #define INA219_PAR_S_12B_X4_2R13MS 0xa
00088 #define INA219_PAR_S_12B_X8_4R26MS 0xb
00089 #define INA219_PAR_S_12B_X16_8MS   0xc
00090 #define INA219_PAR_S_12B_X32_17MS  0xd
00091 #define INA219_PAR_S_12B_X64_34MS  0xe
00092 #define INA219_PAR_S_12B_X128_68MS 0xf
00093 // Set data into "mode"
00094 #define INA219_PAR_M_PDWN          0
00095 #define INA219_PAR_M_SHNT_TRG      1
00096 #define INA219_PAR_M_BUS_TRG       2
00097 #define INA219_PAR_M_SHNTBUS_TRG   3
00098 #define INA219_PAR_M_ADC_OFF       4
00099 #define INA219_PAR_M_SHNT_CONT     5
00100 #define INA219_PAR_M_BUS_CONT      6
00101 #define INA219_PAR_M_SHNTBUS_CONT  7    // Default
00102 
00103 ////////////// DATA TYPE DEFINITION ///////////////////////
00104 typedef struct {
00105     // I2C Address
00106     uint8_t addr;
00107     // CONFIG REG
00108     uint8_t shunt_register;
00109     uint8_t v_max;
00110     uint8_t gain;
00111     uint8_t bus_adc_resolution;
00112     uint8_t Shunt_adc_resolution;
00113     uint8_t mode;
00114     // CALBLATION REG
00115     uint16_t calibration_data;
00116 } INA219_TypeDef;
00117 
00118 ////////////// DEFAULT SETTING ////////////////////////////
00119 // Standard parameter for easy set-up
00120 const INA219_TypeDef ina219_std_paramtr = {
00121     // I2C Address
00122     INA219_ADDR_VV,
00123     // CONFIG REG
00124     INA219_PAR_R_100MOHM,       // 100 milli-ohm
00125     INA219_PAR_V_16V,           // 16V max
00126     INA219_PAR_G_40MV,          // Gain x1 (40mV -> 400mA max with 100 milliOhm)
00127     INA219_PAR_B_12B_X1_532US,  // Bus/resolution 12bit & one time convertion
00128     INA219_PAR_S_12B_X1_532US,  // Shunt/resolution 12bit & one time convertion
00129     INA219_PAR_M_SHNTBUS_CONT,  // Measure continuously both Shunt voltage and Bus voltage
00130     // CALBLATION REG
00131     16384                       // Calibration data
00132 };
00133 
00134 /** INA219 High-Side Measurement,Bi-Directional CURRENT/POWER MONITOR with I2C Interface
00135  *
00136  * @code
00137  * //--------- Default setting -----------------
00138  * #include "mbed.h"
00139  * #include "INA219.h"
00140  *
00141  * // I2C Communication
00142  * INA219 current(dp5, dp27, INA219_ADDR_GG);
00143  * // If you connected I2C line not only this device but also other devices,
00144  * //     you need to declare following method.
00145  * I2C    i2c(dp5, dp27);
00146  * INA219 current(I2C& p_i2c, INA219_ADDR_GG);
00147  *
00148  * int main() {
00149  *     while(1){
00150  *         printf("I=%+6.3f [mA]\r\n", current.read_current());
00151  *         wait(1.0):
00152  *     }
00153  * }
00154  * //--------- Detail setting -----------------
00155  * #include "mbed.h"
00156  * #include "INA219.h"
00157  *
00158  * const INA219_TypeDef ina219_my_paramtr = {
00159  *    // I2C Address
00160  *    INA219_ADDR_GG,
00161  *    // CONFIG Reg.
00162  *    INA219_PAR_R_100MOHM,     // 100 milli-ohm
00163  *    INA219_CFG_B16V,          // 16V max
00164  *    INA219_PAR_G_40MV,        // Gain x1
00165  *    INA219_PAR_M_SHNTBUS_CONT,// Measure continuously
00166  *    // CALIB
00167  *    16384                     // Calibration data is nothing
00168  * };
00169  *
00170  * I2C    i2c(dp5,dp27);
00171  * INA219 current(I2C& p_i2c, &ina219_my_paramtr);
00172  *
00173  * int main() {
00174  *     while(1){
00175  *         printf("I=%+6.3f [mA]\r\n", current.read_current());
00176  *         wait(1.0):
00177  *     }
00178  * }
00179  * @endcode
00180  */
00181 
00182 class INA219
00183 {
00184 public:
00185     /** Configure data pin
00186       * @param data SDA and SCL pins
00187       * @param parameter address chip (INA219_TypeDef)
00188       * @param or just set address or just port
00189       */
00190     INA219(PinName p_sda, PinName p_scl, const INA219_TypeDef *ina219_parameter);
00191     INA219(PinName p_sda, PinName p_scl, uint8_t addr);
00192     INA219(PinName p_sda, PinName p_scl);
00193 
00194     /** Configure data pin (with other devices on I2C line)
00195       * @param I2C previous definition
00196       * @param parameter address chip (INA219_TypeDef)
00197       * @param or just set address or just port
00198       */
00199     INA219(I2C& p_i2c, const INA219_TypeDef *ina219_parameter);
00200     INA219(I2C& p_i2c, uint8_t addr);
00201     INA219(I2C& p_i2c);
00202 
00203     /** Read Current data
00204       * @param none
00205       * @return current [mA]
00206       */
00207     float read_current(void);
00208     float read_current_by_shuntvolt(void);
00209 
00210     /** Read Power data
00211       * @param none
00212       * @return power [w]
00213       */
00214     float read_power(void);
00215 
00216     /** Read Bus voltage
00217       * @param none
00218       * @return voltage [v]
00219       */
00220     float read_bus_voltage(void);
00221 
00222     /** Read Shunt voltage data
00223       * @param none
00224       * @return voltage [v]
00225       */
00226     float read_shunt_voltage(void);
00227 
00228     /** Read configration reg.
00229       * @param none
00230       * @return configrartion register value
00231       */
00232     uint16_t read_config(void);
00233 
00234     /** Set configration reg.
00235       * @param
00236       * @return configrartion register value
00237       */
00238     uint16_t set_config(uint16_t cfg);
00239 
00240     /** Read calibration reg.
00241       * @param none
00242       * @return calibration register value
00243       */
00244     uint16_t read_calb(void);
00245 
00246     /** Set calibration reg.
00247       * @param
00248       * @return calibration register value
00249       */
00250     uint16_t set_calb(uint16_t clb);
00251 
00252     /** Set I2C clock frequency
00253       * @param freq.
00254       * @return none
00255       */
00256     void frequency(int hz);
00257 
00258     /** Read register (general purpose)
00259       * @param register's address
00260       * @return register data
00261       */
00262     uint8_t read_reg(uint8_t addr);
00263 
00264     /** Write register (general purpose)
00265       * @param register's address
00266       * @param data
00267       * @return register data
00268       */
00269     uint8_t write_reg(uint8_t addr, uint8_t data);
00270 
00271 protected:
00272     I2C *_i2c_p;
00273     I2C &_i2c;
00274 
00275     void initialize(void);
00276 
00277 private:
00278     INA219_TypeDef ina219_set_data;
00279     int32_t scale_factor;
00280     uint8_t dt[4];
00281 
00282 };
00283 
00284 //-------------------------------------------------------------------------------------------------
00285 // Following parts are only internal use and you only use it if you manage an internal config reg.
00286 //-------------------------------------------------------------------------------------------------
00287 /////////// BIT DEFINITION ////////////////////////////////
00288 #define INA219_CFG_RESET           (1UL << 15)
00289 
00290 #define INA219_CFG_B32V            (1UL << 13)
00291 #define INA219_CFG_B16V            (0UL << 13)
00292 
00293 #define INA219_CFG_PGA_DIV_1       (0UL << 11)
00294 #define INA219_CFG_PGA_DIV_2       (1UL << 11)
00295 #define INA219_CFG_PGA_DIV_4       (2UL << 11)
00296 #define INA219_CFG_PGA_DIV_8       (3UL << 11)
00297 
00298 #define INA219_CFG_BADC            (0xf << 7)
00299 
00300 #define INA219_CFG_SADC_9B_84U     (0x0 << 3)
00301 #define INA219_CFG_SADC_10B_148U   (0x1 << 3)
00302 #define INA219_CFG_SADC_11B_276U   (0x2 << 3)
00303 #define INA219_CFG_SADC_12B_532U   (0x3 << 3)
00304 #define INA219_CFG_SADC_2S_1R06M   (0x9 << 3)
00305 #define INA219_CFG_SADC_4S_2R13M   (0xa << 3)
00306 #define INA219_CFG_SADC_8S_4R26M   (0xb << 3)
00307 #define INA219_CFG_SADC_16S_8R51M  (0xc << 3)
00308 #define INA219_CFG_SADC_32S_17M    (0xd << 3)
00309 #define INA219_CFG_SADC_64S_34M    (0xe << 3)
00310 #define INA219_CFG_SADC_128S_68M   (0xf << 3)
00311 
00312 #define INA219_CFG_MODE_PDWN       (0UL << 0)
00313 #define INA219_CFG_MODE_SHNTTRG    (1UL << 0)
00314 #define INA219_CFG_MODE_BUSTRG     (2UL << 0)
00315 #define INA219_CFG_MODE_SBTRG      (3UL << 0)
00316 #define INA219_CFG_MODE_ADC_OFF    (4UL << 0)
00317 #define INA219_CFG_MODE_SHNT_CONT  (5UL << 0)
00318 #define INA219_CFG_MODE_BUS_CONT   (6UL << 0)
00319 #define INA219_CFG_MODE_SB_CONT    (7UL << 0)
00320 
00321 #endif  //  MBED_INA219
00322