ina219.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. INA219.cpp - Class file for the INA219 Zero-Drift, Bi-directional Current/Power Monitor Arduino Library.
  3. Version: 1.0.0
  4. (c) 2014 Korneliusz Jarzebski
  5. www.jarzebski.pl
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the version 3 GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "INA219.h"
  17. #include "i2c.h"
  18. static uint16_t currentLSB, powerLSB;
  19. static uint16_t vShuntMax; // millivolt
  20. static uint16_t vBusMax; // millivolt
  21. static uint16_t rShunt; // milliohms
  22. static uint8_t flagCNVR; // Conversion Ready
  23. static uint8_t flagOVF; // Math Overflow Flag
  24. static t_i2c_status status = I2C_SUCCESS;
  25. /**
  26. * Configure I2C, INA219
  27. */
  28. void INA219_Config(void) {
  29. uint16_t config;
  30. uint16_t calibrationValue;
  31. vBusMax = 32000; // !!! Maximum input voltage only 26V !!!
  32. vShuntMax = 80; // !! for shunt 10 mOhm and maximum 8A input current
  33. rShunt = CURRENT_SHUNT_RESISTANCE; // in mOhm
  34. /** INA219 configure */
  35. config = INA219_RESET_OFF; // INA219_RST
  36. config |= INA219_RANGE_32V; // INA219_BVR
  37. config |= INA219_GAIN_80MV; // INA219_PG
  38. config |= INA219_BUS_RES_12BIT_128S; // INA219_BADC
  39. config |= INA219_SHUNT_RES_12BIT_128S; //INA219_SADC
  40. config |= INA219_MODE_SHUNT_BUS_CONT; //INA219_MODE
  41. i2c_wr_reg(INA219_ADDRESS, INA219_REG_CONFIG, config);
  42. /**
  43. * ôîðìóëû èç ìàíóàëà:
  44. * Current_LSB = Maximum Expected Current / 2^15
  45. * Power_LSB = 20 * Current_LSB
  46. * Calibration_Value = trunc( 0.04096 / (Current_LSB * Rshunt) )
  47. */
  48. currentLSB = 244; // 8000000 uA / 32768
  49. powerLSB = 4880;
  50. calibrationValue = 16777; // 0.04096 / ((8/32768) * 0.01)
  51. i2c_wr_reg(INA219_ADDRESS, INA219_REG_CALIBRATION, calibrationValue);
  52. }
  53. /** */
  54. uint16_t getMaxPossibleCurrent(void) {
  55. return (((1000 * vShuntMax) + (rShunt>>1)) / rShunt);
  56. }
  57. /** */
  58. uint16_t getMaxCurrent(void) {
  59. uint16_t maxCurrent = ((currentLSB * 32767) + 500) / 1000;
  60. uint16_t maxPossible = getMaxPossibleCurrent();
  61. if (maxCurrent > maxPossible) {
  62. return maxPossible;
  63. } else {
  64. return maxCurrent;
  65. }
  66. }
  67. /** */
  68. uint16_t getMaxShuntVoltage(void) {
  69. uint16_t maxVoltage = ((getMaxCurrent() * rShunt) + 500) / 1000;
  70. if (maxVoltage >= vShuntMax) {
  71. return vShuntMax;
  72. } else {
  73. return maxVoltage;
  74. }
  75. }
  76. /** */
  77. uint32_t getMaxPower(void) {
  78. return (((getMaxCurrent() * vBusMax) + 500000) / 1000000);
  79. }
  80. /** */
  81. uint16_t readBusCurrent(void) {
  82. uint16_t current;
  83. int16_t tmp;
  84. status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_CURRENT, (uint16_t *)&tmp);
  85. if (status != I2C_SUCCESS) {
  86. return 9999;
  87. }
  88. if (tmp < 0) {
  89. tmp = - tmp;
  90. }
  91. current = (uint16_t)(((tmp * currentLSB) + 500) / 1000);
  92. return current;
  93. }
  94. /** */
  95. uint32_t readBusPower(void) {
  96. uint16_t power;
  97. i2c_rd_reg(INA219_ADDRESS, INA219_REG_POWER, &power);
  98. return (((power * powerLSB) + 500) / 1000);
  99. }
  100. /**
  101. * Currently return raw value of shunt voltage in 10 uV
  102. */
  103. int16_t readShuntVoltage(void) {
  104. uint16_t shvolt;
  105. status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_SHUNTVOLTAGE, &shvolt);
  106. if (status != I2C_SUCCESS) {
  107. return 999;
  108. }
  109. return shvolt;
  110. }
  111. /**
  112. * Return bus voltage in mV
  113. */
  114. uint16_t readBusVoltage(void) {
  115. uint16_t volt;
  116. flagCNVR = 0;
  117. flagOVF = 0;
  118. status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_BUSVOLTAGE, &volt);
  119. if (status != I2C_SUCCESS) {
  120. return 65535;
  121. }
  122. if ((volt & 0x0001) != 0) {
  123. flagOVF = 1;
  124. }
  125. if ((volt & 0x0002) != 0) {
  126. flagCNVR = 1;
  127. }
  128. return ((volt >> 3) * 4);
  129. }
  130. ina219_bvr_t getRange(void) {
  131. uint16_t value;
  132. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  133. value &= 0x2000;
  134. value >>= 13;
  135. return (ina219_bvr_t)value;
  136. }
  137. ina219_pg_t getGain(void) {
  138. uint16_t value;
  139. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  140. value &= 0x1800;
  141. value >>= 11;
  142. return (ina219_pg_t)value;
  143. }
  144. ina219_badc_t getBusRes(void) {
  145. uint16_t value;
  146. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  147. value &= 0x0780;
  148. value >>= 7;
  149. return (ina219_badc_t)value;
  150. }
  151. ina219_sadc_t getShuntRes(void) {
  152. uint16_t value;
  153. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  154. value &= 0x0078;
  155. value >>= 3;
  156. return (ina219_sadc_t)value;
  157. }
  158. ina219_mode_t getMode(void) {
  159. uint16_t value;
  160. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  161. value &= 0x0007;
  162. return (ina219_mode_t)value;
  163. }