A library for the INA219 current sensing breakout from adafruit.

Dependents:   INA219-HelloWorld INA_219 Light example-ublox-cellular-psm ... more

Files at this revision

API Documentation at this revision

Comitter:
melse
Date:
Thu Aug 28 10:32:20 2014 +0000
Commit message:
Initial Commit

Changed in this revision

INA219.cpp Show annotated file Show diff for this revision Revisions of this file
INA219.hpp Show annotated file Show diff for this revision Revisions of this file
INA219_consts.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/INA219.cpp	Thu Aug 28 10:32:20 2014 +0000
@@ -0,0 +1,88 @@
+#include "INA219.hpp"
+
+INA219::INA219 (PinName sda, PinName scl, int addr, int freq, resolution_t res) : I2C(sda, scl), resolution(res), i2c_addr(addr << 1)
+{
+    I2C::frequency(freq);
+    
+    // by default, calibrate to this level.
+    calibrate_16v_400mA();
+}
+
+// Private Methods
+
+void INA219::write_register (uint8_t reg, uint8_t* data, int length)
+{
+    char* transmission = (char*)malloc(length + 1);
+    memcpy(transmission + 1, data, length);
+
+    transmission[0] = reg;
+    I2C::write(i2c_addr, transmission, length + 1);
+
+    free(transmission);
+}
+
+void INA219::write_register_u16 (uint8_t reg, uint16_t data)
+{
+    char transmission[3];
+    transmission[0] = reg;
+    transmission[1] = (data >> 8) & 0xff;
+    transmission[2] = data & 0xff;
+
+    I2C::write(i2c_addr, transmission, 3);
+}
+
+void INA219::write_null(uint8_t reg) {
+    I2C::write(i2c_addr, (char*)&reg, 1);    
+}
+
+uint16_t INA219::read_register_u16 (uint8_t reg)
+{
+    write_null(reg);
+
+    char data[2];
+    I2C::read(i2c_addr, data, 2);
+
+    uint16_t ret_val = data[0] << 8 | data[1];
+    return ret_val;
+}
+
+// Public Methods
+
+void INA219::calibrate_16v_400mA()
+{
+    // ASSUMING A 0.1 OHM RESISTOR!
+    write_register_u16(INA219_REG_CALIBRATION, 8192);
+
+    // Write to config register
+
+    uint16_t resolution_mask = 0x0000;
+    
+    if (resolution == RES_12BITS)
+        resolution_mask = INA219_CONFIG_BADCRES_12BIT | INA219_CONFIG_SADCRES_12BIT_1S_532US;
+    else if (resolution == RES_11BITS)
+        resolution_mask = INA219_CONFIG_BADCRES_11BIT | INA219_CONFIG_SADCRES_11BIT_1S_276US;
+    else if (resolution == RES_10BITS)
+        resolution_mask = INA219_CONFIG_BADCRES_10BIT | INA219_CONFIG_SADCRES_10BIT_1S_148US;
+    else // resolution == RES_9BITS
+        resolution_mask = INA219_CONFIG_BADCRES_9BIT | INA219_CONFIG_SADCRES_9BIT_1S_84US;
+
+    write_register_u16(INA219_REG_CONFIG, INA219_CONFIG_BVOLTAGERANGE_16V |
+                    INA219_CONFIG_GAIN_1_40MV |
+                    resolution_mask |
+                    INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS);
+                    
+    // Set current divider
+    current_divider = 20;
+    power_divider = 1;
+}
+
+int16_t INA219::read_current_raw()
+{
+    return (int16_t)read_register_u16(INA219_REG_CURRENT);
+}
+
+float INA219::read_current_mA()
+{
+    float raw_current = read_current_raw();
+    return raw_current / current_divider;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/INA219.hpp	Thu Aug 28 10:32:20 2014 +0000
@@ -0,0 +1,157 @@
+#include "mbed.h"
+#include "INA219_consts.h"
+
+#define DEBUG
+
+/// Represents the different supported voltage levels
+typedef enum {
+    BUS_16V=16,
+    BUS_32V=32
+} busvoltage_t;
+
+/** @enum Resolution typedef
+ *  @brief Represents the different ADC resolutions supported by the INA219.
+ */
+typedef enum {
+    RES_12BITS=12,  ///< 12 bit precision (532us)
+    RES_11BITS=11,  ///< 11 bit precision (276us)
+    RES_10BITS=10,  ///< 10 bit precision (148us)
+    RES_9BITS =9    ///< 9 bit precision (84us)
+} resolution_t;
+
+/** @class INA219
+ *  @brief A class to represent the INA219 breakout board from Adafruit.
+ */
+class INA219: protected I2C
+{
+public:
+    /** Creates an instance of the INA219 device.
+     *
+     *  @param sda An I2C data pin.
+     *  @param scl An I2C clock pin.
+     *  @param addr The I2C address of the INA219. (Default = 0x40)
+     *  @param freq The I2C clock frequency.
+     *  @param res The desired resolution of the INA219's ADC.
+     */
+    INA219 (PinName sda, PinName scl, int addr=0x40, int freq=100000, resolution_t res=RES_12BITS);
+
+    /** Reads the current raw value of current from the INA219.
+     *
+     *  @see read_current_mA
+     *
+     *  @returns
+     *      A value between -32768 and +32768. Depending on the current calibration and configuration register values, the corresponding current can be calculated.
+     */
+    int16_t read_current_raw();
+    /** Reads the current from the INA219 and calculates the actual value in mA.
+     *
+     *  @see read_current_raw
+     *
+     *  @returns
+     *      A floating point value corresponding to the current flowing through the current shunt, in mA.
+     */
+    float read_current_mA();
+
+    /** Reads the raw power value from the INA219.
+     *
+     *  @see read_power_mW
+     *
+     *  @returns
+     *      A value between -32768 and +32768. Depending on the calibration and configuration register values, the actual power can be calculated.
+     */
+    //int16_t read_power_raw();
+    /** Reads the power from the INA219 and calculates the actual value in mW.
+     *
+     *  @see read_power_raw
+     *
+     *  @returns
+     *      A floating point value corresponding to the power being used in the circuit, in mW.
+     */
+    //float read_power_mW();
+
+    /** Reads the raw shunt voltage value from the INA219.
+     *
+     *  @see read_shunt_voltage_mV
+     *
+     *  @returns
+     *      A value between -32768 and +32768. Depending on the calibration and configuration register values, the actual shunt voltage can be calculated.
+     */
+    //int16_t read_shunt_voltage_raw();
+    /** Reads the shunt voltage from the INA219 and calculates the actual value in mV.
+     *
+     *  @see read_shunt_voltage_raw
+     *
+     *  @returns
+     *      A floating point value corresponding to the potential difference across the current shunt, in mV.
+     */
+    //float read_shunt_voltage_mV();
+
+    /** Reads the raw bus voltage.
+     *
+     *  @see read_bus_voltage
+     *
+     *  @returns
+     *      A value between -32768 and +32768 corresponding to the bus voltage.
+     */
+    //int16_t read_bus_voltage_raw();
+    /** Reads the bus voltage and uses it to calculate the actual bus voltage.
+     *
+     *  @see read_bus_voltage_raw
+     *
+     *  @returns
+     *      A floating point value corresponding to the voltage of V+ (in V).
+     */
+    //float read_bus_voltage();
+
+    /** Sets the calibration register.
+     *     
+     *  Specifies a maximum bus voltage of 16V and maximum current of 400mA.
+     *  
+     */
+    void calibrate_16v_400mA();
+
+protected:
+    resolution_t resolution;
+
+    int i2c_addr;
+    int current_divider;
+    int power_divider;
+    //...
+
+    /** Writes a uint8_t array to the specified I2C register.
+     *
+     *  @see write_register_u16
+     *
+     *  @param reg (8-Bit) Register address to be written to.
+     *  @param data An array of bytes with the data to be written.
+     *  @param length The length of the array.
+     */
+    void write_register(uint8_t reg, uint8_t* data, int length);
+    /** Writes a uint16_t to the specified I2C register.
+     *
+     *  @see write_register
+     *
+     *  @param reg (8-Bit) Register address to be written to.
+     *  @param data (16-Bit) Data to be written.
+     */
+    void write_register_u16(uint8_t reg, uint16_t data);
+    /** Reads a value from the specified I2C register.
+     *
+     *  @see write_register
+     *  @see write_register_u16
+     *
+     *  @param reg (8-Bit) Register to be read from.
+     *
+     *  @returns
+     *      The contents of the specified register, as a 16 bit integer.
+     */
+    uint16_t read_register_u16(uint8_t reg);
+    
+    /** Writes nothing to a specified register. (Used to tell the chip that we want to read from that register)
+     *
+     *  @see read_register_u16
+     *
+     *  @param reg The register for nothing to be written to.
+     */
+     void write_null(uint8_t reg);
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/INA219_consts.h	Thu Aug 28 10:32:20 2014 +0000
@@ -0,0 +1,91 @@
+/*
+    'Borrowed' from Adafruit's INA219 libarary. Licensed under the BSD license.
+    
+    https://github.com/adafruit/Adafruit_INA219
+*/
+
+// Hopefully Adafruit won't mind if I borrow this...
+
+/*=========================================================================
+    I2C ADDRESS/BITS
+    -----------------------------------------------------------------------*/
+#define INA219_ADDRESS                         (0x40)    // 1000000 (A0+A1=GND)
+#define INA219_READ                            (0x01)
+/*=========================================================================*/
+
+/*=========================================================================
+    CONFIG REGISTER (R/W)
+    -----------------------------------------------------------------------*/
+#define INA219_REG_CONFIG                      (0x00)
+/*---------------------------------------------------------------------*/
+#define INA219_CONFIG_RESET                    (0x8000)  // Reset Bit
+
+#define INA219_CONFIG_BVOLTAGERANGE_MASK       (0x2000)  // Bus Voltage Range Mask
+#define INA219_CONFIG_BVOLTAGERANGE_16V        (0x0000)  // 0-16V Range
+#define INA219_CONFIG_BVOLTAGERANGE_32V        (0x2000)  // 0-32V Range
+
+#define INA219_CONFIG_GAIN_MASK                (0x1800)  // Gain Mask
+#define INA219_CONFIG_GAIN_1_40MV              (0x0000)  // Gain 1, 40mV Range
+#define INA219_CONFIG_GAIN_2_80MV              (0x0800)  // Gain 2, 80mV Range
+#define INA219_CONFIG_GAIN_4_160MV             (0x1000)  // Gain 4, 160mV Range
+#define INA219_CONFIG_GAIN_8_320MV             (0x1800)  // Gain 8, 320mV Range
+
+#define INA219_CONFIG_BADCRES_MASK             (0x0780)  // Bus ADC Resolution Mask
+#define INA219_CONFIG_BADCRES_9BIT             (0x0080)  // 9-bit bus res = 0..511
+#define INA219_CONFIG_BADCRES_10BIT            (0x0100)  // 10-bit bus res = 0..1023
+#define INA219_CONFIG_BADCRES_11BIT            (0x0200)  // 11-bit bus res = 0..2047
+#define INA219_CONFIG_BADCRES_12BIT            (0x0400)  // 12-bit bus res = 0..4097
+
+#define INA219_CONFIG_SADCRES_MASK             (0x0078)  // Shunt ADC Resolution and Averaging Mask
+#define INA219_CONFIG_SADCRES_9BIT_1S_84US     (0x0000)  // 1 x 9-bit shunt sample
+#define INA219_CONFIG_SADCRES_10BIT_1S_148US   (0x0008)  // 1 x 10-bit shunt sample
+#define INA219_CONFIG_SADCRES_11BIT_1S_276US   (0x0010)  // 1 x 11-bit shunt sample
+#define INA219_CONFIG_SADCRES_12BIT_1S_532US   (0x0018)  // 1 x 12-bit shunt sample
+#define INA219_CONFIG_SADCRES_12BIT_2S_1060US  (0x0048)  // 2 x 12-bit shunt samples averaged together
+#define INA219_CONFIG_SADCRES_12BIT_4S_2130US  (0x0050)  // 4 x 12-bit shunt samples averaged together
+#define INA219_CONFIG_SADCRES_12BIT_8S_4260US  (0x0058)  // 8 x 12-bit shunt samples averaged together
+#define INA219_CONFIG_SADCRES_12BIT_16S_8510US (0x0060)  // 16 x 12-bit shunt samples averaged together
+#define INA219_CONFIG_SADCRES_12BIT_32S_17MS   (0x0068)  // 32 x 12-bit shunt samples averaged together
+#define INA219_CONFIG_SADCRES_12BIT_64S_34MS   (0x0070)  // 64 x 12-bit shunt samples averaged together
+#define INA219_CONFIG_SADCRES_12BIT_128S_69MS  (0x0078)  // 128 x 12-bit shunt samples averaged together
+
+#define INA219_CONFIG_MODE_MASK                (0x0007)  // Operating Mode Mask
+#define INA219_CONFIG_MODE_POWERDOWN           (0x0000)
+#define INA219_CONFIG_MODE_SVOLT_TRIGGERED     (0x0001)
+#define INA219_CONFIG_MODE_BVOLT_TRIGGERED     (0x0002)
+#define INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED (0x0003)
+#define INA219_CONFIG_MODE_ADCOFF              (0x0004)
+#define INA219_CONFIG_MODE_SVOLT_CONTINUOUS    (0x0005)
+#define INA219_CONFIG_MODE_BVOLT_CONTINUOUS    (0x0006)
+#define INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007)
+/*=========================================================================*/
+
+/*=========================================================================
+    SHUNT VOLTAGE REGISTER (R)
+    -----------------------------------------------------------------------*/
+#define INA219_REG_SHUNTVOLTAGE                (0x01)
+/*=========================================================================*/
+
+/*=========================================================================
+    BUS VOLTAGE REGISTER (R)
+    -----------------------------------------------------------------------*/
+#define INA219_REG_BUSVOLTAGE                  (0x02)
+/*=========================================================================*/
+
+/*=========================================================================
+    POWER REGISTER (R)
+    -----------------------------------------------------------------------*/
+#define INA219_REG_POWER                       (0x03)
+/*=========================================================================*/
+
+/*=========================================================================
+    CURRENT REGISTER (R)
+    -----------------------------------------------------------------------*/
+#define INA219_REG_CURRENT                     (0x04)
+/*=========================================================================*/
+
+/*=========================================================================
+    CALIBRATION REGISTER (R/W)
+    -----------------------------------------------------------------------*/
+#define INA219_REG_CALIBRATION                 (0x05)
+/*=========================================================================*/
\ No newline at end of file