ARM Mbed library for TI INA226. High-Side or Low-Side Measurement, Bi-Directional Current and Power Monitor with I2C Compatible Interface.
README.md@0:ed5e54b4383d, 2019-09-09 (annotated)
- Committer:
- Branilson Luiz
- Date:
- Mon Sep 09 02:34:29 2019 -0300
- Revision:
- 0:ed5e54b4383d
First commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Branilson Luiz |
0:ed5e54b4383d | 1 | <a href="https://github.com/branilson"><img src="https://github.com/branilson/ina226_mbed_library/raw/master/img/INA226.jpg" title="INA226" alt="BranlsonLuiz"></a> |
Branilson Luiz |
0:ed5e54b4383d | 2 | |
Branilson Luiz |
0:ed5e54b4383d | 3 | <!-- [![Branilson Luiz(https://github.com/branilson/ina226_mbed_library/raw/master/img/INA226.jpg](https://github.com/branilson) --> |
Branilson Luiz |
0:ed5e54b4383d | 4 | |
Branilson Luiz |
0:ed5e54b4383d | 5 | |
Branilson Luiz |
0:ed5e54b4383d | 6 | # Ina226 Mbed Library |
Branilson Luiz |
0:ed5e54b4383d | 7 | |
Branilson Luiz |
0:ed5e54b4383d | 8 | > INA226 High-Side or Low-Side Measurement, Bi-Directional Current and Power Monitor with I2C Compatible Interface |
Branilson Luiz |
0:ed5e54b4383d | 9 | |
Branilson Luiz |
0:ed5e54b4383d | 10 | > C++ Library for ARM mbed framework |
Branilson Luiz |
0:ed5e54b4383d | 11 | |
Branilson Luiz |
0:ed5e54b4383d | 12 | > Tested on ST STM32 evaluation boards Nucleo_L432KC and Nucleo_F746zg. |
Branilson Luiz |
0:ed5e54b4383d | 13 | |
Branilson Luiz |
0:ed5e54b4383d | 14 | > This program is free software. Licensed under the terms of the GPL version 3 from the Free Software Foundation. |
Branilson Luiz |
0:ed5e54b4383d | 15 | |
Branilson Luiz |
0:ed5e54b4383d | 16 | **Use** |
Branilson Luiz |
0:ed5e54b4383d | 17 | |
Branilson Luiz |
0:ed5e54b4383d | 18 | <a href="https://github.com/branilson"><img src="https://github.com/branilson/ina226_mbed_library/raw/master/img/Screenshot_ina226.png" title="Branilson Luiz" alt="BranlsonLuiz"></a> |
Branilson Luiz |
0:ed5e54b4383d | 19 | |
Branilson Luiz |
0:ed5e54b4383d | 20 | 1. Instantiate an mbed I2C object: |
Branilson Luiz |
0:ed5e54b4383d | 21 | |
Branilson Luiz |
0:ed5e54b4383d | 22 | <pre><code>I2C i2c(PB_7, PB_6); |
Branilson Luiz |
0:ed5e54b4383d | 23 | </code></pre> |
Branilson Luiz |
0:ed5e54b4383d | 24 | |
Branilson Luiz |
0:ed5e54b4383d | 25 | 2. Instatiate the ina226 object: |
Branilson Luiz |
0:ed5e54b4383d | 26 | The ina226 class constructor receives an mbed I2C object, the device address, and the I2C frequency value as parameters. Notice that mbed I2C implementation uses 8-bit adresses. So, the 7-bit adress of the device needs a bit left shift (<< 1). |
Branilson Luiz |
0:ed5e54b4383d | 27 | |
Branilson Luiz |
0:ed5e54b4383d | 28 | <pre><code>ina226 ina(i2c, 0x80, 400000); |
Branilson Luiz |
0:ed5e54b4383d | 29 | </code></pre> |
Branilson Luiz |
0:ed5e54b4383d | 30 | |
Branilson Luiz |
0:ed5e54b4383d | 31 | 3. Configure the device. |
Branilson Luiz |
0:ed5e54b4383d | 32 | The first thing needed before start measuring is to configure the behavior of the device. This method requires four: |
Branilson Luiz |
0:ed5e54b4383d | 33 | - Averaging Mode options: |
Branilson Luiz |
0:ed5e54b4383d | 34 | - AVERAGES_1, for no averaging; |
Branilson Luiz |
0:ed5e54b4383d | 35 | - AVERAGES_4, for 4 samples averaging; |
Branilson Luiz |
0:ed5e54b4383d | 36 | - AVERAGES_16, for 16 samples averaging; |
Branilson Luiz |
0:ed5e54b4383d | 37 | - AVERAGES_64, for 64 samples averaging; |
Branilson Luiz |
0:ed5e54b4383d | 38 | - AVERAGES_128, for 128 samples averaging; |
Branilson Luiz |
0:ed5e54b4383d | 39 | - AVERAGES_256, for 256 samples averaging; |
Branilson Luiz |
0:ed5e54b4383d | 40 | - AVERAGES_512, for 512 samples averaging; |
Branilson Luiz |
0:ed5e54b4383d | 41 | - AVERAGES_1024, for 1024 samples averaging. |
Branilson Luiz |
0:ed5e54b4383d | 42 | |
Branilson Luiz |
0:ed5e54b4383d | 43 | - Bus Voltage ConversionTime options: |
Branilson Luiz |
0:ed5e54b4383d | 44 | - BUS_CONV_TIME_140US, for 140 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 45 | - BUS_CONV_TIME_204US, for 204 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 46 | - BUS_CONV_TIME_332US, for 332 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 47 | - BUS_CONV_TIME_588US, for 588 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 48 | - BUS_CONV_TIME_1100US, for 1100 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 49 | - BUS_CONV_TIME_2116US, for 2116 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 50 | - BUS_CONV_TIME_4156US, for 4156 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 51 | - BUS_CONV_TIME_8244US, for 8244 microseconds voltage conversion time. |
Branilson Luiz |
0:ed5e54b4383d | 52 | |
Branilson Luiz |
0:ed5e54b4383d | 53 | - Shunt Voltage Conversion Time options: |
Branilson Luiz |
0:ed5e54b4383d | 54 | - BUS_CONV_TIME_140US, for 140 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 55 | - BUS_CONV_TIME_204US, for 204 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 56 | - BUS_CONV_TIME_332US, for 332 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 57 | - BUS_CONV_TIME_588US, for 588 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 58 | - BUS_CONV_TIME_1100US, for 1100 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 59 | - BUS_CONV_TIME_2116US, for 2116 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 60 | - BUS_CONV_TIME_4156US, for 4156 microseconds voltage conversion time; |
Branilson Luiz |
0:ed5e54b4383d | 61 | - BUS_CONV_TIME_8244US, for 8244 microseconds voltage conversion time. |
Branilson Luiz |
0:ed5e54b4383d | 62 | |
Branilson Luiz |
0:ed5e54b4383d | 63 | - Operating Mode options: |
Branilson Luiz |
0:ed5e54b4383d | 64 | - MODE_POWER_DOWN, |
Branilson Luiz |
0:ed5e54b4383d | 65 | - MODE_SHUNT_TRIG, |
Branilson Luiz |
0:ed5e54b4383d | 66 | - MODE_BUS_TRIG, |
Branilson Luiz |
0:ed5e54b4383d | 67 | - MODE_SHUNT_BUS_TRIG, |
Branilson Luiz |
0:ed5e54b4383d | 68 | - MODE_ADC_OFF, |
Branilson Luiz |
0:ed5e54b4383d | 69 | - MODE_SHUNT_CONT, |
Branilson Luiz |
0:ed5e54b4383d | 70 | - MODE_BUS_CONT, |
Branilson Luiz |
0:ed5e54b4383d | 71 | - MODE_SHUNT_BUS_CONT, |
Branilson Luiz |
0:ed5e54b4383d | 72 | |
Branilson Luiz |
0:ed5e54b4383d | 73 | <pre><code>ina.setConfig(AVERAGES_64, BUS_CONV_TIME_1100US, SHUNT_CONV_TIME_1100US, MODE_SHUNT_BUS_CONT); |
Branilson Luiz |
0:ed5e54b4383d | 74 | </code></pre> |
Branilson Luiz |
0:ed5e54b4383d | 75 | |
Branilson Luiz |
0:ed5e54b4383d | 76 | With no parameters, the default settings loaded are AVERAGES_64, BUS_CONV_TIME_1100US, SHUNT_CONV_TIME_1100US, MODE_SHUNT_BUS_CONT. |
Branilson Luiz |
0:ed5e54b4383d | 77 | |
Branilson Luiz |
0:ed5e54b4383d | 78 | <pre><code>ina.setConfig(); |
Branilson Luiz |
0:ed5e54b4383d | 79 | </code></pre> |
Branilson Luiz |
0:ed5e54b4383d | 80 | |
Branilson Luiz |
0:ed5e54b4383d | 81 | 4. Calibrate the device: |
Branilson Luiz |
0:ed5e54b4383d | 82 | This method do the calibration math using the supplied Shunt resistor value and maximun current expected. |
Branilson Luiz |
0:ed5e54b4383d | 83 | |
Branilson Luiz |
0:ed5e54b4383d | 84 | <pre><code>ina.setCalibration(0.01, 5.0); |
Branilson Luiz |
0:ed5e54b4383d | 85 | </code></pre> |
Branilson Luiz |
0:ed5e54b4383d | 86 | |
Branilson Luiz |
0:ed5e54b4383d | 87 | With no parameters the object will adopt the default calibration parameters: rShunt = 0.01 Ohm, Imax = 8.191 A. |
Branilson Luiz |
0:ed5e54b4383d | 88 | |
Branilson Luiz |
0:ed5e54b4383d | 89 | <pre><code>ina.setCalibration(); |
Branilson Luiz |
0:ed5e54b4383d | 90 | </code></pre> |
Branilson Luiz |
0:ed5e54b4383d | 91 | |
Branilson Luiz |
0:ed5e54b4383d | 92 | 5. Set an alert type (optional). |
Branilson Luiz |
0:ed5e54b4383d | 93 | |
Branilson Luiz |
0:ed5e54b4383d | 94 | <pre><code>ina.enableShuntOverVoltageAlert(); //for shunt overvoltage or current limit |
Branilson Luiz |
0:ed5e54b4383d | 95 | </code></pre> |
Branilson Luiz |
0:ed5e54b4383d | 96 | |
Branilson Luiz |
0:ed5e54b4383d | 97 | 6. Set an alert value (optional). |
Branilson Luiz |
0:ed5e54b4383d | 98 | |
Branilson Luiz |
0:ed5e54b4383d | 99 | <pre><code>ina.setOverCurrentLimit(2.4); // for 2.4 A current limit alert |
Branilson Luiz |
0:ed5e54b4383d | 100 | </code></pre> |
Branilson Luiz |
0:ed5e54b4383d | 101 | |
Branilson Luiz |
0:ed5e54b4383d | 102 | 7. Start reading data: |
Branilson Luiz |
0:ed5e54b4383d | 103 | |
Branilson Luiz |
0:ed5e54b4383d | 104 | <pre><code>ina.readManufacturerID(), |
Branilson Luiz |
0:ed5e54b4383d | 105 | ina.readDieID(), |
Branilson Luiz |
0:ed5e54b4383d | 106 | ina.readCalibration(), |
Branilson Luiz |
0:ed5e54b4383d | 107 | ina.readShuntVoltage(), |
Branilson Luiz |
0:ed5e54b4383d | 108 | ina.readBusVoltage(), |
Branilson Luiz |
0:ed5e54b4383d | 109 | ina.readCurrent(), |
Branilson Luiz |
0:ed5e54b4383d | 110 | ina.readPower()); |
Branilson Luiz |
0:ed5e54b4383d | 111 | </code></pre> |
Branilson Luiz |
0:ed5e54b4383d | 112 | |
Branilson Luiz |
0:ed5e54b4383d | 113 | Many other methods for reading and writing data are available in the class. See the header file for more details. |
Branilson Luiz |
0:ed5e54b4383d | 114 | |
Branilson Luiz |
0:ed5e54b4383d | 115 | <a href="https://github.com/branilson"><img src="https://github.com/branilson/ina226_mbed_library/raw/master/img/ina226_circuit.jpg" title="Branilson Luiz" alt="BranlsonLuiz"></a> |