ina219.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. INA219.h - Header file for the 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. #ifndef INA219_h
  17. #define INA219_h
  18. #include "stm8s.h"
  19. #define CURRENT_SHUNT_RESISTANCE 10
  20. #define INA219_ADDRESS (uint8_t)(0x40 << 1)
  21. #define INA219_ADDR_RD (INA219_ADDRESS | 0x01)
  22. /**
  23. * Calibration value -- ôîðìóëû èç ìàíóàëà:
  24. * Current_LSB = Maximum Expected Current / 2^15
  25. * Power_LSB = 20 * Current_LSB
  26. * Calibration_Value = trunc( 0.04096 / (Current_LSB * Rshunt) )
  27. */
  28. // 8000000 uA / 32768
  29. #define CURRENT_LSB 244
  30. #define POWER_LSB 4880
  31. // 0.04096 / ((8 A/32768) * 0.01 Ohm)
  32. #define CALIBRATION_VALUE 16777
  33. /* INA219 Registers */
  34. #define INA219_REG_CONFIG 0x00
  35. #define INA219_REG_SHUNTVOLTAGE 0x01
  36. #define INA219_REG_BUSVOLTAGE 0x02
  37. #define INA219_REG_POWER 0x03
  38. #define INA219_REG_CURRENT 0x04
  39. #define INA219_REG_CALIBRATION 0x05
  40. typedef enum
  41. {
  42. INA219_RESET_OFF = 0x0000,
  43. INA219_RESET_ON = 0x8000
  44. } ina219_reset_t;
  45. typedef enum
  46. {
  47. INA219_RANGE_16V = 0x0000,
  48. INA219_RANGE_32V = 0x2000
  49. } ina219_bvr_t;
  50. typedef enum
  51. {
  52. INA219_GAIN_40MV = 0x0000,
  53. INA219_GAIN_80MV = 0x0800,
  54. INA219_GAIN_160MV = 0x1000,
  55. INA219_GAIN_320MV = 0x1800
  56. } ina219_pg_t;
  57. typedef enum
  58. {
  59. INA219_BUS_RES_9BIT = 0x0000,
  60. INA219_BUS_RES_10BIT = 0x0080,
  61. INA219_BUS_RES_11BIT = 0x0100,
  62. INA219_BUS_RES_12BIT = 0x0180,
  63. INA219_BUS_RES_12BIT_1S = 0x0400,
  64. INA219_BUS_RES_12BIT_2S = 0x0480,
  65. INA219_BUS_RES_12BIT_4S = 0x0500,
  66. INA219_BUS_RES_12BIT_8S = 0x0580,
  67. INA219_BUS_RES_12BIT_16S = 0x0600,
  68. INA219_BUS_RES_12BIT_32S = 0x0680,
  69. INA219_BUS_RES_12BIT_64S = 0x0700,
  70. INA219_BUS_RES_12BIT_128S = 0x0780
  71. } ina219_badc_t;
  72. typedef enum
  73. {
  74. INA219_SHUNT_RES_9BIT = 0x0000,
  75. INA219_SHUNT_RES_10BIT = 0x0008,
  76. INA219_SHUNT_RES_11BIT = 0x0010,
  77. INA219_SHUNT_RES_12BIT = 0x0018,
  78. INA219_SHUNT_RES_12BIT_1S = 0x0040,
  79. INA219_SHUNT_RES_12BIT_2S = 0x0048,
  80. INA219_SHUNT_RES_12BIT_4S = 0x0050,
  81. INA219_SHUNT_RES_12BIT_8S = 0x0058,
  82. INA219_SHUNT_RES_12BIT_16S = 0x0060,
  83. INA219_SHUNT_RES_12BIT_32S = 0x0068,
  84. INA219_SHUNT_RES_12BIT_64S = 0x0070,
  85. INA219_SHUNT_RES_12BIT_128S = 0x0078
  86. } ina219_sadc_t;
  87. typedef enum
  88. {
  89. INA219_MODE_POWER_DOWN = 0x0000,
  90. INA219_MODE_SHUNT_TRIG = 0x0001,
  91. INA219_MODE_BUS_TRIG = 0x0002,
  92. INA219_MODE_SHUNT_BUS_TRIG = 0x0003,
  93. INA219_MODE_ADC_OFF = 0x0004,
  94. INA219_MODE_SHUNT_CONT = 0x0005,
  95. INA219_MODE_BUS_CONT = 0x0006,
  96. INA219_MODE_SHUNT_BUS_CONT = 0x0007
  97. } ina219_mode_t;
  98. /**
  99. * @brief INA219 Configuration register definition
  100. */
  101. typedef struct {
  102. uint8_t INA219_Addr; /* I2C address */
  103. ina219_reset_t INA219_RST; /* Reset Bit */
  104. ina219_bvr_t INA219_BVR; /* Bus Voltage Range */
  105. ina219_pg_t INA219_PG; /* PGA (Shunt Voltage Only) */
  106. ina219_badc_t INA219_BADC; /* Bus ADC Resolution/Averaging */
  107. ina219_sadc_t INA219_SADC; /* Shunt ADC Resolution/Averaging */
  108. ina219_mode_t INA219_MODE; /* Operating Mode */
  109. } INA219_InitTypeDef;
  110. void INA219_Config(void);
  111. ina219_bvr_t getRange(void);
  112. ina219_pg_t getGain(void);
  113. ina219_badc_t getBusRes(void);
  114. ina219_sadc_t getShuntRes(void);
  115. ina219_mode_t getMode(void);
  116. int16_t readShuntVoltage(void);
  117. uint16_t readBusVoltage(void);
  118. uint16_t readBusCurrent(void);
  119. uint32_t readBusPower(void);
  120. uint16_t getMaxPossibleCurrent(void);
  121. uint16_t getMaxCurrent(void);
  122. uint16_t getMaxShuntVoltage(void);
  123. uint32_t getMaxPower(void);
  124. #endif