ina219.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "stm32f0xx.h"
  19. #define CURRENT_SHUNT_RESISTENCE 20
  20. #define INA219_ADDRESS (uint8_t)(0x40<<1)
  21. #define INA219_ADDR_RD (INA219_ADDRESS | 0x01)
  22. /* INA219 Registers */
  23. #define INA219_REG_CONFIG 0x00
  24. #define INA219_REG_SHUNTVOLTAGE 0x01
  25. #define INA219_REG_BUSVOLTAGE 0x02
  26. #define INA219_REG_POWER 0x03
  27. #define INA219_REG_CURRENT 0x04
  28. #define INA219_REG_CALIBRATION 0x05
  29. typedef enum
  30. {
  31. INA219_RESET_OFF = 0x0000,
  32. INA219_RESET_ON = 0x8000
  33. } ina219_reset_t;
  34. typedef enum
  35. {
  36. INA219_RANGE_16V = 0x0000,
  37. INA219_RANGE_32V = 0x2000
  38. } ina219_bvr_t;
  39. typedef enum
  40. {
  41. INA219_GAIN_40MV = 0x0000,
  42. INA219_GAIN_80MV = 0x0800,
  43. INA219_GAIN_160MV = 0x1000,
  44. INA219_GAIN_320MV = 0x1800
  45. } ina219_pg_t;
  46. typedef enum
  47. {
  48. INA219_BUS_RES_9BIT = 0x0000,
  49. INA219_BUS_RES_10BIT = 0x0080,
  50. INA219_BUS_RES_11BIT = 0x0100,
  51. INA219_BUS_RES_12BIT = 0x0180
  52. } ina219_badc_t;
  53. typedef enum
  54. {
  55. INA219_SHUNT_RES_9BIT = 0x0000,
  56. INA219_SHUNT_RES_10BIT = 0x0008,
  57. INA219_SHUNT_RES_11BIT = 0x0010,
  58. INA219_SHUNT_RES_12BIT = 0x0018,
  59. INA219_SHUNT_RES_12BIT_1S = 0x0040,
  60. INA219_SHUNT_RES_12BIT_2S = 0x0048,
  61. INA219_SHUNT_RES_12BIT_4S = 0x0050,
  62. INA219_SHUNT_RES_12BIT_8S = 0x0058,
  63. INA219_SHUNT_RES_12BIT_16S = 0x0060,
  64. INA219_SHUNT_RES_12BIT_32S = 0x0068,
  65. INA219_SHUNT_RES_12BIT_64S = 0x0070,
  66. INA219_SHUNT_RES_12BIT_128S = 0x0078
  67. } ina219_sadc_t;
  68. typedef enum
  69. {
  70. INA219_MODE_POWER_DOWN = 0x0000,
  71. INA219_MODE_SHUNT_TRIG = 0x0001,
  72. INA219_MODE_BUS_TRIG = 0x0002,
  73. INA219_MODE_SHUNT_BUS_TRIG = 0x0003,
  74. INA219_MODE_ADC_OFF = 0x0004,
  75. INA219_MODE_SHUNT_CONT = 0x0005,
  76. INA219_MODE_BUS_CONT = 0x0006,
  77. INA219_MODE_SHUNT_BUS_CONT = 0x0007
  78. } ina219_mode_t;
  79. /**
  80. * @brief INA219 Configuration register definition
  81. */
  82. typedef struct {
  83. uint8_t INA219_Addr; /* I2C address */
  84. ina219_reset_t INA219_RST; /* Reset Bit */
  85. ina219_bvr_t INA219_BVR; /* Bus Voltage Range */
  86. ina219_pg_t INA219_PG; /* PGA (Shunt Voltage Only) */
  87. ina219_badc_t INA219_BADC; /* Bus ADC Resolution/Averaging */
  88. ina219_sadc_t INA219_SADC; /* Shunt ADC Resolution/Averaging */
  89. ina219_mode_t INA219_MODE; /* Operating Mode */
  90. } INA219_InitTypeDef;
  91. void INA219_Config(INA219_InitTypeDef * INA219_InitStruct);
  92. void INA219_StructInit(INA219_InitTypeDef * INA219_InitStruct);
  93. void INA219_Calibrate(void);
  94. ina219_bvr_t getRange(void);
  95. ina219_pg_t getGain(void);
  96. ina219_badc_t getBusRes(void);
  97. ina219_sadc_t getShuntRes(void);
  98. ina219_mode_t getMode(void);
  99. int16_t readShuntVoltage(void);
  100. uint16_t readBusVoltage(void);
  101. uint16_t readBusCurrent(void);
  102. uint32_t readBusPower(void);
  103. uint16_t getMaxPossibleCurrent(void);
  104. uint16_t getMaxCurrent(void);
  105. uint16_t getMaxShuntVoltage(void);
  106. uint32_t getMaxPower(void);
  107. #endif