This is a modified library for the INA219 that can handle a 0.01 ohm resistor instead of a 0.1 Ohm resistor. This allows it to read up to 32A instead of 3.2A.

Dependents:   Hybrid_Supercapacitor_Car_Battery_Monitoring

Fork of INA219 by Components

Committer:
melse
Date:
Thu Aug 28 10:32:20 2014 +0000
Revision:
0:eee9c8ba72ff
Child:
1:8ccc8e47e3d5
Initial Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
melse 0:eee9c8ba72ff 1 #include "mbed.h"
melse 0:eee9c8ba72ff 2 #include "INA219_consts.h"
melse 0:eee9c8ba72ff 3
melse 0:eee9c8ba72ff 4 #define DEBUG
melse 0:eee9c8ba72ff 5
melse 0:eee9c8ba72ff 6 /// Represents the different supported voltage levels
melse 0:eee9c8ba72ff 7 typedef enum {
melse 0:eee9c8ba72ff 8 BUS_16V=16,
melse 0:eee9c8ba72ff 9 BUS_32V=32
melse 0:eee9c8ba72ff 10 } busvoltage_t;
melse 0:eee9c8ba72ff 11
melse 0:eee9c8ba72ff 12 /** @enum Resolution typedef
melse 0:eee9c8ba72ff 13 * @brief Represents the different ADC resolutions supported by the INA219.
melse 0:eee9c8ba72ff 14 */
melse 0:eee9c8ba72ff 15 typedef enum {
melse 0:eee9c8ba72ff 16 RES_12BITS=12, ///< 12 bit precision (532us)
melse 0:eee9c8ba72ff 17 RES_11BITS=11, ///< 11 bit precision (276us)
melse 0:eee9c8ba72ff 18 RES_10BITS=10, ///< 10 bit precision (148us)
melse 0:eee9c8ba72ff 19 RES_9BITS =9 ///< 9 bit precision (84us)
melse 0:eee9c8ba72ff 20 } resolution_t;
melse 0:eee9c8ba72ff 21
melse 0:eee9c8ba72ff 22 /** @class INA219
melse 0:eee9c8ba72ff 23 * @brief A class to represent the INA219 breakout board from Adafruit.
melse 0:eee9c8ba72ff 24 */
melse 0:eee9c8ba72ff 25 class INA219: protected I2C
melse 0:eee9c8ba72ff 26 {
melse 0:eee9c8ba72ff 27 public:
melse 0:eee9c8ba72ff 28 /** Creates an instance of the INA219 device.
melse 0:eee9c8ba72ff 29 *
melse 0:eee9c8ba72ff 30 * @param sda An I2C data pin.
melse 0:eee9c8ba72ff 31 * @param scl An I2C clock pin.
melse 0:eee9c8ba72ff 32 * @param addr The I2C address of the INA219. (Default = 0x40)
melse 0:eee9c8ba72ff 33 * @param freq The I2C clock frequency.
melse 0:eee9c8ba72ff 34 * @param res The desired resolution of the INA219's ADC.
melse 0:eee9c8ba72ff 35 */
melse 0:eee9c8ba72ff 36 INA219 (PinName sda, PinName scl, int addr=0x40, int freq=100000, resolution_t res=RES_12BITS);
melse 0:eee9c8ba72ff 37
melse 0:eee9c8ba72ff 38 /** Reads the current raw value of current from the INA219.
melse 0:eee9c8ba72ff 39 *
melse 0:eee9c8ba72ff 40 * @see read_current_mA
melse 0:eee9c8ba72ff 41 *
melse 0:eee9c8ba72ff 42 * @returns
melse 0:eee9c8ba72ff 43 * A value between -32768 and +32768. Depending on the current calibration and configuration register values, the corresponding current can be calculated.
melse 0:eee9c8ba72ff 44 */
melse 0:eee9c8ba72ff 45 int16_t read_current_raw();
melse 0:eee9c8ba72ff 46 /** Reads the current from the INA219 and calculates the actual value in mA.
melse 0:eee9c8ba72ff 47 *
melse 0:eee9c8ba72ff 48 * @see read_current_raw
melse 0:eee9c8ba72ff 49 *
melse 0:eee9c8ba72ff 50 * @returns
melse 0:eee9c8ba72ff 51 * A floating point value corresponding to the current flowing through the current shunt, in mA.
melse 0:eee9c8ba72ff 52 */
melse 0:eee9c8ba72ff 53 float read_current_mA();
melse 0:eee9c8ba72ff 54
melse 0:eee9c8ba72ff 55 /** Reads the raw power value from the INA219.
melse 0:eee9c8ba72ff 56 *
melse 0:eee9c8ba72ff 57 * @see read_power_mW
melse 0:eee9c8ba72ff 58 *
melse 0:eee9c8ba72ff 59 * @returns
melse 0:eee9c8ba72ff 60 * A value between -32768 and +32768. Depending on the calibration and configuration register values, the actual power can be calculated.
melse 0:eee9c8ba72ff 61 */
melse 0:eee9c8ba72ff 62 //int16_t read_power_raw();
melse 0:eee9c8ba72ff 63 /** Reads the power from the INA219 and calculates the actual value in mW.
melse 0:eee9c8ba72ff 64 *
melse 0:eee9c8ba72ff 65 * @see read_power_raw
melse 0:eee9c8ba72ff 66 *
melse 0:eee9c8ba72ff 67 * @returns
melse 0:eee9c8ba72ff 68 * A floating point value corresponding to the power being used in the circuit, in mW.
melse 0:eee9c8ba72ff 69 */
melse 0:eee9c8ba72ff 70 //float read_power_mW();
melse 0:eee9c8ba72ff 71
melse 0:eee9c8ba72ff 72 /** Reads the raw shunt voltage value from the INA219.
melse 0:eee9c8ba72ff 73 *
melse 0:eee9c8ba72ff 74 * @see read_shunt_voltage_mV
melse 0:eee9c8ba72ff 75 *
melse 0:eee9c8ba72ff 76 * @returns
melse 0:eee9c8ba72ff 77 * A value between -32768 and +32768. Depending on the calibration and configuration register values, the actual shunt voltage can be calculated.
melse 0:eee9c8ba72ff 78 */
melse 0:eee9c8ba72ff 79 //int16_t read_shunt_voltage_raw();
melse 0:eee9c8ba72ff 80 /** Reads the shunt voltage from the INA219 and calculates the actual value in mV.
melse 0:eee9c8ba72ff 81 *
melse 0:eee9c8ba72ff 82 * @see read_shunt_voltage_raw
melse 0:eee9c8ba72ff 83 *
melse 0:eee9c8ba72ff 84 * @returns
melse 0:eee9c8ba72ff 85 * A floating point value corresponding to the potential difference across the current shunt, in mV.
melse 0:eee9c8ba72ff 86 */
melse 0:eee9c8ba72ff 87 //float read_shunt_voltage_mV();
melse 0:eee9c8ba72ff 88
melse 0:eee9c8ba72ff 89 /** Reads the raw bus voltage.
melse 0:eee9c8ba72ff 90 *
melse 0:eee9c8ba72ff 91 * @see read_bus_voltage
melse 0:eee9c8ba72ff 92 *
melse 0:eee9c8ba72ff 93 * @returns
melse 0:eee9c8ba72ff 94 * A value between -32768 and +32768 corresponding to the bus voltage.
melse 0:eee9c8ba72ff 95 */
melse 0:eee9c8ba72ff 96 //int16_t read_bus_voltage_raw();
melse 0:eee9c8ba72ff 97 /** Reads the bus voltage and uses it to calculate the actual bus voltage.
melse 0:eee9c8ba72ff 98 *
melse 0:eee9c8ba72ff 99 * @see read_bus_voltage_raw
melse 0:eee9c8ba72ff 100 *
melse 0:eee9c8ba72ff 101 * @returns
melse 0:eee9c8ba72ff 102 * A floating point value corresponding to the voltage of V+ (in V).
melse 0:eee9c8ba72ff 103 */
melse 0:eee9c8ba72ff 104 //float read_bus_voltage();
melse 0:eee9c8ba72ff 105
melse 0:eee9c8ba72ff 106 /** Sets the calibration register.
melse 0:eee9c8ba72ff 107 *
melse 0:eee9c8ba72ff 108 * Specifies a maximum bus voltage of 16V and maximum current of 400mA.
melse 0:eee9c8ba72ff 109 *
melse 0:eee9c8ba72ff 110 */
melse 0:eee9c8ba72ff 111 void calibrate_16v_400mA();
melse 0:eee9c8ba72ff 112
melse 0:eee9c8ba72ff 113 protected:
melse 0:eee9c8ba72ff 114 resolution_t resolution;
melse 0:eee9c8ba72ff 115
melse 0:eee9c8ba72ff 116 int i2c_addr;
melse 0:eee9c8ba72ff 117 int current_divider;
melse 0:eee9c8ba72ff 118 int power_divider;
melse 0:eee9c8ba72ff 119 //...
melse 0:eee9c8ba72ff 120
melse 0:eee9c8ba72ff 121 /** Writes a uint8_t array to the specified I2C register.
melse 0:eee9c8ba72ff 122 *
melse 0:eee9c8ba72ff 123 * @see write_register_u16
melse 0:eee9c8ba72ff 124 *
melse 0:eee9c8ba72ff 125 * @param reg (8-Bit) Register address to be written to.
melse 0:eee9c8ba72ff 126 * @param data An array of bytes with the data to be written.
melse 0:eee9c8ba72ff 127 * @param length The length of the array.
melse 0:eee9c8ba72ff 128 */
melse 0:eee9c8ba72ff 129 void write_register(uint8_t reg, uint8_t* data, int length);
melse 0:eee9c8ba72ff 130 /** Writes a uint16_t to the specified I2C register.
melse 0:eee9c8ba72ff 131 *
melse 0:eee9c8ba72ff 132 * @see write_register
melse 0:eee9c8ba72ff 133 *
melse 0:eee9c8ba72ff 134 * @param reg (8-Bit) Register address to be written to.
melse 0:eee9c8ba72ff 135 * @param data (16-Bit) Data to be written.
melse 0:eee9c8ba72ff 136 */
melse 0:eee9c8ba72ff 137 void write_register_u16(uint8_t reg, uint16_t data);
melse 0:eee9c8ba72ff 138 /** Reads a value from the specified I2C register.
melse 0:eee9c8ba72ff 139 *
melse 0:eee9c8ba72ff 140 * @see write_register
melse 0:eee9c8ba72ff 141 * @see write_register_u16
melse 0:eee9c8ba72ff 142 *
melse 0:eee9c8ba72ff 143 * @param reg (8-Bit) Register to be read from.
melse 0:eee9c8ba72ff 144 *
melse 0:eee9c8ba72ff 145 * @returns
melse 0:eee9c8ba72ff 146 * The contents of the specified register, as a 16 bit integer.
melse 0:eee9c8ba72ff 147 */
melse 0:eee9c8ba72ff 148 uint16_t read_register_u16(uint8_t reg);
melse 0:eee9c8ba72ff 149
melse 0:eee9c8ba72ff 150 /** Writes nothing to a specified register. (Used to tell the chip that we want to read from that register)
melse 0:eee9c8ba72ff 151 *
melse 0:eee9c8ba72ff 152 * @see read_register_u16
melse 0:eee9c8ba72ff 153 *
melse 0:eee9c8ba72ff 154 * @param reg The register for nothing to be written to.
melse 0:eee9c8ba72ff 155 */
melse 0:eee9c8ba72ff 156 void write_null(uint8_t reg);
melse 0:eee9c8ba72ff 157 };