ina219.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 vShuntMax; // millivolt
  19. static uint16_t vBusMax; // millivolt
  20. static uint16_t rShunt; // milliohms
  21. static uint8_t flagCNVR; // Conversion Ready
  22. static uint8_t flagOVF; // Math Overflow Flag
  23. static t_i2c_status status = I2C_SUCCESS;
  24. /**
  25. * Configure I2C, INA219
  26. */
  27. void INA219_Config(void) {
  28. uint16_t config;
  29. vBusMax = 32000; // !!! Maximum input voltage only 26V !!!
  30. vShuntMax = 80; // !! for shunt 10 mOhm and maximum 8A input current
  31. rShunt = CURRENT_SHUNT_RESISTANCE; // in mOhm
  32. /** INA219 configure */
  33. config = INA219_RESET_OFF; // INA219_RST
  34. config |= INA219_RANGE_32V; // INA219_BVR
  35. config |= INA219_GAIN_80MV; // INA219_PG
  36. config |= INA219_BUS_RES_12BIT_128S; // INA219_BADC
  37. config |= INA219_SHUNT_RES_12BIT_128S; //INA219_SADC
  38. config |= INA219_MODE_SHUNT_BUS_CONT; //INA219_MODE
  39. i2c_wr_reg(INA219_ADDRESS, INA219_REG_CONFIG, config);
  40. i2c_wr_reg(INA219_ADDRESS, INA219_REG_CALIBRATION, CALIBRATION_VALUE);
  41. }
  42. /** */
  43. uint16_t getMaxPossibleCurrent(void) {
  44. return (((1000 * vShuntMax) + (rShunt>>1)) / rShunt);
  45. }
  46. /** */
  47. uint16_t getMaxCurrent(void) {
  48. uint16_t maxCurrent = ((CURRENT_LSB * 32767) + 500) / 1000;
  49. uint16_t maxPossible = getMaxPossibleCurrent();
  50. if (maxCurrent > maxPossible) {
  51. return maxPossible;
  52. } else {
  53. return maxCurrent;
  54. }
  55. }
  56. /** */
  57. uint16_t getMaxShuntVoltage(void) {
  58. uint16_t maxVoltage = ((getMaxCurrent() * rShunt) + 500) / 1000;
  59. if (maxVoltage >= vShuntMax) {
  60. return vShuntMax;
  61. } else {
  62. return maxVoltage;
  63. }
  64. }
  65. /** */
  66. uint32_t getMaxPower(void) {
  67. return (((getMaxCurrent() * vBusMax) + 500000) / 1000000);
  68. }
  69. /** */
  70. uint16_t readBusCurrent(void) {
  71. uint32_t current;;
  72. int16_t tmp;
  73. status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_CURRENT, (uint16_t *)&tmp);
  74. if (status != I2C_SUCCESS) {
  75. return 9999;
  76. }
  77. if (tmp < 0) {
  78. tmp = - tmp;
  79. }
  80. current = tmp;
  81. current *= CURRENT_LSB;
  82. current += 500;
  83. current /= 1000;
  84. return (uint16_t)current;
  85. }
  86. /** */
  87. uint32_t readBusPower(void) {
  88. uint32_t power;
  89. uint16_t tmp;
  90. i2c_rd_reg(INA219_ADDRESS, INA219_REG_POWER, &tmp);
  91. power = tmp;
  92. power *= POWER_LSB;
  93. power += 500;
  94. power /= 1000;
  95. return power;
  96. }
  97. /**
  98. * Currently return raw value of shunt voltage in 10 uV
  99. */
  100. int16_t readShuntVoltage(void) {
  101. uint16_t shvolt;
  102. status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_SHUNTVOLTAGE, &shvolt);
  103. if (status != I2C_SUCCESS) {
  104. return 999;
  105. }
  106. return shvolt;
  107. }
  108. /**
  109. * Return bus voltage in mV
  110. */
  111. uint16_t readBusVoltage(void) {
  112. uint16_t volt;
  113. flagCNVR = 0;
  114. flagOVF = 0;
  115. status = i2c_rd_reg(INA219_ADDRESS, INA219_REG_BUSVOLTAGE, &volt);
  116. if (status != I2C_SUCCESS) {
  117. return 65535;
  118. }
  119. if ((volt & 0x0001) != 0) {
  120. flagOVF = 1;
  121. }
  122. if ((volt & 0x0002) != 0) {
  123. flagCNVR = 1;
  124. }
  125. return ((volt >> 3) * 4);
  126. }
  127. ina219_bvr_t getRange(void) {
  128. uint16_t value;
  129. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  130. value &= 0x2000;
  131. value >>= 13;
  132. return (ina219_bvr_t)value;
  133. }
  134. ina219_pg_t getGain(void) {
  135. uint16_t value;
  136. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  137. value &= 0x1800;
  138. value >>= 11;
  139. return (ina219_pg_t)value;
  140. }
  141. ina219_badc_t getBusRes(void) {
  142. uint16_t value;
  143. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  144. value &= 0x0780;
  145. value >>= 7;
  146. return (ina219_badc_t)value;
  147. }
  148. ina219_sadc_t getShuntRes(void) {
  149. uint16_t value;
  150. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  151. value &= 0x0078;
  152. value >>= 3;
  153. return (ina219_sadc_t)value;
  154. }
  155. ina219_mode_t getMode(void) {
  156. uint16_t value;
  157. i2c_rd_reg(INA219_ADDRESS, INA219_REG_CONFIG, &value);
  158. value &= 0x0007;
  159. return (ina219_mode_t)value;
  160. }