INA219 (Texas Instruments) High-Side Measurement,Bi-Directional CURRENT/POWER MONITOR with I2C Interface
INA219.h@2:088b8d5de7e9, 2015-05-05 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue May 05 07:47:02 2015 +0000
- Revision:
- 2:088b8d5de7e9
- Parent:
- 1:fea983b0385b
- Child:
- 3:8c444591d7f2
updated parameter structure and functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:a3b41ab8433b | 1 | /* |
kenjiArai | 0:a3b41ab8433b | 2 | * mbed library program |
kenjiArai | 1:fea983b0385b | 3 | * INA219 High-Side Measurement,Bi-Directional CURRENT/POWER MONITOR with I2C Interface |
kenjiArai | 0:a3b41ab8433b | 4 | * by Texas Instruments |
kenjiArai | 0:a3b41ab8433b | 5 | * |
kenjiArai | 0:a3b41ab8433b | 6 | * Copyright (c) 2015 Kenji Arai / JH1PJL |
kenjiArai | 0:a3b41ab8433b | 7 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 0:a3b41ab8433b | 8 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 0:a3b41ab8433b | 9 | * Created: January 25th, 2015 |
kenjiArai | 2:088b8d5de7e9 | 10 | * Revised: May 5th, 2015 |
kenjiArai | 0:a3b41ab8433b | 11 | * |
kenjiArai | 0:a3b41ab8433b | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
kenjiArai | 0:a3b41ab8433b | 13 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
kenjiArai | 0:a3b41ab8433b | 14 | * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
kenjiArai | 0:a3b41ab8433b | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 0:a3b41ab8433b | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
kenjiArai | 0:a3b41ab8433b | 17 | */ |
kenjiArai | 0:a3b41ab8433b | 18 | /* |
kenjiArai | 0:a3b41ab8433b | 19 | *---------------- REFERENCE ---------------------------------------------------------------------- |
kenjiArai | 0:a3b41ab8433b | 20 | * Original Information |
kenjiArai | 0:a3b41ab8433b | 21 | * http://www.ti.com/product/INA219/description |
kenjiArai | 0:a3b41ab8433b | 22 | * http://www.ti.com/lit/ds/sbos448f/sbos448f.pdf |
kenjiArai | 0:a3b41ab8433b | 23 | * Device kit |
kenjiArai | 0:a3b41ab8433b | 24 | * https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/overview |
kenjiArai | 0:a3b41ab8433b | 25 | * http://akizukidenshi.com/catalog/g/gM-08221/ |
kenjiArai | 0:a3b41ab8433b | 26 | */ |
kenjiArai | 0:a3b41ab8433b | 27 | |
kenjiArai | 0:a3b41ab8433b | 28 | #ifndef MBED_INA219 |
kenjiArai | 0:a3b41ab8433b | 29 | #define MBED_INA219 |
kenjiArai | 0:a3b41ab8433b | 30 | |
kenjiArai | 0:a3b41ab8433b | 31 | /////////// ADDRESS ///////////////////////////// |
kenjiArai | 0:a3b41ab8433b | 32 | // 7bit address = 0b1000000(0x40) |
kenjiArai | 0:a3b41ab8433b | 33 | // G=GND, V=VS+, A=SDA, L=SCL |
kenjiArai | 1:fea983b0385b | 34 | // e.g. _VG: A1=VS+, A0=GND |
kenjiArai | 0:a3b41ab8433b | 35 | // -> Please make sure your H/W configuration |
kenjiArai | 0:a3b41ab8433b | 36 | // Set data into "addr" |
kenjiArai | 0:a3b41ab8433b | 37 | #define INA219_ADDR_GG (0x40 << 1) |
kenjiArai | 0:a3b41ab8433b | 38 | #define INA219_ADDR_GV (0x41 << 1) |
kenjiArai | 0:a3b41ab8433b | 39 | #define INA219_ADDR_GA (0x42 << 1) |
kenjiArai | 0:a3b41ab8433b | 40 | #define INA219_ADDR_GL (0x43 << 1) |
kenjiArai | 0:a3b41ab8433b | 41 | #define INA219_ADDR_VG (0x44 << 1) |
kenjiArai | 0:a3b41ab8433b | 42 | #define INA219_ADDR_VV (0x45 << 1) |
kenjiArai | 0:a3b41ab8433b | 43 | #define INA219_ADDR_VA (0x46 << 1) |
kenjiArai | 0:a3b41ab8433b | 44 | #define INA219_ADDR_VL (0x47 << 1) |
kenjiArai | 0:a3b41ab8433b | 45 | #define INA219_ADDR_AG (0x48 << 1) |
kenjiArai | 0:a3b41ab8433b | 46 | #define INA219_ADDR_AV (0x49 << 1) |
kenjiArai | 0:a3b41ab8433b | 47 | #define INA219_ADDR_AA (0x4a << 1) |
kenjiArai | 0:a3b41ab8433b | 48 | #define INA219_ADDR_AL (0x4b << 1) |
kenjiArai | 0:a3b41ab8433b | 49 | #define INA219_ADDR_LG (0x4c << 1) |
kenjiArai | 0:a3b41ab8433b | 50 | #define INA219_ADDR_LV (0x4d << 1) |
kenjiArai | 0:a3b41ab8433b | 51 | #define INA219_ADDR_LA (0x4e << 1) |
kenjiArai | 0:a3b41ab8433b | 52 | #define INA219_ADDR_LL (0x4f << 1) |
kenjiArai | 0:a3b41ab8433b | 53 | |
kenjiArai | 0:a3b41ab8433b | 54 | /////////// REGISTER DEFINITION ///////////////// |
kenjiArai | 0:a3b41ab8433b | 55 | #define INA219_CONFIG 0x00 |
kenjiArai | 0:a3b41ab8433b | 56 | #define INA219_SHUNT_V 0x01 |
kenjiArai | 0:a3b41ab8433b | 57 | #define INA219_BUS_VOLT 0x02 |
kenjiArai | 0:a3b41ab8433b | 58 | #define INA219_POWER 0x03 |
kenjiArai | 0:a3b41ab8433b | 59 | #define INA219_CURRENT 0x04 |
kenjiArai | 0:a3b41ab8433b | 60 | #define INA219_CALBLATION 0x05 |
kenjiArai | 0:a3b41ab8433b | 61 | |
kenjiArai | 0:a3b41ab8433b | 62 | /////////// PARAMETER SETTING /////////////////// |
kenjiArai | 0:a3b41ab8433b | 63 | // Set data into "shunt_register" |
kenjiArai | 0:a3b41ab8433b | 64 | #define INA219_PAR_R_100MOHM 100 |
kenjiArai | 0:a3b41ab8433b | 65 | #define INA219_PAR_R_200MORM 200 |
kenjiArai | 0:a3b41ab8433b | 66 | #define INA219_PAR_R_MORM(x) (x) |
kenjiArai | 0:a3b41ab8433b | 67 | // Set data into "v_max" |
kenjiArai | 0:a3b41ab8433b | 68 | #define INA219_PAR_V_16V 0 |
kenjiArai | 2:088b8d5de7e9 | 69 | #define INA219_PAR_V_32V 1 // Default |
kenjiArai | 0:a3b41ab8433b | 70 | // Set data into "gain" |
kenjiArai | 0:a3b41ab8433b | 71 | #define INA219_PAR_G_40MV 0 // 400[mA] max if R=0.1[Ohm] |
kenjiArai | 0:a3b41ab8433b | 72 | #define INA219_PAR_G_80MV 1 |
kenjiArai | 0:a3b41ab8433b | 73 | #define INA219_PAR_G_160MV 2 |
kenjiArai | 2:088b8d5de7e9 | 74 | #define INA219_PAR_G_320MV 3 // Default |
kenjiArai | 2:088b8d5de7e9 | 75 | // Set data into "bus_adc_resolution" |
kenjiArai | 2:088b8d5de7e9 | 76 | #define INA219_PAR_B_9B_X1_84US 0x0 |
kenjiArai | 2:088b8d5de7e9 | 77 | #define INA219_PAR_B_10B_X1_148US 0x1 |
kenjiArai | 2:088b8d5de7e9 | 78 | #define INA219_PAR_B_11B_X1_276US 0x2 |
kenjiArai | 2:088b8d5de7e9 | 79 | #define INA219_PAR_B_12B_X1_532US 0x3 // Default |
kenjiArai | 2:088b8d5de7e9 | 80 | #define INA219_PAR_B_12B_X2_1R06MS 0x9 |
kenjiArai | 2:088b8d5de7e9 | 81 | #define INA219_PAR_B_12B_X4_2R13MS 0xa |
kenjiArai | 2:088b8d5de7e9 | 82 | #define INA219_PAR_B_12B_X8_4R26MS 0xb |
kenjiArai | 2:088b8d5de7e9 | 83 | #define INA219_PAR_B_12B_X16_8MS 0xc |
kenjiArai | 2:088b8d5de7e9 | 84 | #define INA219_PAR_B_12B_X32_17MS 0xd |
kenjiArai | 2:088b8d5de7e9 | 85 | #define INA219_PAR_B_12B_X64_34MS 0xe |
kenjiArai | 2:088b8d5de7e9 | 86 | #define INA219_PAR_B_12B_X128_68MS 0xf |
kenjiArai | 2:088b8d5de7e9 | 87 | // Set data into "shunt_adc_resolution" |
kenjiArai | 0:a3b41ab8433b | 88 | #define INA219_PAR_S_9B_X1_84US 0x0 |
kenjiArai | 0:a3b41ab8433b | 89 | #define INA219_PAR_S_10B_X1_148US 0x1 |
kenjiArai | 0:a3b41ab8433b | 90 | #define INA219_PAR_S_11B_X1_276US 0x2 |
kenjiArai | 2:088b8d5de7e9 | 91 | #define INA219_PAR_S_12B_X1_532US 0x3 // Default |
kenjiArai | 0:a3b41ab8433b | 92 | #define INA219_PAR_S_12B_X2_1R06MS 0x9 |
kenjiArai | 0:a3b41ab8433b | 93 | #define INA219_PAR_S_12B_X4_2R13MS 0xa |
kenjiArai | 0:a3b41ab8433b | 94 | #define INA219_PAR_S_12B_X8_4R26MS 0xb |
kenjiArai | 0:a3b41ab8433b | 95 | #define INA219_PAR_S_12B_X16_8MS 0xc |
kenjiArai | 0:a3b41ab8433b | 96 | #define INA219_PAR_S_12B_X32_17MS 0xd |
kenjiArai | 0:a3b41ab8433b | 97 | #define INA219_PAR_S_12B_X64_34MS 0xe |
kenjiArai | 0:a3b41ab8433b | 98 | #define INA219_PAR_S_12B_X128_68MS 0xf |
kenjiArai | 0:a3b41ab8433b | 99 | // Set data into "mode" |
kenjiArai | 0:a3b41ab8433b | 100 | #define INA219_PAR_M_PDWN 0 |
kenjiArai | 0:a3b41ab8433b | 101 | #define INA219_PAR_M_SHNT_TRG 1 |
kenjiArai | 0:a3b41ab8433b | 102 | #define INA219_PAR_M_BUS_TRG 2 |
kenjiArai | 0:a3b41ab8433b | 103 | #define INA219_PAR_M_SHNTBUS_TRG 3 |
kenjiArai | 0:a3b41ab8433b | 104 | #define INA219_PAR_M_ADC_OFF 4 |
kenjiArai | 0:a3b41ab8433b | 105 | #define INA219_PAR_M_SHNT_CONT 5 |
kenjiArai | 0:a3b41ab8433b | 106 | #define INA219_PAR_M_BUS_CONT 6 |
kenjiArai | 2:088b8d5de7e9 | 107 | #define INA219_PAR_M_SHNTBUS_CONT 7 // Default |
kenjiArai | 0:a3b41ab8433b | 108 | |
kenjiArai | 0:a3b41ab8433b | 109 | ////////////// DATA TYPE DEFINITION /////////////////////// |
kenjiArai | 0:a3b41ab8433b | 110 | typedef struct { |
kenjiArai | 2:088b8d5de7e9 | 111 | // I2C Address |
kenjiArai | 0:a3b41ab8433b | 112 | uint8_t addr; |
kenjiArai | 2:088b8d5de7e9 | 113 | // CONFIG REG |
kenjiArai | 0:a3b41ab8433b | 114 | uint8_t shunt_register; |
kenjiArai | 0:a3b41ab8433b | 115 | uint8_t v_max; |
kenjiArai | 0:a3b41ab8433b | 116 | uint8_t gain; |
kenjiArai | 2:088b8d5de7e9 | 117 | uint8_t bus_adc_resolution; |
kenjiArai | 2:088b8d5de7e9 | 118 | uint8_t Shunt_adc_resolution; |
kenjiArai | 0:a3b41ab8433b | 119 | uint8_t mode; |
kenjiArai | 2:088b8d5de7e9 | 120 | // CALBLATION REG |
kenjiArai | 0:a3b41ab8433b | 121 | uint16_t calibration_data; |
kenjiArai | 0:a3b41ab8433b | 122 | } INA219_TypeDef; |
kenjiArai | 0:a3b41ab8433b | 123 | |
kenjiArai | 0:a3b41ab8433b | 124 | ////////////// DEFAULT SETTING //////////////////////////// |
kenjiArai | 0:a3b41ab8433b | 125 | // Standard parameter for easy set-up |
kenjiArai | 0:a3b41ab8433b | 126 | const INA219_TypeDef ina219_std_paramtr = { |
kenjiArai | 2:088b8d5de7e9 | 127 | // I2C Address |
kenjiArai | 2:088b8d5de7e9 | 128 | INA219_ADDR_VV, |
kenjiArai | 2:088b8d5de7e9 | 129 | // CONFIG REG |
kenjiArai | 2:088b8d5de7e9 | 130 | INA219_PAR_R_100MOHM, // 100 milli-ohm |
kenjiArai | 2:088b8d5de7e9 | 131 | INA219_PAR_V_16V, // 16V max |
kenjiArai | 2:088b8d5de7e9 | 132 | INA219_PAR_G_40MV, // Gain x1 (40mV -> 400mA max with 100 milliOhm) |
kenjiArai | 2:088b8d5de7e9 | 133 | INA219_PAR_B_12B_X1_532US, // Bus/resolution 12bit & one time convertion |
kenjiArai | 2:088b8d5de7e9 | 134 | INA219_PAR_S_12B_X1_532US, // Shunt/resolution 12bit & one time convertion |
kenjiArai | 0:a3b41ab8433b | 135 | INA219_PAR_M_SHNTBUS_CONT, // Measure continuously both Shunt voltage and Bus voltage |
kenjiArai | 2:088b8d5de7e9 | 136 | // CALBLATION REG |
kenjiArai | 2:088b8d5de7e9 | 137 | 16384 // Calibration data |
kenjiArai | 0:a3b41ab8433b | 138 | }; |
kenjiArai | 0:a3b41ab8433b | 139 | |
kenjiArai | 0:a3b41ab8433b | 140 | /** INA219 High-Side Measurement,Bi-Directional CURRENT/POWER MONITOR with I2C Interface |
kenjiArai | 0:a3b41ab8433b | 141 | * |
kenjiArai | 0:a3b41ab8433b | 142 | * @code |
kenjiArai | 0:a3b41ab8433b | 143 | * //--------- Default setting ----------------- |
kenjiArai | 0:a3b41ab8433b | 144 | * #include "mbed.h" |
kenjiArai | 0:a3b41ab8433b | 145 | * #include "INA219.h" |
kenjiArai | 0:a3b41ab8433b | 146 | * |
kenjiArai | 0:a3b41ab8433b | 147 | * // I2C Communication |
kenjiArai | 0:a3b41ab8433b | 148 | * INA219 current(dp5, dp27, INA219_ADDR_GG); |
kenjiArai | 0:a3b41ab8433b | 149 | * // If you connected I2C line not only this device but also other devices, |
kenjiArai | 0:a3b41ab8433b | 150 | * // you need to declare following method. |
kenjiArai | 0:a3b41ab8433b | 151 | * I2C i2c(dp5, dp27); |
kenjiArai | 0:a3b41ab8433b | 152 | * INA219 current(I2C& p_i2c, INA219_ADDR_GG); |
kenjiArai | 0:a3b41ab8433b | 153 | * |
kenjiArai | 0:a3b41ab8433b | 154 | * int main() { |
kenjiArai | 0:a3b41ab8433b | 155 | * while(1){ |
kenjiArai | 0:a3b41ab8433b | 156 | * printf("I=%+6.3f [mA]\r\n", current.read_current()); |
kenjiArai | 0:a3b41ab8433b | 157 | * wait(1.0): |
kenjiArai | 0:a3b41ab8433b | 158 | * } |
kenjiArai | 0:a3b41ab8433b | 159 | * } |
kenjiArai | 0:a3b41ab8433b | 160 | * //--------- Detail setting ----------------- |
kenjiArai | 0:a3b41ab8433b | 161 | * #include "mbed.h" |
kenjiArai | 1:fea983b0385b | 162 | * #include "INA219.h" |
kenjiArai | 0:a3b41ab8433b | 163 | * |
kenjiArai | 0:a3b41ab8433b | 164 | * const INA219_TypeDef ina219_my_paramtr = { |
kenjiArai | 2:088b8d5de7e9 | 165 | * // I2C Address |
kenjiArai | 2:088b8d5de7e9 | 166 | * INA219_ADDR_GG, |
kenjiArai | 2:088b8d5de7e9 | 167 | * // CONFIG Reg. |
kenjiArai | 2:088b8d5de7e9 | 168 | * INA219_PAR_R_100MOHM, // 100 milli-ohm |
kenjiArai | 2:088b8d5de7e9 | 169 | * INA219_CFG_B16V, // 16V max |
kenjiArai | 2:088b8d5de7e9 | 170 | * INA219_PAR_G_40MV, // Gain x1 |
kenjiArai | 2:088b8d5de7e9 | 171 | * INA219_PAR_M_SHNTBUS_CONT,// Measure continuously |
kenjiArai | 2:088b8d5de7e9 | 172 | * // CALIB |
kenjiArai | 2:088b8d5de7e9 | 173 | * 16384 // Calibration data is nothing |
kenjiArai | 0:a3b41ab8433b | 174 | * }; |
kenjiArai | 0:a3b41ab8433b | 175 | * |
kenjiArai | 0:a3b41ab8433b | 176 | * I2C i2c(dp5,dp27); |
kenjiArai | 0:a3b41ab8433b | 177 | * INA219 current(I2C& p_i2c, &ina219_my_paramtr); |
kenjiArai | 0:a3b41ab8433b | 178 | * |
kenjiArai | 0:a3b41ab8433b | 179 | * int main() { |
kenjiArai | 0:a3b41ab8433b | 180 | * while(1){ |
kenjiArai | 0:a3b41ab8433b | 181 | * printf("I=%+6.3f [mA]\r\n", current.read_current()); |
kenjiArai | 0:a3b41ab8433b | 182 | * wait(1.0): |
kenjiArai | 0:a3b41ab8433b | 183 | * } |
kenjiArai | 0:a3b41ab8433b | 184 | * } |
kenjiArai | 0:a3b41ab8433b | 185 | * @endcode |
kenjiArai | 0:a3b41ab8433b | 186 | */ |
kenjiArai | 0:a3b41ab8433b | 187 | |
kenjiArai | 0:a3b41ab8433b | 188 | class INA219 |
kenjiArai | 0:a3b41ab8433b | 189 | { |
kenjiArai | 0:a3b41ab8433b | 190 | public: |
kenjiArai | 0:a3b41ab8433b | 191 | /** Configure data pin |
kenjiArai | 0:a3b41ab8433b | 192 | * @param data SDA and SCL pins |
kenjiArai | 0:a3b41ab8433b | 193 | * @param parameter address chip (INA219_TypeDef) |
kenjiArai | 0:a3b41ab8433b | 194 | * @param or just set address or just port |
kenjiArai | 0:a3b41ab8433b | 195 | */ |
kenjiArai | 0:a3b41ab8433b | 196 | INA219(PinName p_sda, PinName p_scl, const INA219_TypeDef *ina219_parameter); |
kenjiArai | 0:a3b41ab8433b | 197 | INA219(PinName p_sda, PinName p_scl, uint8_t addr); |
kenjiArai | 0:a3b41ab8433b | 198 | INA219(PinName p_sda, PinName p_scl); |
kenjiArai | 0:a3b41ab8433b | 199 | |
kenjiArai | 0:a3b41ab8433b | 200 | /** Configure data pin (with other devices on I2C line) |
kenjiArai | 0:a3b41ab8433b | 201 | * @param I2C previous definition |
kenjiArai | 0:a3b41ab8433b | 202 | * @param parameter address chip (INA219_TypeDef) |
kenjiArai | 0:a3b41ab8433b | 203 | * @param or just set address or just port |
kenjiArai | 0:a3b41ab8433b | 204 | */ |
kenjiArai | 0:a3b41ab8433b | 205 | INA219(I2C& p_i2c, const INA219_TypeDef *ina219_parameter); |
kenjiArai | 0:a3b41ab8433b | 206 | INA219(I2C& p_i2c, uint8_t addr); |
kenjiArai | 0:a3b41ab8433b | 207 | INA219(I2C& p_i2c); |
kenjiArai | 0:a3b41ab8433b | 208 | |
kenjiArai | 0:a3b41ab8433b | 209 | /** Read Current data |
kenjiArai | 0:a3b41ab8433b | 210 | * @param none |
kenjiArai | 0:a3b41ab8433b | 211 | * @return current [mA] |
kenjiArai | 0:a3b41ab8433b | 212 | */ |
kenjiArai | 0:a3b41ab8433b | 213 | float read_current(void); |
kenjiArai | 0:a3b41ab8433b | 214 | float read_current_by_shuntvolt(void); |
kenjiArai | 0:a3b41ab8433b | 215 | |
kenjiArai | 0:a3b41ab8433b | 216 | /** Read Power data |
kenjiArai | 0:a3b41ab8433b | 217 | * @param none |
kenjiArai | 0:a3b41ab8433b | 218 | * @return power [w] |
kenjiArai | 0:a3b41ab8433b | 219 | */ |
kenjiArai | 0:a3b41ab8433b | 220 | float read_power(void); |
kenjiArai | 0:a3b41ab8433b | 221 | |
kenjiArai | 0:a3b41ab8433b | 222 | /** Read Bus voltage |
kenjiArai | 0:a3b41ab8433b | 223 | * @param none |
kenjiArai | 0:a3b41ab8433b | 224 | * @return voltage [v] |
kenjiArai | 0:a3b41ab8433b | 225 | */ |
kenjiArai | 0:a3b41ab8433b | 226 | float read_bus_voltage(void); |
kenjiArai | 0:a3b41ab8433b | 227 | |
kenjiArai | 0:a3b41ab8433b | 228 | /** Read Shunt voltage data |
kenjiArai | 0:a3b41ab8433b | 229 | * @param none |
kenjiArai | 0:a3b41ab8433b | 230 | * @return voltage [v] |
kenjiArai | 0:a3b41ab8433b | 231 | */ |
kenjiArai | 0:a3b41ab8433b | 232 | float read_shunt_voltage(void); |
kenjiArai | 0:a3b41ab8433b | 233 | |
kenjiArai | 0:a3b41ab8433b | 234 | /** Read configration reg. |
kenjiArai | 0:a3b41ab8433b | 235 | * @param none |
kenjiArai | 0:a3b41ab8433b | 236 | * @return configrartion register value |
kenjiArai | 0:a3b41ab8433b | 237 | */ |
kenjiArai | 0:a3b41ab8433b | 238 | uint16_t read_config(void); |
kenjiArai | 0:a3b41ab8433b | 239 | |
kenjiArai | 0:a3b41ab8433b | 240 | /** Set configration reg. |
kenjiArai | 0:a3b41ab8433b | 241 | * @param |
kenjiArai | 0:a3b41ab8433b | 242 | * @return configrartion register value |
kenjiArai | 0:a3b41ab8433b | 243 | */ |
kenjiArai | 0:a3b41ab8433b | 244 | uint16_t set_config(uint16_t cfg); |
kenjiArai | 0:a3b41ab8433b | 245 | |
kenjiArai | 0:a3b41ab8433b | 246 | /** Read calibration reg. |
kenjiArai | 0:a3b41ab8433b | 247 | * @param none |
kenjiArai | 0:a3b41ab8433b | 248 | * @return calibration register value |
kenjiArai | 0:a3b41ab8433b | 249 | */ |
kenjiArai | 0:a3b41ab8433b | 250 | uint16_t read_calb(void); |
kenjiArai | 0:a3b41ab8433b | 251 | |
kenjiArai | 0:a3b41ab8433b | 252 | /** Set calibration reg. |
kenjiArai | 0:a3b41ab8433b | 253 | * @param |
kenjiArai | 0:a3b41ab8433b | 254 | * @return calibration register value |
kenjiArai | 0:a3b41ab8433b | 255 | */ |
kenjiArai | 0:a3b41ab8433b | 256 | uint16_t set_calb(uint16_t clb); |
kenjiArai | 0:a3b41ab8433b | 257 | |
kenjiArai | 0:a3b41ab8433b | 258 | /** Set I2C clock frequency |
kenjiArai | 0:a3b41ab8433b | 259 | * @param freq. |
kenjiArai | 0:a3b41ab8433b | 260 | * @return none |
kenjiArai | 0:a3b41ab8433b | 261 | */ |
kenjiArai | 0:a3b41ab8433b | 262 | void frequency(int hz); |
kenjiArai | 0:a3b41ab8433b | 263 | |
kenjiArai | 0:a3b41ab8433b | 264 | /** Read register (general purpose) |
kenjiArai | 0:a3b41ab8433b | 265 | * @param register's address |
kenjiArai | 0:a3b41ab8433b | 266 | * @return register data |
kenjiArai | 0:a3b41ab8433b | 267 | */ |
kenjiArai | 0:a3b41ab8433b | 268 | uint8_t read_reg(uint8_t addr); |
kenjiArai | 0:a3b41ab8433b | 269 | |
kenjiArai | 0:a3b41ab8433b | 270 | /** Write register (general purpose) |
kenjiArai | 0:a3b41ab8433b | 271 | * @param register's address |
kenjiArai | 0:a3b41ab8433b | 272 | * @param data |
kenjiArai | 0:a3b41ab8433b | 273 | * @return register data |
kenjiArai | 0:a3b41ab8433b | 274 | */ |
kenjiArai | 0:a3b41ab8433b | 275 | uint8_t write_reg(uint8_t addr, uint8_t data); |
kenjiArai | 0:a3b41ab8433b | 276 | |
kenjiArai | 0:a3b41ab8433b | 277 | protected: |
kenjiArai | 0:a3b41ab8433b | 278 | I2C _i2c; |
kenjiArai | 0:a3b41ab8433b | 279 | |
kenjiArai | 0:a3b41ab8433b | 280 | void initialize(void); |
kenjiArai | 0:a3b41ab8433b | 281 | |
kenjiArai | 0:a3b41ab8433b | 282 | private: |
kenjiArai | 0:a3b41ab8433b | 283 | INA219_TypeDef ina219_set_data; |
kenjiArai | 0:a3b41ab8433b | 284 | int32_t scale_factor; |
kenjiArai | 0:a3b41ab8433b | 285 | uint8_t dt[4]; |
kenjiArai | 0:a3b41ab8433b | 286 | |
kenjiArai | 0:a3b41ab8433b | 287 | }; |
kenjiArai | 0:a3b41ab8433b | 288 | |
kenjiArai | 1:fea983b0385b | 289 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 2:088b8d5de7e9 | 290 | // Following parts are only internal use and you only use it if you manage an internal config reg. |
kenjiArai | 1:fea983b0385b | 291 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 1:fea983b0385b | 292 | /////////// BIT DEFINITION //////////////////////////////// |
kenjiArai | 1:fea983b0385b | 293 | #define INA219_CFG_RESET (1UL << 15) |
kenjiArai | 1:fea983b0385b | 294 | |
kenjiArai | 1:fea983b0385b | 295 | #define INA219_CFG_B32V (1UL << 13) |
kenjiArai | 1:fea983b0385b | 296 | #define INA219_CFG_B16V (0UL << 13) |
kenjiArai | 1:fea983b0385b | 297 | |
kenjiArai | 1:fea983b0385b | 298 | #define INA219_CFG_PGA_DIV_1 (0UL << 11) |
kenjiArai | 1:fea983b0385b | 299 | #define INA219_CFG_PGA_DIV_2 (1UL << 11) |
kenjiArai | 1:fea983b0385b | 300 | #define INA219_CFG_PGA_DIV_4 (2UL << 11) |
kenjiArai | 1:fea983b0385b | 301 | #define INA219_CFG_PGA_DIV_8 (3UL << 11) |
kenjiArai | 1:fea983b0385b | 302 | |
kenjiArai | 1:fea983b0385b | 303 | #define INA219_CFG_BADC (0xf << 7) |
kenjiArai | 1:fea983b0385b | 304 | |
kenjiArai | 1:fea983b0385b | 305 | #define INA219_CFG_SADC_9B_84U (0x0 << 3) |
kenjiArai | 1:fea983b0385b | 306 | #define INA219_CFG_SADC_10B_148U (0x1 << 3) |
kenjiArai | 1:fea983b0385b | 307 | #define INA219_CFG_SADC_11B_276U (0x2 << 3) |
kenjiArai | 1:fea983b0385b | 308 | #define INA219_CFG_SADC_12B_532U (0x3 << 3) |
kenjiArai | 1:fea983b0385b | 309 | #define INA219_CFG_SADC_2S_1R06M (0x9 << 3) |
kenjiArai | 1:fea983b0385b | 310 | #define INA219_CFG_SADC_4S_2R13M (0xa << 3) |
kenjiArai | 1:fea983b0385b | 311 | #define INA219_CFG_SADC_8S_4R26M (0xb << 3) |
kenjiArai | 1:fea983b0385b | 312 | #define INA219_CFG_SADC_16S_8R51M (0xc << 3) |
kenjiArai | 1:fea983b0385b | 313 | #define INA219_CFG_SADC_32S_17M (0xd << 3) |
kenjiArai | 1:fea983b0385b | 314 | #define INA219_CFG_SADC_64S_34M (0xe << 3) |
kenjiArai | 1:fea983b0385b | 315 | #define INA219_CFG_SADC_128S_68M (0xf << 3) |
kenjiArai | 1:fea983b0385b | 316 | |
kenjiArai | 1:fea983b0385b | 317 | #define INA219_CFG_MODE_PDWN (0UL << 0) |
kenjiArai | 1:fea983b0385b | 318 | #define INA219_CFG_MODE_SHNTTRG (1UL << 0) |
kenjiArai | 1:fea983b0385b | 319 | #define INA219_CFG_MODE_BUSTRG (2UL << 0) |
kenjiArai | 1:fea983b0385b | 320 | #define INA219_CFG_MODE_SBTRG (3UL << 0) |
kenjiArai | 1:fea983b0385b | 321 | #define INA219_CFG_MODE_ADC_OFF (4UL << 0) |
kenjiArai | 1:fea983b0385b | 322 | #define INA219_CFG_MODE_SHNT_CONT (5UL << 0) |
kenjiArai | 1:fea983b0385b | 323 | #define INA219_CFG_MODE_BUS_CONT (6UL << 0) |
kenjiArai | 1:fea983b0385b | 324 | #define INA219_CFG_MODE_SB_CONT (7UL << 0) |
kenjiArai | 1:fea983b0385b | 325 | |
kenjiArai | 0:a3b41ab8433b | 326 | #endif // MBED_INA219 |