Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
INA219.h
00001 /**************************************************************************/ 00002 /*! 00003 @file Adafruit_INA219.h 00004 @author K. Townsend (Adafruit Industries) 00005 @license BSD (see license.txt) 00006 00007 This is a library for the Adafruit INA219 breakout board 00008 ----> https://www.adafruit.com/products/??? 00009 00010 Adafruit invests time and resources providing this open source code, 00011 please support Adafruit and open-source hardware by purchasing 00012 products from Adafruit! 00013 @section HISTORY 00014 v1.0 - First release 00015 */ 00016 /**************************************************************************/ 00017 00018 #ifndef INA219_H 00019 #define INA219_H 00020 00021 //#if ARDUINO >= 100 00022 // #include "Arduino.h" 00023 //#else 00024 // #include "WProgram.h" 00025 //#endif 00026 00027 //#include <Wire.h> 00028 #include "mbed.h" 00029 00030 /*========================================================================= 00031 I2C ADDRESS/BITS 00032 -----------------------------------------------------------------------*/ 00033 #define INA219_ADDRESS (0x40) // 1000000 (A0+A1=GND) 00034 #define INA219_READ (0x01) 00035 /*=========================================================================*/ 00036 00037 /*========================================================================= 00038 CONFIG REGISTER (R/W) 00039 -----------------------------------------------------------------------*/ 00040 #define INA219_REG_CONFIG (0x00) 00041 /*---------------------------------------------------------------------*/ 00042 #define INA219_CONFIG_RESET (0x8000) // Reset Bit 00043 00044 #define INA219_CONFIG_BVOLTAGERANGE_MASK (0x2000) // Bus Voltage Range Mask 00045 #define INA219_CONFIG_BVOLTAGERANGE_16V (0x0000) // 0-16V Range 00046 #define INA219_CONFIG_BVOLTAGERANGE_32V (0x2000) // 0-32V Range 00047 00048 #define INA219_CONFIG_GAIN_MASK (0x1800) // Gain Mask 00049 #define INA219_CONFIG_GAIN_1_40MV (0x0000) // Gain 1, 40mV Range 00050 #define INA219_CONFIG_GAIN_2_80MV (0x0800) // Gain 2, 80mV Range 00051 #define INA219_CONFIG_GAIN_4_160MV (0x1000) // Gain 4, 160mV Range 00052 #define INA219_CONFIG_GAIN_8_320MV (0x1800) // Gain 8, 320mV Range 00053 00054 #define INA219_CONFIG_BADCRES_MASK (0x0780) // Bus ADC Resolution Mask 00055 #define INA219_CONFIG_BADCRES_9BIT (0x0080) // 9-bit bus res = 0..511 00056 #define INA219_CONFIG_BADCRES_10BIT (0x0100) // 10-bit bus res = 0..1023 00057 #define INA219_CONFIG_BADCRES_11BIT (0x0200) // 11-bit bus res = 0..2047 00058 #define INA219_CONFIG_BADCRES_12BIT (0x0400) // 12-bit bus res = 0..4097 00059 00060 #define INA219_CONFIG_SADCRES_MASK (0x0078) // Shunt ADC Resolution and Averaging Mask 00061 #define INA219_CONFIG_SADCRES_9BIT_1S_84US (0x0000) // 1 x 9-bit shunt sample 00062 #define INA219_CONFIG_SADCRES_10BIT_1S_148US (0x0008) // 1 x 10-bit shunt sample 00063 #define INA219_CONFIG_SADCRES_11BIT_1S_276US (0x0010) // 1 x 11-bit shunt sample 00064 #define INA219_CONFIG_SADCRES_12BIT_1S_532US (0x0018) // 1 x 12-bit shunt sample 00065 #define INA219_CONFIG_SADCRES_12BIT_2S_1060US (0x0048) // 2 x 12-bit shunt samples averaged together 00066 #define INA219_CONFIG_SADCRES_12BIT_4S_2130US (0x0050) // 4 x 12-bit shunt samples averaged together 00067 #define INA219_CONFIG_SADCRES_12BIT_8S_4260US (0x0058) // 8 x 12-bit shunt samples averaged together 00068 #define INA219_CONFIG_SADCRES_12BIT_16S_8510US (0x0060) // 16 x 12-bit shunt samples averaged together 00069 #define INA219_CONFIG_SADCRES_12BIT_32S_17MS (0x0068) // 32 x 12-bit shunt samples averaged together 00070 #define INA219_CONFIG_SADCRES_12BIT_64S_34MS (0x0070) // 64 x 12-bit shunt samples averaged together 00071 #define INA219_CONFIG_SADCRES_12BIT_128S_69MS (0x0078) // 128 x 12-bit shunt samples averaged together 00072 00073 #define INA219_CONFIG_MODE_MASK (0x0007) // Operating Mode Mask 00074 #define INA219_CONFIG_MODE_POWERDOWN (0x0000) 00075 #define INA219_CONFIG_MODE_SVOLT_TRIGGERED (0x0001) 00076 #define INA219_CONFIG_MODE_BVOLT_TRIGGERED (0x0002) 00077 #define INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED (0x0003) 00078 #define INA219_CONFIG_MODE_ADCOFF (0x0004) 00079 #define INA219_CONFIG_MODE_SVOLT_CONTINUOUS (0x0005) 00080 #define INA219_CONFIG_MODE_BVOLT_CONTINUOUS (0x0006) 00081 #define INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007) 00082 /*=========================================================================*/ 00083 00084 /*========================================================================= 00085 SHUNT VOLTAGE REGISTER (R) 00086 -----------------------------------------------------------------------*/ 00087 #define INA219_REG_SHUNTVOLTAGE (0x01) 00088 /*=========================================================================*/ 00089 00090 /*========================================================================= 00091 BUS VOLTAGE REGISTER (R) 00092 -----------------------------------------------------------------------*/ 00093 #define INA219_REG_BUSVOLTAGE (0x02) 00094 /*=========================================================================*/ 00095 00096 /*========================================================================= 00097 POWER REGISTER (R) 00098 -----------------------------------------------------------------------*/ 00099 #define INA219_REG_POWER (0x03) 00100 /*=========================================================================*/ 00101 00102 /*========================================================================= 00103 CURRENT REGISTER (R) 00104 -----------------------------------------------------------------------*/ 00105 #define INA219_REG_CURRENT (0x04) 00106 /*=========================================================================*/ 00107 00108 /*========================================================================= 00109 CALIBRATION REGISTER (R/W) 00110 -----------------------------------------------------------------------*/ 00111 #define INA219_REG_CALIBRATION (0x05) 00112 /*=========================================================================*/ 00113 00114 class Adafruit_INA219{ 00115 public: 00116 Adafruit_INA219(PinName sda, PinName scl, uint8_t addr = INA219_ADDRESS); 00117 void begin(void); 00118 float getBusVoltage_V(void); 00119 float getShuntVoltage_mV(void); 00120 float getCurrent_mA(void); 00121 00122 private: 00123 I2C ina219_i2c; 00124 uint8_t ina219_i2caddr; 00125 uint32_t ina219_calValue; 00126 // The following multipliers are used to convert raw current and power 00127 // values to mA and mW, taking into account the current config settings 00128 uint32_t ina219_currentDivider_mA; 00129 uint32_t ina219_powerDivider_mW; 00130 00131 void wireWriteRegister(uint8_t reg, uint16_t value); 00132 void wireReadRegister(uint8_t reg, uint16_t *value); 00133 void ina219SetCalibration_32V_2A(void); 00134 void ina219SetCalibration_32V_1A(void); 00135 void ina219SetCalibration_16V_400mA(void); 00136 int16_t getBusVoltage_raw(void); 00137 int16_t getShuntVoltage_raw(void); 00138 int16_t getCurrent_raw(void); 00139 }; 00140 00141 #endif // INA219_H
Generated on Thu Jul 21 2022 21:11:50 by
1.7.2