Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

142 linhas
7.4 KiB

  1. /**************************************************************************/
  2. /*!
  3. @file ina219.h
  4. @author K. Townsend (microBuilder.eu)
  5. @section LICENSE
  6. Software License Agreement (BSD License)
  7. Copyright (c) 2012 Kevin Townsend
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. Neither the name of the copyright holders nor the
  17. names of its contributors may be used to endorse or promote products
  18. derived from this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  20. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  23. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**************************************************************************/
  31. #ifndef _INA219_H_
  32. #define _INS219_H_
  33. #include "define.h"
  34. //#include "projectconfig.h"
  35. //#include "core/i2c/i2c.h"
  36. /*=========================================================================
  37. I2C ADDRESS/BITS
  38. -----------------------------------------------------------------------*/
  39. #define INA219_ADDRESS (0x80) // 1000000x (A0+A1=GND)
  40. #define INA219_READ (0x01)
  41. /*=========================================================================*/
  42. /*=========================================================================
  43. CONFIG REGISTER (R/W)
  44. -----------------------------------------------------------------------*/
  45. #define INA219_REG_CONFIG (0x00)
  46. /*---------------------------------------------------------------------*/
  47. #define INA219_CONFIG_RESET (0x8000) // Reset Bit
  48. #define INA219_CONFIG_RESET_READBACK 0x39FF
  49. #define INA219_CONFIG_BVOLTAGERANGE_MASK (0x4000) // Bus Voltage Range Mask
  50. #define INA219_CONFIG_BVOLTAGERANGE_16V (0x0000) // 0-16V Range
  51. #define INA219_CONFIG_BVOLTAGERANGE_32V (0x4000) // 0-32V Range
  52. #define INA219_CONFIG_GAIN_MASK (0x1800) // Gain Mask
  53. #define INA219_CONFIG_GAIN_1_40MV (0x0000) // Gain 1, 40mV Range
  54. #define INA219_CONFIG_GAIN_2_80MV (0x0800) // Gain 2, 80mV Range
  55. #define INA219_CONFIG_GAIN_4_160MV (0x1000) // Gain 4, 160mV Range
  56. #define INA219_CONFIG_GAIN_8_320MV (0x1800) // Gain 8, 320mV Range
  57. #define INA219_CONFIG_BADCRES_MASK (0x0780) // Bus ADC Resolution Mask
  58. #define INA219_CONFIG_BADCRES_9BIT (0x0080) // 9-bit bus res = 0..511
  59. #define INA219_CONFIG_BADCRES_10BIT (0x0100) // 10-bit bus res = 0..1023
  60. #define INA219_CONFIG_BADCRES_11BIT (0x0200) // 11-bit bus res = 0..2047
  61. #define INA219_CONFIG_BADCRES_12BIT (0x0400) // 12-bit bus res = 0..4097
  62. #define INA219_CONFIG_SADCRES_MASK (0x0078) // Shunt ADC Resolution and Averaging Mask
  63. #define INA219_CONFIG_SADCRES_9BIT_1S_84US (0x0000) // 1 x 9-bit shunt sample
  64. #define INA219_CONFIG_SADCRES_10BIT_1S_148US (0x0008) // 1 x 10-bit shunt sample
  65. #define INA219_CONFIG_SADCRES_11BIT_1S_276US (0x0010) // 1 x 11-bit shunt sample
  66. #define INA219_CONFIG_SADCRES_12BIT_1S_532US (0x0018) // 1 x 12-bit shunt sample
  67. #define INA219_CONFIG_SADCRES_12BIT_2S_1060US (0x0048) // 2 x 12-bit shunt samples averaged together
  68. #define INA219_CONFIG_SADCRES_12BIT_4S_2130US (0x0050) // 4 x 12-bit shunt samples averaged together
  69. #define INA219_CONFIG_SADCRES_12BIT_8S_4260US (0x0058) // 8 x 12-bit shunt samples averaged together
  70. #define INA219_CONFIG_SADCRES_12BIT_16S_8510US (0x0060) // 16 x 12-bit shunt samples averaged together
  71. #define INA219_CONFIG_SADCRES_12BIT_32S_17MS (0x0068) // 32 x 12-bit shunt samples averaged together
  72. #define INA219_CONFIG_SADCRES_12BIT_64S_34MS (0x0070) // 64 x 12-bit shunt samples averaged together
  73. #define INA219_CONFIG_SADCRES_12BIT_128S_69MS (0x0078) // 128 x 12-bit shunt samples averaged together
  74. #define INA219_CONFIG_MODE_MASK (0x0007) // Operating Mode Mask
  75. #define INA219_CONFIG_MODE_POWERDOWN (0x0000)
  76. #define INA219_CONFIG_MODE_SVOLT_TRIGGERED (0x0001)
  77. #define INA219_CONFIG_MODE_BVOLT_TRIGGERED (0x0002)
  78. #define INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED (0x0003)
  79. #define INA219_CONFIG_MODE_ADCOFF (0x0004)
  80. #define INA219_CONFIG_MODE_SVOLT_CONTINUOUS (0x0005)
  81. #define INA219_CONFIG_MODE_BVOLT_CONTINUOUS (0x0006)
  82. #define INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS (0x0007)
  83. /*=========================================================================*/
  84. /*=========================================================================
  85. SHUNT VOLTAGE REGISTER (R)
  86. -----------------------------------------------------------------------*/
  87. #define INA219_REG_SHUNTVOLTAGE (0x01)
  88. /*=========================================================================*/
  89. /*=========================================================================
  90. BUS VOLTAGE REGISTER (R)
  91. -----------------------------------------------------------------------*/
  92. #define INA219_REG_BUSVOLTAGE (0x02)
  93. /*=========================================================================*/
  94. /*=========================================================================
  95. POWER REGISTER (R)
  96. -----------------------------------------------------------------------*/
  97. #define INA219_REG_POWER (0x03)
  98. /*=========================================================================*/
  99. /*=========================================================================
  100. CURRENT REGISTER (R)
  101. -----------------------------------------------------------------------*/
  102. #define INA219_REG_CURRENT (0x04)
  103. /*=========================================================================*/
  104. /*=========================================================================
  105. CALIBRATION REGISTER (R/W)
  106. -----------------------------------------------------------------------*/
  107. #define INA219_REG_CALIBRATION (0x05)
  108. /*=========================================================================*/
  109. int ina219Init(void);
  110. int16_t ina219GetShuntVoltage(void);
  111. int16_t ina219GetBusVoltage(void);
  112. int16_t ina219GetPower(void);
  113. int16_t ina219GetPower_mW(void);
  114. int16_t ina219GetCurrent(void);
  115. int16_t ina219GetCurrent_mA(void);
  116. #endif