Texas Instruments INA230 hi/lo side, bi-directional current and voltage monitor with I2C. Code hacked and enhanced from the INA219 driver from TI
Fork of INA219 by
INA219.h@1:fea983b0385b, 2015-04-20 (annotated)
- Committer:
- kenjiArai
- Date:
- Mon Apr 20 12:00:02 2015 +0000
- Revision:
- 1:fea983b0385b
- Parent:
- 0:a3b41ab8433b
- Child:
- 2:088b8d5de7e9
modified comments
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 | 1:fea983b0385b | 10 | * Revised: March 22nd, 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 | 0:a3b41ab8433b | 69 | #define INA219_PAR_V_32V 1 |
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 | 0:a3b41ab8433b | 74 | #define INA219_PAR_G_320MV 3 |
kenjiArai | 0:a3b41ab8433b | 75 | // Set data into "adc_resolution" |
kenjiArai | 0:a3b41ab8433b | 76 | #define INA219_PAR_S_9B_X1_84US 0x0 |
kenjiArai | 0:a3b41ab8433b | 77 | #define INA219_PAR_S_10B_X1_148US 0x1 |
kenjiArai | 0:a3b41ab8433b | 78 | #define INA219_PAR_S_11B_X1_276US 0x2 |
kenjiArai | 0:a3b41ab8433b | 79 | #define INA219_PAR_S_12B_X1_5328US 0x3 |
kenjiArai | 0:a3b41ab8433b | 80 | #define INA219_PAR_S_12B_X2_1R06MS 0x9 |
kenjiArai | 0:a3b41ab8433b | 81 | #define INA219_PAR_S_12B_X4_2R13MS 0xa |
kenjiArai | 0:a3b41ab8433b | 82 | #define INA219_PAR_S_12B_X8_4R26MS 0xb |
kenjiArai | 0:a3b41ab8433b | 83 | #define INA219_PAR_S_12B_X16_8MS 0xc |
kenjiArai | 0:a3b41ab8433b | 84 | #define INA219_PAR_S_12B_X32_17MS 0xd |
kenjiArai | 0:a3b41ab8433b | 85 | #define INA219_PAR_S_12B_X64_34MS 0xe |
kenjiArai | 0:a3b41ab8433b | 86 | #define INA219_PAR_S_12B_X128_68MS 0xf |
kenjiArai | 0:a3b41ab8433b | 87 | // Set data into "mode" |
kenjiArai | 0:a3b41ab8433b | 88 | #define INA219_PAR_M_PDWN 0 |
kenjiArai | 0:a3b41ab8433b | 89 | #define INA219_PAR_M_SHNT_TRG 1 |
kenjiArai | 0:a3b41ab8433b | 90 | #define INA219_PAR_M_BUS_TRG 2 |
kenjiArai | 0:a3b41ab8433b | 91 | #define INA219_PAR_M_SHNTBUS_TRG 3 |
kenjiArai | 0:a3b41ab8433b | 92 | #define INA219_PAR_M_ADC_OFF 4 |
kenjiArai | 0:a3b41ab8433b | 93 | #define INA219_PAR_M_SHNT_CONT 5 |
kenjiArai | 0:a3b41ab8433b | 94 | #define INA219_PAR_M_BUS_CONT 6 |
kenjiArai | 0:a3b41ab8433b | 95 | #define INA219_PAR_M_SHNTBUS_CONT 7 |
kenjiArai | 0:a3b41ab8433b | 96 | |
kenjiArai | 0:a3b41ab8433b | 97 | ////////////// DATA TYPE DEFINITION /////////////////////// |
kenjiArai | 0:a3b41ab8433b | 98 | typedef struct { |
kenjiArai | 0:a3b41ab8433b | 99 | uint8_t addr; |
kenjiArai | 0:a3b41ab8433b | 100 | uint8_t shunt_register; |
kenjiArai | 0:a3b41ab8433b | 101 | uint8_t v_max; |
kenjiArai | 0:a3b41ab8433b | 102 | uint8_t gain; |
kenjiArai | 0:a3b41ab8433b | 103 | uint8_t adc_resolution; |
kenjiArai | 0:a3b41ab8433b | 104 | uint8_t mode; |
kenjiArai | 0:a3b41ab8433b | 105 | uint16_t calibration_data; |
kenjiArai | 0:a3b41ab8433b | 106 | } INA219_TypeDef; |
kenjiArai | 0:a3b41ab8433b | 107 | |
kenjiArai | 0:a3b41ab8433b | 108 | ////////////// DEFAULT SETTING //////////////////////////// |
kenjiArai | 0:a3b41ab8433b | 109 | // Standard parameter for easy set-up |
kenjiArai | 0:a3b41ab8433b | 110 | const INA219_TypeDef ina219_std_paramtr = { |
kenjiArai | 1:fea983b0385b | 111 | INA219_ADDR_VV, // I2C Address, Acc & Magn |
kenjiArai | 0:a3b41ab8433b | 112 | INA219_PAR_R_100MOHM, // 100 milli-ohm |
kenjiArai | 1:fea983b0385b | 113 | INA219_PAR_V_16V, // 16V max |
kenjiArai | 0:a3b41ab8433b | 114 | INA219_PAR_G_40MV, // Gain x1 (40mV -> 400mA max with 100 milliOhm) |
kenjiArai | 0:a3b41ab8433b | 115 | INA219_PAR_S_12B_X1_5328US, // resolution 12bit & one time convertion |
kenjiArai | 0:a3b41ab8433b | 116 | INA219_PAR_M_SHNTBUS_CONT, // Measure continuously both Shunt voltage and Bus voltage |
kenjiArai | 0:a3b41ab8433b | 117 | 16384 // Calibration data is nothing |
kenjiArai | 0:a3b41ab8433b | 118 | }; |
kenjiArai | 0:a3b41ab8433b | 119 | |
kenjiArai | 0:a3b41ab8433b | 120 | /** INA219 High-Side Measurement,Bi-Directional CURRENT/POWER MONITOR with I2C Interface |
kenjiArai | 0:a3b41ab8433b | 121 | * |
kenjiArai | 0:a3b41ab8433b | 122 | * @code |
kenjiArai | 0:a3b41ab8433b | 123 | * //--------- Default setting ----------------- |
kenjiArai | 0:a3b41ab8433b | 124 | * #include "mbed.h" |
kenjiArai | 0:a3b41ab8433b | 125 | * #include "INA219.h" |
kenjiArai | 0:a3b41ab8433b | 126 | * |
kenjiArai | 0:a3b41ab8433b | 127 | * // I2C Communication |
kenjiArai | 0:a3b41ab8433b | 128 | * INA219 current(dp5, dp27, INA219_ADDR_GG); |
kenjiArai | 0:a3b41ab8433b | 129 | * // If you connected I2C line not only this device but also other devices, |
kenjiArai | 0:a3b41ab8433b | 130 | * // you need to declare following method. |
kenjiArai | 0:a3b41ab8433b | 131 | * I2C i2c(dp5, dp27); |
kenjiArai | 0:a3b41ab8433b | 132 | * INA219 current(I2C& p_i2c, INA219_ADDR_GG); |
kenjiArai | 0:a3b41ab8433b | 133 | * |
kenjiArai | 0:a3b41ab8433b | 134 | * int main() { |
kenjiArai | 0:a3b41ab8433b | 135 | * while(1){ |
kenjiArai | 0:a3b41ab8433b | 136 | * printf("I=%+6.3f [mA]\r\n", current.read_current()); |
kenjiArai | 0:a3b41ab8433b | 137 | * wait(1.0): |
kenjiArai | 0:a3b41ab8433b | 138 | * } |
kenjiArai | 0:a3b41ab8433b | 139 | * } |
kenjiArai | 0:a3b41ab8433b | 140 | * //--------- Detail setting ----------------- |
kenjiArai | 0:a3b41ab8433b | 141 | * #include "mbed.h" |
kenjiArai | 1:fea983b0385b | 142 | * #include "INA219.h" |
kenjiArai | 0:a3b41ab8433b | 143 | * |
kenjiArai | 0:a3b41ab8433b | 144 | * const INA219_TypeDef ina219_my_paramtr = { |
kenjiArai | 0:a3b41ab8433b | 145 | * INA219_ADDR_GG, // I2C Address, Acc & Magn |
kenjiArai | 0:a3b41ab8433b | 146 | * INA219_PAR_R_100MOHM, // 100 milli-ohm |
kenjiArai | 0:a3b41ab8433b | 147 | * INA219_CFG_B16V, // 16V max |
kenjiArai | 0:a3b41ab8433b | 148 | * INA219_PAR_G_40MV, // Gain x1 |
kenjiArai | 0:a3b41ab8433b | 149 | * INA219_PAR_M_SHNTBUS_CONT, // Measure continuously |
kenjiArai | 1:fea983b0385b | 150 | * 16384 // Calibration data is nothing |
kenjiArai | 0:a3b41ab8433b | 151 | * }; |
kenjiArai | 0:a3b41ab8433b | 152 | * |
kenjiArai | 0:a3b41ab8433b | 153 | * I2C i2c(dp5,dp27); |
kenjiArai | 0:a3b41ab8433b | 154 | * INA219 current(I2C& p_i2c, &ina219_my_paramtr); |
kenjiArai | 0:a3b41ab8433b | 155 | * |
kenjiArai | 0:a3b41ab8433b | 156 | * int main() { |
kenjiArai | 0:a3b41ab8433b | 157 | * while(1){ |
kenjiArai | 0:a3b41ab8433b | 158 | * printf("I=%+6.3f [mA]\r\n", current.read_current()); |
kenjiArai | 0:a3b41ab8433b | 159 | * wait(1.0): |
kenjiArai | 0:a3b41ab8433b | 160 | * } |
kenjiArai | 0:a3b41ab8433b | 161 | * } |
kenjiArai | 0:a3b41ab8433b | 162 | * @endcode |
kenjiArai | 0:a3b41ab8433b | 163 | */ |
kenjiArai | 0:a3b41ab8433b | 164 | |
kenjiArai | 0:a3b41ab8433b | 165 | class INA219 |
kenjiArai | 0:a3b41ab8433b | 166 | { |
kenjiArai | 0:a3b41ab8433b | 167 | public: |
kenjiArai | 0:a3b41ab8433b | 168 | /** Configure data pin |
kenjiArai | 0:a3b41ab8433b | 169 | * @param data SDA and SCL pins |
kenjiArai | 0:a3b41ab8433b | 170 | * @param parameter address chip (INA219_TypeDef) |
kenjiArai | 0:a3b41ab8433b | 171 | * @param or just set address or just port |
kenjiArai | 0:a3b41ab8433b | 172 | */ |
kenjiArai | 0:a3b41ab8433b | 173 | INA219(PinName p_sda, PinName p_scl, const INA219_TypeDef *ina219_parameter); |
kenjiArai | 0:a3b41ab8433b | 174 | INA219(PinName p_sda, PinName p_scl, uint8_t addr); |
kenjiArai | 0:a3b41ab8433b | 175 | INA219(PinName p_sda, PinName p_scl); |
kenjiArai | 0:a3b41ab8433b | 176 | |
kenjiArai | 0:a3b41ab8433b | 177 | /** Configure data pin (with other devices on I2C line) |
kenjiArai | 0:a3b41ab8433b | 178 | * @param I2C previous definition |
kenjiArai | 0:a3b41ab8433b | 179 | * @param parameter address chip (INA219_TypeDef) |
kenjiArai | 0:a3b41ab8433b | 180 | * @param or just set address or just port |
kenjiArai | 0:a3b41ab8433b | 181 | */ |
kenjiArai | 0:a3b41ab8433b | 182 | INA219(I2C& p_i2c, const INA219_TypeDef *ina219_parameter); |
kenjiArai | 0:a3b41ab8433b | 183 | INA219(I2C& p_i2c, uint8_t addr); |
kenjiArai | 0:a3b41ab8433b | 184 | INA219(I2C& p_i2c); |
kenjiArai | 0:a3b41ab8433b | 185 | |
kenjiArai | 0:a3b41ab8433b | 186 | /** Read Current data |
kenjiArai | 0:a3b41ab8433b | 187 | * @param none |
kenjiArai | 0:a3b41ab8433b | 188 | * @return current [mA] |
kenjiArai | 0:a3b41ab8433b | 189 | */ |
kenjiArai | 0:a3b41ab8433b | 190 | float read_current(void); |
kenjiArai | 0:a3b41ab8433b | 191 | float read_current_by_shuntvolt(void); |
kenjiArai | 0:a3b41ab8433b | 192 | |
kenjiArai | 0:a3b41ab8433b | 193 | /** Read Power data |
kenjiArai | 0:a3b41ab8433b | 194 | * @param none |
kenjiArai | 0:a3b41ab8433b | 195 | * @return power [w] |
kenjiArai | 0:a3b41ab8433b | 196 | */ |
kenjiArai | 0:a3b41ab8433b | 197 | float read_power(void); |
kenjiArai | 0:a3b41ab8433b | 198 | |
kenjiArai | 0:a3b41ab8433b | 199 | /** Read Bus voltage |
kenjiArai | 0:a3b41ab8433b | 200 | * @param none |
kenjiArai | 0:a3b41ab8433b | 201 | * @return voltage [v] |
kenjiArai | 0:a3b41ab8433b | 202 | */ |
kenjiArai | 0:a3b41ab8433b | 203 | float read_bus_voltage(void); |
kenjiArai | 0:a3b41ab8433b | 204 | |
kenjiArai | 0:a3b41ab8433b | 205 | /** Read Shunt voltage data |
kenjiArai | 0:a3b41ab8433b | 206 | * @param none |
kenjiArai | 0:a3b41ab8433b | 207 | * @return voltage [v] |
kenjiArai | 0:a3b41ab8433b | 208 | */ |
kenjiArai | 0:a3b41ab8433b | 209 | float read_shunt_voltage(void); |
kenjiArai | 0:a3b41ab8433b | 210 | |
kenjiArai | 0:a3b41ab8433b | 211 | /** Read configration reg. |
kenjiArai | 0:a3b41ab8433b | 212 | * @param none |
kenjiArai | 0:a3b41ab8433b | 213 | * @return configrartion register value |
kenjiArai | 0:a3b41ab8433b | 214 | */ |
kenjiArai | 0:a3b41ab8433b | 215 | uint16_t read_config(void); |
kenjiArai | 0:a3b41ab8433b | 216 | |
kenjiArai | 0:a3b41ab8433b | 217 | /** Set configration reg. |
kenjiArai | 0:a3b41ab8433b | 218 | * @param |
kenjiArai | 0:a3b41ab8433b | 219 | * @return configrartion register value |
kenjiArai | 0:a3b41ab8433b | 220 | */ |
kenjiArai | 0:a3b41ab8433b | 221 | uint16_t set_config(uint16_t cfg); |
kenjiArai | 0:a3b41ab8433b | 222 | |
kenjiArai | 0:a3b41ab8433b | 223 | /** Read calibration reg. |
kenjiArai | 0:a3b41ab8433b | 224 | * @param none |
kenjiArai | 0:a3b41ab8433b | 225 | * @return calibration register value |
kenjiArai | 0:a3b41ab8433b | 226 | */ |
kenjiArai | 0:a3b41ab8433b | 227 | uint16_t read_calb(void); |
kenjiArai | 0:a3b41ab8433b | 228 | |
kenjiArai | 0:a3b41ab8433b | 229 | /** Set calibration reg. |
kenjiArai | 0:a3b41ab8433b | 230 | * @param |
kenjiArai | 0:a3b41ab8433b | 231 | * @return calibration register value |
kenjiArai | 0:a3b41ab8433b | 232 | */ |
kenjiArai | 0:a3b41ab8433b | 233 | uint16_t set_calb(uint16_t clb); |
kenjiArai | 0:a3b41ab8433b | 234 | |
kenjiArai | 0:a3b41ab8433b | 235 | /** Set I2C clock frequency |
kenjiArai | 0:a3b41ab8433b | 236 | * @param freq. |
kenjiArai | 0:a3b41ab8433b | 237 | * @return none |
kenjiArai | 0:a3b41ab8433b | 238 | */ |
kenjiArai | 0:a3b41ab8433b | 239 | void frequency(int hz); |
kenjiArai | 0:a3b41ab8433b | 240 | |
kenjiArai | 0:a3b41ab8433b | 241 | /** Read register (general purpose) |
kenjiArai | 0:a3b41ab8433b | 242 | * @param register's address |
kenjiArai | 0:a3b41ab8433b | 243 | * @return register data |
kenjiArai | 0:a3b41ab8433b | 244 | */ |
kenjiArai | 0:a3b41ab8433b | 245 | uint8_t read_reg(uint8_t addr); |
kenjiArai | 0:a3b41ab8433b | 246 | |
kenjiArai | 0:a3b41ab8433b | 247 | /** Write register (general purpose) |
kenjiArai | 0:a3b41ab8433b | 248 | * @param register's address |
kenjiArai | 0:a3b41ab8433b | 249 | * @param data |
kenjiArai | 0:a3b41ab8433b | 250 | * @return register data |
kenjiArai | 0:a3b41ab8433b | 251 | */ |
kenjiArai | 0:a3b41ab8433b | 252 | uint8_t write_reg(uint8_t addr, uint8_t data); |
kenjiArai | 0:a3b41ab8433b | 253 | |
kenjiArai | 0:a3b41ab8433b | 254 | protected: |
kenjiArai | 0:a3b41ab8433b | 255 | I2C _i2c; |
kenjiArai | 0:a3b41ab8433b | 256 | |
kenjiArai | 0:a3b41ab8433b | 257 | void initialize(void); |
kenjiArai | 0:a3b41ab8433b | 258 | |
kenjiArai | 0:a3b41ab8433b | 259 | private: |
kenjiArai | 0:a3b41ab8433b | 260 | INA219_TypeDef ina219_set_data; |
kenjiArai | 0:a3b41ab8433b | 261 | int32_t scale_factor; |
kenjiArai | 0:a3b41ab8433b | 262 | uint8_t dt[4]; |
kenjiArai | 0:a3b41ab8433b | 263 | |
kenjiArai | 0:a3b41ab8433b | 264 | }; |
kenjiArai | 0:a3b41ab8433b | 265 | |
kenjiArai | 1:fea983b0385b | 266 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 1:fea983b0385b | 267 | // Following parts are only internal use and you only use it if you control internal config reg. |
kenjiArai | 1:fea983b0385b | 268 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 1:fea983b0385b | 269 | /////////// BIT DEFINITION //////////////////////////////// |
kenjiArai | 1:fea983b0385b | 270 | #define INA219_CFG_RESET (1UL << 15) |
kenjiArai | 1:fea983b0385b | 271 | |
kenjiArai | 1:fea983b0385b | 272 | #define INA219_CFG_B32V (1UL << 13) |
kenjiArai | 1:fea983b0385b | 273 | #define INA219_CFG_B16V (0UL << 13) |
kenjiArai | 1:fea983b0385b | 274 | |
kenjiArai | 1:fea983b0385b | 275 | #define INA219_CFG_PGA_DIV_1 (0UL << 11) |
kenjiArai | 1:fea983b0385b | 276 | #define INA219_CFG_PGA_DIV_2 (1UL << 11) |
kenjiArai | 1:fea983b0385b | 277 | #define INA219_CFG_PGA_DIV_4 (2UL << 11) |
kenjiArai | 1:fea983b0385b | 278 | #define INA219_CFG_PGA_DIV_8 (3UL << 11) |
kenjiArai | 1:fea983b0385b | 279 | |
kenjiArai | 1:fea983b0385b | 280 | #define INA219_CFG_BADC (0xf << 7) |
kenjiArai | 1:fea983b0385b | 281 | |
kenjiArai | 1:fea983b0385b | 282 | #define INA219_CFG_SADC_9B_84U (0x0 << 3) |
kenjiArai | 1:fea983b0385b | 283 | #define INA219_CFG_SADC_10B_148U (0x1 << 3) |
kenjiArai | 1:fea983b0385b | 284 | #define INA219_CFG_SADC_11B_276U (0x2 << 3) |
kenjiArai | 1:fea983b0385b | 285 | #define INA219_CFG_SADC_12B_532U (0x3 << 3) |
kenjiArai | 1:fea983b0385b | 286 | #define INA219_CFG_SADC_2S_1R06M (0x9 << 3) |
kenjiArai | 1:fea983b0385b | 287 | #define INA219_CFG_SADC_4S_2R13M (0xa << 3) |
kenjiArai | 1:fea983b0385b | 288 | #define INA219_CFG_SADC_8S_4R26M (0xb << 3) |
kenjiArai | 1:fea983b0385b | 289 | #define INA219_CFG_SADC_16S_8R51M (0xc << 3) |
kenjiArai | 1:fea983b0385b | 290 | #define INA219_CFG_SADC_32S_17M (0xd << 3) |
kenjiArai | 1:fea983b0385b | 291 | #define INA219_CFG_SADC_64S_34M (0xe << 3) |
kenjiArai | 1:fea983b0385b | 292 | #define INA219_CFG_SADC_128S_68M (0xf << 3) |
kenjiArai | 1:fea983b0385b | 293 | |
kenjiArai | 1:fea983b0385b | 294 | #define INA219_CFG_MODE_PDWN (0UL << 0) |
kenjiArai | 1:fea983b0385b | 295 | #define INA219_CFG_MODE_SHNTTRG (1UL << 0) |
kenjiArai | 1:fea983b0385b | 296 | #define INA219_CFG_MODE_BUSTRG (2UL << 0) |
kenjiArai | 1:fea983b0385b | 297 | #define INA219_CFG_MODE_SBTRG (3UL << 0) |
kenjiArai | 1:fea983b0385b | 298 | #define INA219_CFG_MODE_ADC_OFF (4UL << 0) |
kenjiArai | 1:fea983b0385b | 299 | #define INA219_CFG_MODE_SHNT_CONT (5UL << 0) |
kenjiArai | 1:fea983b0385b | 300 | #define INA219_CFG_MODE_BUS_CONT (6UL << 0) |
kenjiArai | 1:fea983b0385b | 301 | #define INA219_CFG_MODE_SB_CONT (7UL << 0) |
kenjiArai | 1:fea983b0385b | 302 | |
kenjiArai | 0:a3b41ab8433b | 303 | #endif // MBED_INA219 |