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
INA230.cpp@3:3ee11c82acdb, 2017-03-24 (annotated)
- Committer:
- loopsva
- Date:
- Fri Mar 24 00:13:28 2017 +0000
- Revision:
- 3:3ee11c82acdb
Initial release of the INA230 I2C driver
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
loopsva | 3:3ee11c82acdb | 1 | /* |
loopsva | 3:3ee11c82acdb | 2 | * mbed library program |
loopsva | 3:3ee11c82acdb | 3 | * INA230 High-Side Measurement,Bi-Directional CURRENT/POWER MONITOR with I2C Interface |
loopsva | 3:3ee11c82acdb | 4 | * by Texas Instruments |
loopsva | 3:3ee11c82acdb | 5 | * |
loopsva | 3:3ee11c82acdb | 6 | * Copyright (c) 2015 Kenji Arai / JH1PJL |
loopsva | 3:3ee11c82acdb | 7 | * http://www.page.sannet.ne.jp/kenjia/index.html |
loopsva | 3:3ee11c82acdb | 8 | * http://mbed.org/users/kenjiArai/ |
loopsva | 3:3ee11c82acdb | 9 | * Created: January 25th, 2015 |
loopsva | 3:3ee11c82acdb | 10 | * Revised: May 5th, 2015 |
loopsva | 3:3ee11c82acdb | 11 | * |
loopsva | 3:3ee11c82acdb | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
loopsva | 3:3ee11c82acdb | 13 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
loopsva | 3:3ee11c82acdb | 14 | * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
loopsva | 3:3ee11c82acdb | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
loopsva | 3:3ee11c82acdb | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
loopsva | 3:3ee11c82acdb | 17 | */ |
loopsva | 3:3ee11c82acdb | 18 | |
loopsva | 3:3ee11c82acdb | 19 | #include "mbed.h" |
loopsva | 3:3ee11c82acdb | 20 | #include "INA230.h" |
loopsva | 3:3ee11c82acdb | 21 | |
loopsva | 3:3ee11c82acdb | 22 | INA230::INA230 (PinName p_sda, PinName p_scl, const INA230_TypeDef *ina230_parameter) : |
loopsva | 3:3ee11c82acdb | 23 | _i2c(p_sda, p_scl) |
loopsva | 3:3ee11c82acdb | 24 | { |
loopsva | 3:3ee11c82acdb | 25 | _i2c.frequency(400000); |
loopsva | 3:3ee11c82acdb | 26 | ina230_set_data = *ina230_parameter; |
loopsva | 3:3ee11c82acdb | 27 | initialize(); |
loopsva | 3:3ee11c82acdb | 28 | } |
loopsva | 3:3ee11c82acdb | 29 | |
loopsva | 3:3ee11c82acdb | 30 | INA230::INA230 (PinName p_sda, PinName p_scl, uint8_t addr) : |
loopsva | 3:3ee11c82acdb | 31 | _i2c(p_sda, p_scl) |
loopsva | 3:3ee11c82acdb | 32 | { |
loopsva | 3:3ee11c82acdb | 33 | _i2c.frequency(400000); |
loopsva | 3:3ee11c82acdb | 34 | // Use standard setting |
loopsva | 3:3ee11c82acdb | 35 | ina230_set_data = ina230_std_paramtr; |
loopsva | 3:3ee11c82acdb | 36 | // Change user defined address |
loopsva | 3:3ee11c82acdb | 37 | ina230_set_data.addr = addr; |
loopsva | 3:3ee11c82acdb | 38 | initialize(); |
loopsva | 3:3ee11c82acdb | 39 | } |
loopsva | 3:3ee11c82acdb | 40 | |
loopsva | 3:3ee11c82acdb | 41 | INA230::INA230 (PinName p_sda, PinName p_scl) : |
loopsva | 3:3ee11c82acdb | 42 | _i2c(p_sda, p_scl) |
loopsva | 3:3ee11c82acdb | 43 | { |
loopsva | 3:3ee11c82acdb | 44 | _i2c.frequency(400000); |
loopsva | 3:3ee11c82acdb | 45 | // Use standard setting |
loopsva | 3:3ee11c82acdb | 46 | ina230_set_data = ina230_std_paramtr; |
loopsva | 3:3ee11c82acdb | 47 | initialize(); |
loopsva | 3:3ee11c82acdb | 48 | } |
loopsva | 3:3ee11c82acdb | 49 | |
loopsva | 3:3ee11c82acdb | 50 | INA230::INA230 (I2C& p_i2c, const INA230_TypeDef *ina230_parameter) : _i2c(p_i2c) |
loopsva | 3:3ee11c82acdb | 51 | { |
loopsva | 3:3ee11c82acdb | 52 | _i2c.frequency(400000); |
loopsva | 3:3ee11c82acdb | 53 | ina230_set_data = *ina230_parameter; |
loopsva | 3:3ee11c82acdb | 54 | initialize(); |
loopsva | 3:3ee11c82acdb | 55 | } |
loopsva | 3:3ee11c82acdb | 56 | |
loopsva | 3:3ee11c82acdb | 57 | INA230::INA230 (I2C& p_i2c, uint8_t addr) : _i2c(p_i2c) |
loopsva | 3:3ee11c82acdb | 58 | { |
loopsva | 3:3ee11c82acdb | 59 | _i2c.frequency(400000); |
loopsva | 3:3ee11c82acdb | 60 | // Use standard setting |
loopsva | 3:3ee11c82acdb | 61 | ina230_set_data = ina230_std_paramtr; |
loopsva | 3:3ee11c82acdb | 62 | // Change user defined address |
loopsva | 3:3ee11c82acdb | 63 | ina230_set_data.addr = addr; |
loopsva | 3:3ee11c82acdb | 64 | initialize(); |
loopsva | 3:3ee11c82acdb | 65 | } |
loopsva | 3:3ee11c82acdb | 66 | |
loopsva | 3:3ee11c82acdb | 67 | INA230::INA230 (I2C& p_i2c) : _i2c(p_i2c) |
loopsva | 3:3ee11c82acdb | 68 | { |
loopsva | 3:3ee11c82acdb | 69 | _i2c.frequency(400000); |
loopsva | 3:3ee11c82acdb | 70 | // Use standard setting |
loopsva | 3:3ee11c82acdb | 71 | ina230_set_data = ina230_std_paramtr; |
loopsva | 3:3ee11c82acdb | 72 | initialize(); |
loopsva | 3:3ee11c82acdb | 73 | } |
loopsva | 3:3ee11c82acdb | 74 | |
loopsva | 3:3ee11c82acdb | 75 | /////////////// Read Current ////////////////////////////// |
loopsva | 3:3ee11c82acdb | 76 | //int16_t rawIreg = 0; |
loopsva | 3:3ee11c82acdb | 77 | float INA230::read_current() |
loopsva | 3:3ee11c82acdb | 78 | { |
loopsva | 3:3ee11c82acdb | 79 | dt[0] = INA230_CURRENT; |
loopsva | 3:3ee11c82acdb | 80 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 81 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 82 | int16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 83 | //rawIreg = data; |
loopsva | 3:3ee11c82acdb | 84 | return (double)data * 0.08255; |
loopsva | 3:3ee11c82acdb | 85 | // 0.06605 & CalReg = 0xF000 works to 2.16A (25.9W) with 2512 0.01ohm, cal'd @ 1.500A |
loopsva | 3:3ee11c82acdb | 86 | // 0.07075 & CalReg = 0xE000 works to 2.31A (27.7W) |
loopsva | 3:3ee11c82acdb | 87 | // 0.07617 & CalReg = 0xD000 works to 2.50A (30.0W) |
loopsva | 3:3ee11c82acdb | 88 | // 0.08255 & CalReg = 0xC000 works to 2.70A (32.4W) |
loopsva | 3:3ee11c82acdb | 89 | //was |
loopsva | 3:3ee11c82acdb | 90 | // 0.06065 & CalReg = 0xF000 works to 1.98A (23.8W) with 1206 0.01ohm |
loopsva | 3:3ee11c82acdb | 91 | // 0.06500 & CalReg = 0xE000 works to 2.15A (25.9W) |
loopsva | 3:3ee11c82acdb | 92 | // 0.07570 & CalReg = 0xC000 works to 2.48A (29.4W) |
loopsva | 3:3ee11c82acdb | 93 | } |
loopsva | 3:3ee11c82acdb | 94 | |
loopsva | 3:3ee11c82acdb | 95 | /////////////// Read Current ////////////////////////////// |
loopsva | 3:3ee11c82acdb | 96 | int16_t INA230::read_current_reg() |
loopsva | 3:3ee11c82acdb | 97 | { |
loopsva | 3:3ee11c82acdb | 98 | dt[0] = INA230_CURRENT; |
loopsva | 3:3ee11c82acdb | 99 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 100 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 101 | uint16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 102 | return data; |
loopsva | 3:3ee11c82acdb | 103 | } |
loopsva | 3:3ee11c82acdb | 104 | |
loopsva | 3:3ee11c82acdb | 105 | /////////////// Read Power //////////////////////////////// |
loopsva | 3:3ee11c82acdb | 106 | float INA230::read_power() |
loopsva | 3:3ee11c82acdb | 107 | { |
loopsva | 3:3ee11c82acdb | 108 | dt[0] = INA230_POWER; |
loopsva | 3:3ee11c82acdb | 109 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 110 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 111 | int16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 112 | return (float)data / 2000; |
loopsva | 3:3ee11c82acdb | 113 | } |
loopsva | 3:3ee11c82acdb | 114 | |
loopsva | 3:3ee11c82acdb | 115 | //--------------------------------------------------------------------- |
loopsva | 3:3ee11c82acdb | 116 | // Read Bus_volt |
loopsva | 3:3ee11c82acdb | 117 | |
loopsva | 3:3ee11c82acdb | 118 | float INA230::read_bus_voltage() |
loopsva | 3:3ee11c82acdb | 119 | { |
loopsva | 3:3ee11c82acdb | 120 | dt[0] = INA230_BUS_VOLT; |
loopsva | 3:3ee11c82acdb | 121 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 122 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 123 | int16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 124 | //pc.printf("\r\nG = %+9.3f [V] 0x%x\r\n", (float)data * 0.00125f, data); |
loopsva | 3:3ee11c82acdb | 125 | return (float)data * 0.00125f; |
loopsva | 3:3ee11c82acdb | 126 | } |
loopsva | 3:3ee11c82acdb | 127 | |
loopsva | 3:3ee11c82acdb | 128 | /////////////// Read Shunt volt /////////////////////////// |
loopsva | 3:3ee11c82acdb | 129 | float INA230::read_shunt_voltage() |
loopsva | 3:3ee11c82acdb | 130 | { |
loopsva | 3:3ee11c82acdb | 131 | dt[0] = INA230_SHUNT_V; |
loopsva | 3:3ee11c82acdb | 132 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 133 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 134 | int16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 135 | return (float)data; |
loopsva | 3:3ee11c82acdb | 136 | } |
loopsva | 3:3ee11c82acdb | 137 | |
loopsva | 3:3ee11c82acdb | 138 | float INA230::read_current_by_shuntvolt() |
loopsva | 3:3ee11c82acdb | 139 | { |
loopsva | 3:3ee11c82acdb | 140 | dt[0] = INA230_SHUNT_V; |
loopsva | 3:3ee11c82acdb | 141 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 142 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 143 | int16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 144 | return (float)data / 10; |
loopsva | 3:3ee11c82acdb | 145 | // return ((float)data / ina230_set_data.shunt_register) / 1000; |
loopsva | 3:3ee11c82acdb | 146 | } |
loopsva | 3:3ee11c82acdb | 147 | |
loopsva | 3:3ee11c82acdb | 148 | /////////////// Read configulation //////////////////////// |
loopsva | 3:3ee11c82acdb | 149 | uint16_t INA230::read_config() |
loopsva | 3:3ee11c82acdb | 150 | { |
loopsva | 3:3ee11c82acdb | 151 | dt[0] = INA230_CONFIG; |
loopsva | 3:3ee11c82acdb | 152 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 153 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 154 | uint16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 155 | return data; |
loopsva | 3:3ee11c82acdb | 156 | } |
loopsva | 3:3ee11c82acdb | 157 | |
loopsva | 3:3ee11c82acdb | 158 | //--------------------------------------------------------------------- |
loopsva | 3:3ee11c82acdb | 159 | // get mask enable reg |
loopsva | 3:3ee11c82acdb | 160 | |
loopsva | 3:3ee11c82acdb | 161 | uint16_t INA230::read_mask_enable() |
loopsva | 3:3ee11c82acdb | 162 | { |
loopsva | 3:3ee11c82acdb | 163 | dt[0] = INA230_MASK_ENABLE; |
loopsva | 3:3ee11c82acdb | 164 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 165 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 166 | uint16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 167 | ina230_set_data.die_id_data = data; |
loopsva | 3:3ee11c82acdb | 168 | return data; |
loopsva | 3:3ee11c82acdb | 169 | } |
loopsva | 3:3ee11c82acdb | 170 | |
loopsva | 3:3ee11c82acdb | 171 | //--------------------------------------------------------------------- |
loopsva | 3:3ee11c82acdb | 172 | // get alert limit register |
loopsva | 3:3ee11c82acdb | 173 | |
loopsva | 3:3ee11c82acdb | 174 | uint16_t INA230::read_alert_limit() |
loopsva | 3:3ee11c82acdb | 175 | { |
loopsva | 3:3ee11c82acdb | 176 | dt[0] = INA230_ALERT_LIMIT; |
loopsva | 3:3ee11c82acdb | 177 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 178 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 179 | uint16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 180 | ina230_set_data.die_id_data = data; |
loopsva | 3:3ee11c82acdb | 181 | return data; |
loopsva | 3:3ee11c82acdb | 182 | } |
loopsva | 3:3ee11c82acdb | 183 | |
loopsva | 3:3ee11c82acdb | 184 | //--------------------------------------------------------------------- |
loopsva | 3:3ee11c82acdb | 185 | // get die ID |
loopsva | 3:3ee11c82acdb | 186 | |
loopsva | 3:3ee11c82acdb | 187 | uint16_t INA230::read_die_id() |
loopsva | 3:3ee11c82acdb | 188 | { |
loopsva | 3:3ee11c82acdb | 189 | dt[0] = INA230_DIE_ID; |
loopsva | 3:3ee11c82acdb | 190 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 191 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 192 | uint16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 193 | ina230_set_data.die_id_data = data; |
loopsva | 3:3ee11c82acdb | 194 | return data; |
loopsva | 3:3ee11c82acdb | 195 | } |
loopsva | 3:3ee11c82acdb | 196 | |
loopsva | 3:3ee11c82acdb | 197 | //--------------------------------------------------------------------- |
loopsva | 3:3ee11c82acdb | 198 | // set mask enable register |
loopsva | 3:3ee11c82acdb | 199 | |
loopsva | 3:3ee11c82acdb | 200 | uint16_t INA230::set_mask_enable(uint16_t cfg) |
loopsva | 3:3ee11c82acdb | 201 | { |
loopsva | 3:3ee11c82acdb | 202 | uint16_t data = cfg; |
loopsva | 3:3ee11c82acdb | 203 | dt[0] = INA230_MASK_ENABLE; |
loopsva | 3:3ee11c82acdb | 204 | dt[1] = data >> 8; // MSB 1st |
loopsva | 3:3ee11c82acdb | 205 | dt[2] = data & 0xff; // LSB 2nd |
loopsva | 3:3ee11c82acdb | 206 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 3, false); |
loopsva | 3:3ee11c82acdb | 207 | return data; |
loopsva | 3:3ee11c82acdb | 208 | } |
loopsva | 3:3ee11c82acdb | 209 | |
loopsva | 3:3ee11c82acdb | 210 | //--------------------------------------------------------------------- |
loopsva | 3:3ee11c82acdb | 211 | // set mask enable register |
loopsva | 3:3ee11c82acdb | 212 | |
loopsva | 3:3ee11c82acdb | 213 | uint16_t INA230::set_alert_limit(uint16_t cfg) |
loopsva | 3:3ee11c82acdb | 214 | { |
loopsva | 3:3ee11c82acdb | 215 | uint16_t data = cfg; |
loopsva | 3:3ee11c82acdb | 216 | dt[0] = INA230_ALERT_LIMIT; |
loopsva | 3:3ee11c82acdb | 217 | dt[1] = data >> 8; // MSB 1st |
loopsva | 3:3ee11c82acdb | 218 | dt[2] = data & 0xff; // LSB 2nd |
loopsva | 3:3ee11c82acdb | 219 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 3, false); |
loopsva | 3:3ee11c82acdb | 220 | return data; |
loopsva | 3:3ee11c82acdb | 221 | } |
loopsva | 3:3ee11c82acdb | 222 | |
loopsva | 3:3ee11c82acdb | 223 | //--------------------------------------------------------------------- |
loopsva | 3:3ee11c82acdb | 224 | // get shunt resistor value |
loopsva | 3:3ee11c82acdb | 225 | |
loopsva | 3:3ee11c82acdb | 226 | int16_t INA230::get_shunt_res() { return ina230_set_data.shunt_res; } |
loopsva | 3:3ee11c82acdb | 227 | |
loopsva | 3:3ee11c82acdb | 228 | /* |
loopsva | 3:3ee11c82acdb | 229 | //--------------------------------------------------------------------- |
loopsva | 3:3ee11c82acdb | 230 | // print stuff for degugging |
loopsva | 3:3ee11c82acdb | 231 | |
loopsva | 3:3ee11c82acdb | 232 | extern RawSerial pc; |
loopsva | 3:3ee11c82acdb | 233 | |
loopsva | 3:3ee11c82acdb | 234 | void INA230::dumpStructure() { |
loopsva | 3:3ee11c82acdb | 235 | pc.printf("Addr: %02x A: %04x B: %04x S: %04x M: %04x Conf: %04x Cal: %04x Die: %04x SR: %d\r\n", ina230_set_data.addr, ina230_set_data.average, ina230_set_data.bus_ct, |
loopsva | 3:3ee11c82acdb | 236 | ina230_set_data.shunt_ct, ina230_set_data.mode, |
loopsva | 3:3ee11c82acdb | 237 | ina230_set_data.average | ina230_set_data.bus_ct | ina230_set_data.shunt_ct | ina230_set_data.mode, |
loopsva | 3:3ee11c82acdb | 238 | ina230_set_data.cal_data, ina230_set_data.die_id_data, ina230_set_data.shunt_res); |
loopsva | 3:3ee11c82acdb | 239 | } |
loopsva | 3:3ee11c82acdb | 240 | */ |
loopsva | 3:3ee11c82acdb | 241 | |
loopsva | 3:3ee11c82acdb | 242 | /////////////// Set configulation ///////////////////////// |
loopsva | 3:3ee11c82acdb | 243 | uint16_t INA230::set_config(uint16_t cfg) |
loopsva | 3:3ee11c82acdb | 244 | { |
loopsva | 3:3ee11c82acdb | 245 | uint16_t data = cfg; |
loopsva | 3:3ee11c82acdb | 246 | dt[0] = INA230_CONFIG; |
loopsva | 3:3ee11c82acdb | 247 | dt[1] = data >> 8; // MSB 1st |
loopsva | 3:3ee11c82acdb | 248 | dt[2] = data & 0xff; // LSB 2nd |
loopsva | 3:3ee11c82acdb | 249 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 3, false); |
loopsva | 3:3ee11c82acdb | 250 | return data; |
loopsva | 3:3ee11c82acdb | 251 | } |
loopsva | 3:3ee11c82acdb | 252 | |
loopsva | 3:3ee11c82acdb | 253 | /////////////// Read Calibration reg. ///////////////////// |
loopsva | 3:3ee11c82acdb | 254 | uint16_t INA230::read_calb(void) |
loopsva | 3:3ee11c82acdb | 255 | { |
loopsva | 3:3ee11c82acdb | 256 | dt[0] = INA230_CALIB; |
loopsva | 3:3ee11c82acdb | 257 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 258 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 259 | uint16_t data = (dt[0] << 8) | dt[1]; |
loopsva | 3:3ee11c82acdb | 260 | return data; |
loopsva | 3:3ee11c82acdb | 261 | } |
loopsva | 3:3ee11c82acdb | 262 | |
loopsva | 3:3ee11c82acdb | 263 | /////////////// Set Calibration reg. ////////////////////// |
loopsva | 3:3ee11c82acdb | 264 | uint16_t INA230::set_calb(uint16_t clb) |
loopsva | 3:3ee11c82acdb | 265 | { |
loopsva | 3:3ee11c82acdb | 266 | uint16_t data = clb; |
loopsva | 3:3ee11c82acdb | 267 | dt[0] = INA230_CALIB; |
loopsva | 3:3ee11c82acdb | 268 | dt[1] = data >> 8; // MSB 1st |
loopsva | 3:3ee11c82acdb | 269 | dt[2] = data & 0xff; // LSB 2nd |
loopsva | 3:3ee11c82acdb | 270 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 3, false); |
loopsva | 3:3ee11c82acdb | 271 | return data; |
loopsva | 3:3ee11c82acdb | 272 | } |
loopsva | 3:3ee11c82acdb | 273 | |
loopsva | 3:3ee11c82acdb | 274 | /////////////// Read/Write specific register ////////////// |
loopsva | 3:3ee11c82acdb | 275 | uint8_t INA230::read_reg(uint8_t addr) |
loopsva | 3:3ee11c82acdb | 276 | { |
loopsva | 3:3ee11c82acdb | 277 | dt[0] = addr; |
loopsva | 3:3ee11c82acdb | 278 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 1, true); |
loopsva | 3:3ee11c82acdb | 279 | _i2c.read((int)ina230_set_data.addr, (char *)dt, 1, false); |
loopsva | 3:3ee11c82acdb | 280 | return dt[0]; |
loopsva | 3:3ee11c82acdb | 281 | } |
loopsva | 3:3ee11c82acdb | 282 | |
loopsva | 3:3ee11c82acdb | 283 | uint8_t INA230::write_reg(uint8_t addr, uint8_t data) |
loopsva | 3:3ee11c82acdb | 284 | { |
loopsva | 3:3ee11c82acdb | 285 | dt[0] = addr; |
loopsva | 3:3ee11c82acdb | 286 | dt[1] = data; |
loopsva | 3:3ee11c82acdb | 287 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 2, false); |
loopsva | 3:3ee11c82acdb | 288 | return dt[1]; |
loopsva | 3:3ee11c82acdb | 289 | } |
loopsva | 3:3ee11c82acdb | 290 | |
loopsva | 3:3ee11c82acdb | 291 | /////////////// Initialize //////////////////////////////// |
loopsva | 3:3ee11c82acdb | 292 | void INA230::initialize() |
loopsva | 3:3ee11c82acdb | 293 | { |
loopsva | 3:3ee11c82acdb | 294 | /* |
loopsva | 3:3ee11c82acdb | 295 | uint16_t data = 0; |
loopsva | 3:3ee11c82acdb | 296 | data = (ina230_set_data.v_max & 0x01) << 13; |
loopsva | 3:3ee11c82acdb | 297 | data |= (ina230_set_data.gain & 0x03) << 11; |
loopsva | 3:3ee11c82acdb | 298 | data |= (ina230_set_data.bus_adc_resolution & 0x0f) << 7; |
loopsva | 3:3ee11c82acdb | 299 | data |= (ina230_set_data.Shunt_adc_resolution & 0x0f) << 3; |
loopsva | 3:3ee11c82acdb | 300 | data |= (ina230_set_data.mode & 0x07); |
loopsva | 3:3ee11c82acdb | 301 | */ |
loopsva | 3:3ee11c82acdb | 302 | uint16_t data = ina230_set_data.average | ina230_set_data.bus_ct | ina230_set_data.shunt_ct | ina230_set_data.bus_ct | ina230_set_data.mode; |
loopsva | 3:3ee11c82acdb | 303 | dt[0] = INA230_CONFIG; |
loopsva | 3:3ee11c82acdb | 304 | dt[1] = data >> 8; // MSB 1st |
loopsva | 3:3ee11c82acdb | 305 | dt[2] = data & 0xff; // LSB 2nd |
loopsva | 3:3ee11c82acdb | 306 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 3, false); |
loopsva | 3:3ee11c82acdb | 307 | dt[0] = INA230_CALIB; |
loopsva | 3:3ee11c82acdb | 308 | dt[1] = ina230_set_data.cal_data >> 8; // MSB 1st |
loopsva | 3:3ee11c82acdb | 309 | dt[2] = ina230_set_data.cal_data & 0xff; // LSB 2nd |
loopsva | 3:3ee11c82acdb | 310 | _i2c.write((int)ina230_set_data.addr, (char *)dt, 3, false); |
loopsva | 3:3ee11c82acdb | 311 | scale_factor = 0; |
loopsva | 3:3ee11c82acdb | 312 | } |
loopsva | 3:3ee11c82acdb | 313 | |
loopsva | 3:3ee11c82acdb | 314 | /////////////// I2C Freq. ///////////////////////////////// |
loopsva | 3:3ee11c82acdb | 315 | void INA230::frequency(int hz) |
loopsva | 3:3ee11c82acdb | 316 | { |
loopsva | 3:3ee11c82acdb | 317 | _i2c.frequency(hz); |
loopsva | 3:3ee11c82acdb | 318 | } |
loopsva | 3:3ee11c82acdb | 319 | |
loopsva | 3:3ee11c82acdb | 320 | |
loopsva | 3:3ee11c82acdb | 321 |