選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

679 行
24 KiB

  1. /**************************************************************************/
  2. /*!
  3. @file ina219.c
  4. @author K. Townsend (microBuilder.eu)
  5. @brief Driver for the TI INA219 current/power monitor
  6. @section DESCRIPTION
  7. The INA219 is an I2C-based current/power monitor that monitors the
  8. voltage drop across a shunt resistor, as well as the supply voltage.
  9. @section EXAMPLE
  10. @code
  11. ina219Init();
  12. int16_t current = 0;
  13. int16_t power = 0;
  14. int16_t current_mA = 0;
  15. int16_t power_mW = 0;
  16. int16_t busvoltage = 0;
  17. int16_t shuntvoltage = 0;
  18. int16_t loadVoltage = 0;
  19. while(1)
  20. {
  21. shuntvoltage = ina219GetShuntVoltage();
  22. busvoltage = ina219GetBusVoltage();
  23. power = ina219GetPower();
  24. current = ina219GetCurrent();
  25. power_mW = ina219GetPower_mW();
  26. current_mA = ina219GetCurrent_mA();
  27. loadVoltage = busvoltage + (shuntvoltage / 100);
  28. printf("%-15s %6d = %d.%dmV (%duV) \r\n", "Shunt Voltage:", shuntvoltage, shuntvoltage / 100, shuntvoltage % 100, shuntvoltage * 10);
  29. printf("%-15s %6d = %d.%dV \r\n", "Bus Voltage:", busvoltage, busvoltage / 1000, busvoltage % 1000);
  30. printf("%-15s %6d = %d.%dV \r\n", "Load Voltage:", loadVoltage, loadVoltage / 1000, loadVoltage % 1000);
  31. printf("%-15s %6d = %dmW \r\n", "Power:", power, power_mW);
  32. printf("%-15s %6d = %dmA \r\n", "Current:", current, current_mA);
  33. printf("\r\n");
  34. systickDelay(5000);
  35. }
  36. @endcode
  37. @section LICENSE
  38. Software License Agreement (BSD License)
  39. Copyright (c) 2012 Kevin Townsend
  40. All rights reserved.
  41. Redistribution and use in source and binary forms, with or without
  42. modification, are permitted provided that the following conditions are met:
  43. 1. Redistributions of source code must retain the above copyright
  44. notice, this list of conditions and the following disclaimer.
  45. 2. Redistributions in binary form must reproduce the above copyright
  46. notice, this list of conditions and the following disclaimer in the
  47. documentation and/or other materials provided with the distribution.
  48. 3. Neither the name of the copyright holders nor the
  49. names of its contributors may be used to endorse or promote products
  50. derived from this software without specific prior written permission.
  51. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
  52. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  53. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  54. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
  55. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  56. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  57. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  58. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  60. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. */
  62. /**************************************************************************/
  63. #include "ina219.h"
  64. #include "I2C.h"
  65. #include <stdint.h>
  66. unsigned char I2CMasterBuffer[I2C_BUFSIZE];
  67. unsigned char I2CSlaveBuffer[I2C_BUFSIZE];
  68. // The following multipliers are used to convert raw current and power
  69. // values to mA and mW, taking into account the current config settings
  70. uint32_t ina219_currentDivider_mA = 0;
  71. uint32_t ina219_powerDivider_mW = 0;
  72. /**************************************************************************/
  73. /*!
  74. @brief Sends a single command byte over I2C
  75. */
  76. /**************************************************************************/
  77. static int ina219WriteRegister (uint8_t reg, uint16_t value)
  78. {
  79. // Clear write buffers
  80. uint32_t i;
  81. for ( i = 0; i < I2C_BUFSIZE; i++ )
  82. {
  83. I2CMasterBuffer[i] = 0x00;
  84. }
  85. //I2CWriteLength = 4;
  86. // I2CReadLength = 0;
  87. I2CMasterBuffer[0] = INA219_ADDRESS; // I2C device address
  88. I2CMasterBuffer[1] = reg; // Register
  89. I2CMasterBuffer[2] = value >> 8; // Upper 8-bits
  90. I2CMasterBuffer[3] = value & 0xFF; // Lower 8-bits
  91. return I2CWrite(I2CMasterBuffer,4);
  92. //i2cEngine();
  93. }
  94. /**************************************************************************/
  95. /*!
  96. @brief Reads a 16 bit values over I2C
  97. */
  98. /**************************************************************************/
  99. static void ina219Read16(uint8_t reg, uint16_t *value)
  100. {
  101. // Clear write buffers
  102. int RET = RET_OK;
  103. uint32_t i;
  104. for ( i = 0; i < I2C_BUFSIZE; i++ )
  105. {
  106. I2CMasterBuffer[i] = 0x00;
  107. }
  108. // I2CWriteLength = 2;
  109. //I2CReadLength = 2;
  110. I2CMasterBuffer[0] = INA219_ADDRESS; // I2C device address
  111. I2CMasterBuffer[1] = reg; // Command register
  112. // Append address w/read bit
  113. I2CMasterBuffer[2] = INA219_ADDRESS | INA219_READ;
  114. // i2cEngine();
  115. I2CWrite(I2CMasterBuffer,2); //write pointer to the good register
  116. RET = I2CRead(I2CMasterBuffer[2],&I2CSlaveBuffer[0],2); //read the data
  117. if(RET != RET_OK)
  118. {
  119. * value = 0;
  120. return;
  121. }
  122. // Shift values to create properly formed integer
  123. *value = ((I2CSlaveBuffer[0] << 8) | I2CSlaveBuffer[1]);
  124. }
  125. void ina219SetCalibration_13V_10A(void)
  126. {
  127. // By default we use a pretty huge range for the input voltage,
  128. // which probably isn't the most appropriate choice for system
  129. // that don't use a lot of power. But all of the calculations
  130. // are shown below if you want to change the settings. You will
  131. // also need to change any relevant register settings, such as
  132. // setting the VBUS_MAX to 16V instead of 32V, etc.
  133. // VBUS_MAX = 13V (Assumes 32V, can also be set to 16V)
  134. // VSHUNT_MAX = 0.32 (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04)
  135. // RSHUNT = 0.1 (Resistor value in ohms)
  136. // 1. Determine max possible current
  137. // MaxPossible_I = VSHUNT_MAX / RSHUNT
  138. // MaxPossible_I = 3.2A
  139. // 2. Determine max expected current
  140. // MaxExpected_I = 3.2A
  141. // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
  142. // MinimumLSB = MaxExpected_I/32767
  143. // MinimumLSB = 0.00009765 (98µA per bit)
  144. // MaximumLSB = MaxExpected_I/4096
  145. // MaximumLSB = 0,000488 (488µA per bit)
  146. // 4. Choose an LSB between the min and max values
  147. // (Preferrably a roundish number close to MinLSB)
  148. // CurrentLSB = 0.0001 (100µA per bit)
  149. // 5. Compute the calibration register
  150. // Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
  151. // Cal = 4096 (0x1000)
  152. // 6. Calculate the power LSB
  153. // PowerLSB = 20 * CurrentLSB
  154. // PowerLSB = 0.002 (2mW per bit)
  155. // 7. Compute the maximum current and shunt voltage values before overflow
  156. //
  157. // Max_Current = Current_LSB * 32767
  158. // Max_Current = 3.2767A before overflow
  159. //
  160. // If Max_Current > Max_Possible_I then
  161. // Max_Current_Before_Overflow = MaxPossible_I
  162. // Else
  163. // Max_Current_Before_Overflow = Max_Current
  164. // End If
  165. //
  166. // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
  167. // Max_ShuntVoltage = 0.32V
  168. //
  169. // If Max_ShuntVoltage >= VSHUNT_MAX
  170. // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
  171. // Else
  172. // Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
  173. // End If
  174. // 8. Computer the Maximum Power
  175. // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
  176. // MaximumPower = 3.2 * 32V
  177. // MaximumPower = 102.4W
  178. // Set multipliers to convert raw current/power values
  179. ina219_currentDivider_mA = 10; // Current LSB = 100uA per bit (1000/100 = 10)
  180. ina219_powerDivider_mW = 2; // Power LSB = 1mW per bit (2/1)
  181. // Set Calibration register to 'Cal' calculated above
  182. ina219WriteRegister(INA219_REG_CALIBRATION, 0x1000);
  183. // Set Config register to take into account the settings above
  184. uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V |
  185. INA219_CONFIG_GAIN_8_320MV |
  186. INA219_CONFIG_BADCRES_12BIT |
  187. INA219_CONFIG_SADCRES_12BIT_1S_532US |
  188. // INA219_CONFIG_MODE_SVOLT_CONTINUOUS;
  189. INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
  190. ina219WriteRegister(INA219_REG_CONFIG, config);
  191. }
  192. /**************************************************************************/
  193. /*!
  194. @brief Configures to INA219 to be able to measure up to 32V and 2A
  195. of current. Each unit of current corresponds to 100uA, and
  196. each unit of power corresponds to 2mW. Counter overflow
  197. occurs at 3.2A.
  198. @note These calculations assume a 0.1 ohm resistor is present
  199. */
  200. /**************************************************************************/
  201. void ina219SetCalibration_32V_2A(void)
  202. {
  203. // By default we use a pretty huge range for the input voltage,
  204. // which probably isn't the most appropriate choice for system
  205. // that don't use a lot of power. But all of the calculations
  206. // are shown below if you want to change the settings. You will
  207. // also need to change any relevant register settings, such as
  208. // setting the VBUS_MAX to 16V instead of 32V, etc.
  209. // VBUS_MAX = 32V (Assumes 32V, can also be set to 16V)
  210. // VSHUNT_MAX = 0.32 (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04)
  211. // RSHUNT = 0.1 (Resistor value in ohms)
  212. // 1. Determine max possible current
  213. // MaxPossible_I = VSHUNT_MAX / RSHUNT
  214. // MaxPossible_I = 3.2A
  215. // 2. Determine max expected current
  216. // MaxExpected_I = 2.0A
  217. // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
  218. // MinimumLSB = MaxExpected_I/32767
  219. // MinimumLSB = 0.000061 (61µA per bit)
  220. // MaximumLSB = MaxExpected_I/4096
  221. // MaximumLSB = 0,000488 (488µA per bit)
  222. // 4. Choose an LSB between the min and max values
  223. // (Preferrably a roundish number close to MinLSB)
  224. // CurrentLSB = 0.0001 (100µA per bit)
  225. // 5. Compute the calibration register
  226. // Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
  227. // Cal = 4096 (0x1000)
  228. // 6. Calculate the power LSB
  229. // PowerLSB = 20 * CurrentLSB
  230. // PowerLSB = 0.002 (2mW per bit)
  231. // 7. Compute the maximum current and shunt voltage values before overflow
  232. //
  233. // Max_Current = Current_LSB * 32767
  234. // Max_Current = 3.2767A before overflow
  235. //
  236. // If Max_Current > Max_Possible_I then
  237. // Max_Current_Before_Overflow = MaxPossible_I
  238. // Else
  239. // Max_Current_Before_Overflow = Max_Current
  240. // End If
  241. //
  242. // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
  243. // Max_ShuntVoltage = 0.32V
  244. //
  245. // If Max_ShuntVoltage >= VSHUNT_MAX
  246. // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
  247. // Else
  248. // Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
  249. // End If
  250. // 8. Computer the Maximum Power
  251. // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
  252. // MaximumPower = 3.2 * 32V
  253. // MaximumPower = 102.4W
  254. // Set multipliers to convert raw current/power values
  255. ina219_currentDivider_mA = 10; // Current LSB = 100uA per bit (1000/100 = 10)
  256. ina219_powerDivider_mW = 2; // Power LSB = 1mW per bit (2/1)
  257. // Set Calibration register to 'Cal' calculated above
  258. ina219WriteRegister(INA219_REG_CALIBRATION, 0x1000);
  259. // Set Config register to take into account the settings above
  260. uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V |
  261. INA219_CONFIG_GAIN_8_320MV |
  262. INA219_CONFIG_BADCRES_12BIT |
  263. INA219_CONFIG_SADCRES_12BIT_1S_532US |
  264. INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
  265. ina219WriteRegister(INA219_REG_CONFIG, config);
  266. }
  267. /**************************************************************************/
  268. /*!
  269. @brief Configures to INA219 to be able to measure up to 32V and 1A
  270. of current. Each unit of current corresponds to 40uA, and each
  271. unit of power corresponds to 800µW. Counter overflow occurs at
  272. 1.3A.
  273. @note These calculations assume a 0.1 ohm resistor is present
  274. */
  275. /**************************************************************************/
  276. void ina219SetCalibration_32V_1A(void)
  277. {
  278. // By default we use a pretty huge range for the input voltage,
  279. // which probably isn't the most appropriate choice for system
  280. // that don't use a lot of power. But all of the calculations
  281. // are shown below if you want to change the settings. You will
  282. // also need to change any relevant register settings, such as
  283. // setting the VBUS_MAX to 16V instead of 32V, etc.
  284. // VBUS_MAX = 32V (Assumes 32V, can also be set to 16V)
  285. // VSHUNT_MAX = 0.32 (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04)
  286. // RSHUNT = 0.1 (Resistor value in ohms)
  287. // 1. Determine max possible current
  288. // MaxPossible_I = VSHUNT_MAX / RSHUNT
  289. // MaxPossible_I = 3.2A
  290. // 2. Determine max expected current
  291. // MaxExpected_I = 1.0A
  292. // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
  293. // MinimumLSB = MaxExpected_I/32767
  294. // MinimumLSB = 0.0000305 (30.5µA per bit)
  295. // MaximumLSB = MaxExpected_I/4096
  296. // MaximumLSB = 0.000244 (244µA per bit)
  297. // 4. Choose an LSB between the min and max values
  298. // (Preferrably a roundish number close to MinLSB)
  299. // CurrentLSB = 0.0000400 (40µA per bit)
  300. // 5. Compute the calibration register
  301. // Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
  302. // Cal = 10240 (0x2800)
  303. // 6. Calculate the power LSB
  304. // PowerLSB = 20 * CurrentLSB
  305. // PowerLSB = 0.0008 (800µW per bit)
  306. // 7. Compute the maximum current and shunt voltage values before overflow
  307. //
  308. // Max_Current = Current_LSB * 32767
  309. // Max_Current = 1.31068A before overflow
  310. //
  311. // If Max_Current > Max_Possible_I then
  312. // Max_Current_Before_Overflow = MaxPossible_I
  313. // Else
  314. // Max_Current_Before_Overflow = Max_Current
  315. // End If
  316. //
  317. // ... In this case, we're good though since Max_Current is less than MaxPossible_I
  318. //
  319. // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
  320. // Max_ShuntVoltage = 0.131068V
  321. //
  322. // If Max_ShuntVoltage >= VSHUNT_MAX
  323. // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
  324. // Else
  325. // Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
  326. // End If
  327. // 8. Computer the Maximum Power
  328. // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
  329. // MaximumPower = 1.31068 * 32V
  330. // MaximumPower = 41.94176W
  331. // Set multipliers to convert raw current/power values
  332. ina219_currentDivider_mA = 25; // Current LSB = 40uA per bit (1000/40 = 25)
  333. ina219_powerDivider_mW = 1; // Power LSB = 800µW per bit
  334. // Set Calibration register to 'Cal' calculated above
  335. ina219WriteRegister(INA219_REG_CALIBRATION, 0x2800);
  336. // Set Config register to take into account the settings above
  337. uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V |
  338. INA219_CONFIG_GAIN_8_320MV |
  339. INA219_CONFIG_BADCRES_12BIT |
  340. INA219_CONFIG_SADCRES_12BIT_1S_532US |
  341. INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
  342. ina219WriteRegister(INA219_REG_CONFIG, config);
  343. }
  344. /**************************************************************************/
  345. /*!
  346. @brief Configures to INA219 to be able to measure up to 16V and 500mA
  347. of current. Each unit of current corresponds to 25uA, and each
  348. unit of power corresponds to 500µW. Counter overflow occurs at
  349. 800mA.
  350. @note These calculations assume a 0.1 ohm resistor is present
  351. */
  352. /**************************************************************************/
  353. void ina219SetCalibration_16V_500mA(void)
  354. {
  355. // VBUS_MAX = 16V
  356. // VSHUNT_MAX = 0.08 (Assumes Gain 2, 80mV, can also be 0.32, 0.16, 0.04)
  357. // RSHUNT = 0.1 (Resistor value in ohms)
  358. // 1. Determine max possible current
  359. // MaxPossible_I = VSHUNT_MAX / RSHUNT
  360. // MaxPossible_I = 0.8A
  361. // 2. Determine max expected current
  362. // MaxExpected_I = 0.5A
  363. // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
  364. // MinimumLSB = MaxExpected_I/32767
  365. // MinimumLSB = 0.0000153 (15.3µA per bit)
  366. // MaximumLSB = MaxExpected_I/4096
  367. // MaximumLSB = 0.0001221 (122µA per bit)
  368. // 4. Choose an LSB between the min and max values
  369. // (Preferrably a roundish number close to MinLSB)
  370. // CurrentLSB = 0.0000250 (25µA per bit)
  371. // 5. Compute the calibration register
  372. // Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
  373. // Cal = 16384 (0x4000)
  374. // 6. Calculate the power LSB
  375. // PowerLSB = 20 * CurrentLSB
  376. // PowerLSB = 0.0005 (500µW per bit)
  377. // 7. Compute the maximum current and shunt voltage values before overflow
  378. //
  379. // Max_Current = Current_LSB * 32767
  380. // Max_Current = 0.819175 (819 mA before overflow)
  381. //
  382. // If Max_Current > Max_Possible_I then
  383. // Max_Current_Before_Overflow = MaxPossible_I
  384. // Else
  385. // Max_Current_Before_Overflow = Max_Current
  386. // End If
  387. //
  388. // Max_Current_Before_Overflow = 0.8A
  389. //
  390. // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
  391. // Max_ShuntVoltage = 0.8 * 0.1
  392. // Max_ShuntVoltage = 0.08V
  393. //
  394. // If Max_ShuntVoltage >= VSHUNT_MAX
  395. // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
  396. // Else
  397. // Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
  398. // End If
  399. // 8. Computer the Maximum Power
  400. // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
  401. // MaximumPower = 0.8 * 16V
  402. // MaximumPower = 12.8W
  403. // Set multipliers to convert raw current/power values
  404. ina219_currentDivider_mA = 40; // Current LSB = 25uA per bit (1000/25 = 40)
  405. ina219_powerDivider_mW = 1; // Power LSB = 500µW per bit
  406. // Set Calibration register to 'Cal' calculated above
  407. ina219WriteRegister(INA219_REG_CALIBRATION, 0x4000);
  408. // Set Config register to take into account the settings above
  409. uint16_t config = INA219_CONFIG_BVOLTAGERANGE_16V |
  410. INA219_CONFIG_GAIN_2_80MV |
  411. INA219_CONFIG_BADCRES_12BIT |
  412. INA219_CONFIG_SADCRES_12BIT_1S_532US |
  413. INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
  414. ina219WriteRegister(INA219_REG_CONFIG, config);
  415. }
  416. /**************************************************************************/
  417. /*!
  418. @brief Configures to INA219 to be able to measure up to 16V and 200mA
  419. of current. Each unit of current corresponds to 10uA, and each
  420. unit of power corresponds to 200µW. Counter overflow occurs at
  421. 327mA.
  422. @note These calculations assume a 0.1 ohm resistor is present
  423. */
  424. /**************************************************************************/
  425. void ina219SetCalibration_16V_200mA(void)
  426. {
  427. // VBUS_MAX = 16V
  428. // VSHUNT_MAX = 0.04 (Assumes Gain 1, 40mV, can also be 0.32, 0.16, 0.08)
  429. // RSHUNT = 0.1 (Resistor value in ohms)
  430. // 1. Determine max possible current
  431. // MaxPossible_I = VSHUNT_MAX / RSHUNT
  432. // MaxPossible_I = 0.4A
  433. // 2. Determine max expected current
  434. // MaxExpected_I = 0.2A
  435. // 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
  436. // MinimumLSB = MaxExpected_I/32767
  437. // MinimumLSB = 0.000006104 (6.104µA per bit)
  438. // MaximumLSB = MaxExpected_I/4096
  439. // MaximumLSB = 0,000048828 (48.82µA per bit)
  440. // 4. Choose an LSB between the min and max values
  441. // CurrentLSB = 0.000010 (10µA per bit)
  442. // 5. Compute the calibration register
  443. // Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
  444. // Cal = 40960 (0xA000)
  445. // 6. Calculate the power LSB
  446. // PowerLSB = 20 * CurrentLSB
  447. // PowerLSB = 0.0002 (200µW per bit)
  448. // 7. Compute the maximum current and shunt voltage values before overflow
  449. //
  450. // Max_Current = Current_LSB * 32767
  451. // Max_Current = 0.32767 (328 mA before overflow)
  452. //
  453. // If Max_Current > Max_Possible_I then
  454. // Max_Current_Before_Overflow = MaxPossible_I
  455. // Else
  456. // Max_Current_Before_Overflow = Max_Current
  457. // End If
  458. //
  459. // Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
  460. // Max_ShuntVoltage = 0.032767V
  461. //
  462. // If Max_ShuntVoltage >= VSHUNT_MAX
  463. // Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
  464. // Else
  465. // Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
  466. // End If
  467. // 8. Computer the Maximum Power
  468. // MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
  469. // MaximumPower = 0.32767 * 16V
  470. // MaximumPower = 5.24W
  471. // Set multipliers to convert raw current/power values
  472. ina219_currentDivider_mA = 100; // Current LSB = 10uA per bit (1000/10 = 100)
  473. ina219_powerDivider_mW = 1; // Power LSB = 200µW per bit
  474. // Set Calibration register to 'Cal' calculated above
  475. ina219WriteRegister(INA219_REG_CALIBRATION, 0xA000);
  476. // Set Config register to take into account the settings above
  477. uint16_t config = INA219_CONFIG_BVOLTAGERANGE_16V |
  478. INA219_CONFIG_GAIN_1_40MV |
  479. INA219_CONFIG_BADCRES_12BIT |
  480. INA219_CONFIG_SADCRES_12BIT_1S_532US |
  481. INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
  482. ina219WriteRegister(INA219_REG_CONFIG, config);
  483. }
  484. /**************************************************************************/
  485. /*!
  486. @brief Initialises the I2C block
  487. */
  488. /**************************************************************************/
  489. int ina219Init(void)
  490. {
  491. int RET = RET_OK;
  492. uint16_t ReadBack;
  493. // Reset INA219 (set to default values)
  494. RET = ina219WriteRegister(INA219_REG_CONFIG, INA219_CONFIG_RESET);
  495. ina219Read16(INA219_REG_CONFIG,&ReadBack);
  496. if(ReadBack != INA219_CONFIG_RESET_READBACK)
  497. {
  498. return RET_ERROR;
  499. }
  500. // Setup chip for 32V and 2A by default
  501. // ina219SetCalibration_32V_2A();
  502. ina219SetCalibration_13V_10A();
  503. return RET;
  504. }
  505. /**************************************************************************/
  506. /*!
  507. @brief Gets the shunt voltage (16-bit signed integer, so +-32767)
  508. */
  509. /**************************************************************************/
  510. int16_t ina219GetShuntVoltage(void)
  511. {
  512. uint16_t value;
  513. ina219Read16(INA219_REG_SHUNTVOLTAGE, &value);
  514. return value;
  515. }
  516. /**************************************************************************/
  517. /*!
  518. @brief Gets the shunt voltage (16-bit signed integer, so +-32767)
  519. */
  520. /**************************************************************************/
  521. int16_t ina219GetBusVoltage(void)
  522. {
  523. uint16_t value;
  524. ina219Read16(INA219_REG_BUSVOLTAGE, &value);
  525. // Shift to the right 3 to drop CNVR and OVF and then multiply by LSB
  526. return (value >> 3) * 4;
  527. }
  528. /**************************************************************************/
  529. /*!
  530. @brief Gets the raw power value (16-bit signed integer, so +-32767)
  531. */
  532. /**************************************************************************/
  533. int16_t ina219GetPower(void)
  534. {
  535. uint16_t value;
  536. ina219Read16(INA219_REG_POWER, &value);
  537. return value;
  538. }
  539. /**************************************************************************/
  540. /*!
  541. @brief Gets the power value in mW, taking into account the config
  542. settings and power LSB
  543. */
  544. /**************************************************************************/
  545. int16_t ina219GetPower_mW(void)
  546. {
  547. uint16_t value;
  548. ina219Read16(INA219_REG_POWER, &value);
  549. return value / ina219_powerDivider_mW;
  550. }
  551. /**************************************************************************/
  552. /*!
  553. @brief Gets the raw current value (16-bit signed integer, so +-32767)
  554. */
  555. /**************************************************************************/
  556. int16_t ina219GetCurrent(void)
  557. {
  558. uint16_t value;
  559. ina219Read16(INA219_REG_CURRENT, &value);
  560. return value;
  561. }
  562. /**************************************************************************/
  563. /*!
  564. @brief Gets the current value in mA, taking into account the
  565. config settings and current LSB
  566. */
  567. /**************************************************************************/
  568. int16_t ina219GetCurrent_mA(void)
  569. {
  570. uint16_t value;
  571. ina219Read16(INA219_REG_CURRENT, &value);
  572. return value / ina219_currentDivider_mA;
  573. }