A library for the INA219 current sensing breakout from adafruit.

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers INA219.hpp Source File

INA219.hpp

00001 #include "mbed.h"
00002 #include "INA219_consts.h"
00003 
00004 #define DEBUG
00005 
00006 /// Represents the different supported voltage levels
00007 typedef enum {
00008     BUS_16V=16,
00009     BUS_32V=32
00010 } busvoltage_t;
00011 
00012 /** @enum Resolution typedef
00013  *  @brief Represents the different ADC resolutions supported by the INA219.
00014  */
00015 typedef enum {
00016     RES_12BITS=12,  ///< 12 bit precision (532us)
00017     RES_11BITS=11,  ///< 11 bit precision (276us)
00018     RES_10BITS=10,  ///< 10 bit precision (148us)
00019     RES_9BITS =9    ///< 9 bit precision (84us)
00020 } resolution_t;
00021 
00022 /** @class INA219
00023  *  @brief A class to represent the INA219 breakout board from Adafruit.
00024  */
00025 class INA219: protected I2C
00026 {
00027 public:
00028     /** Creates an instance of the INA219 device.
00029      *
00030      *  @param sda An I2C data pin.
00031      *  @param scl An I2C clock pin.
00032      *  @param addr The I2C address of the INA219. (Default = 0x40)
00033      *  @param freq The I2C clock frequency.
00034      *  @param res The desired resolution of the INA219's ADC.
00035      */
00036     INA219 (PinName sda, PinName scl, int addr=0x40, int freq=100000, resolution_t res=RES_12BITS);
00037 
00038     /** Reads the current raw value of current from the INA219.
00039      *
00040      *  @see read_current_mA
00041      *
00042      *  @returns
00043      *      A value between -32768 and +32768. Depending on the current calibration and configuration register values, the corresponding current can be calculated.
00044      */
00045     int16_t read_current_raw();
00046     /** Reads the current from the INA219 and calculates the actual value in mA.
00047      *
00048      *  @see read_current_raw
00049      *
00050      *  @returns
00051      *      A floating point value corresponding to the current flowing through the current shunt, in mA.
00052      */
00053     float read_current_mA();
00054 
00055     /** Reads the raw power value from the INA219.
00056      *
00057      *  @see read_power_mW
00058      *
00059      *  @returns
00060      *      A value between -32768 and +32768. Depending on the calibration and configuration register values, the actual power can be calculated.
00061      */
00062     //int16_t read_power_raw();
00063     /** Reads the power from the INA219 and calculates the actual value in mW.
00064      *
00065      *  @see read_power_raw
00066      *
00067      *  @returns
00068      *      A floating point value corresponding to the power being used in the circuit, in mW.
00069      */
00070     //float read_power_mW();
00071 
00072     /** Reads the raw shunt voltage value from the INA219.
00073      *
00074      *  @see read_shunt_voltage_mV
00075      *
00076      *  @returns
00077      *      A value between -32768 and +32768. Depending on the calibration and configuration register values, the actual shunt voltage can be calculated.
00078      */
00079     //int16_t read_shunt_voltage_raw();
00080     /** Reads the shunt voltage from the INA219 and calculates the actual value in mV.
00081      *
00082      *  @see read_shunt_voltage_raw
00083      *
00084      *  @returns
00085      *      A floating point value corresponding to the potential difference across the current shunt, in mV.
00086      */
00087     //float read_shunt_voltage_mV();
00088 
00089     /** Reads the raw bus voltage.
00090      *
00091      *  @see read_bus_voltage
00092      *
00093      *  @returns
00094      *      A value between -32768 and +32768 corresponding to the bus voltage.
00095      */
00096     //int16_t read_bus_voltage_raw();
00097     /** Reads the bus voltage and uses it to calculate the actual bus voltage.
00098      *
00099      *  @see read_bus_voltage_raw
00100      *
00101      *  @returns
00102      *      A floating point value corresponding to the voltage of V+ (in V).
00103      */
00104     //float read_bus_voltage();
00105 
00106     /** Sets the calibration register.
00107      *     
00108      *  Specifies a maximum bus voltage of 16V and maximum current of 400mA.
00109      *  
00110      */
00111     void calibrate_16v_400mA();
00112 
00113 protected:
00114     resolution_t resolution;
00115 
00116     int i2c_addr;
00117     int current_divider;
00118     int power_divider;
00119     //...
00120 
00121     /** Writes a uint8_t array to the specified I2C register.
00122      *
00123      *  @see write_register_u16
00124      *
00125      *  @param reg (8-Bit) Register address to be written to.
00126      *  @param data An array of bytes with the data to be written.
00127      *  @param length The length of the array.
00128      */
00129     void write_register(uint8_t reg, uint8_t* data, int length);
00130     /** Writes a uint16_t to the specified I2C register.
00131      *
00132      *  @see write_register
00133      *
00134      *  @param reg (8-Bit) Register address to be written to.
00135      *  @param data (16-Bit) Data to be written.
00136      */
00137     void write_register_u16(uint8_t reg, uint16_t data);
00138     /** Reads a value from the specified I2C register.
00139      *
00140      *  @see write_register
00141      *  @see write_register_u16
00142      *
00143      *  @param reg (8-Bit) Register to be read from.
00144      *
00145      *  @returns
00146      *      The contents of the specified register, as a 16 bit integer.
00147      */
00148     uint16_t read_register_u16(uint8_t reg);
00149     
00150     /** Writes nothing to a specified register. (Used to tell the chip that we want to read from that register)
00151      *
00152      *  @see read_register_u16
00153      *
00154      *  @param reg The register for nothing to be written to.
00155      */
00156      void write_null(uint8_t reg);
00157 };