LTC2941.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. A library for Grove - Coulomb Counter for 3.3V to 5V (LTC2941)
  3. Copyright (c) 2018 seeed technology co., ltd.
  4. Author : Wayen Weng
  5. Create Time : June 2018
  6. Change Log :
  7. The MIT License (MIT)
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. */
  24. #include "LTC2941.h"
  25. const float LTC2941_CHARGE_LSB = 0.085;
  26. const uint8_t prescalarTable[8] = {1, 2, 4, 8, 16, 32, 64, 128};
  27. LTC2941::LTC2941(void) {
  28. resistor = 0.050;
  29. prescalar = 128;
  30. powerMaxAMh = LTC2941_BATTERY_MAX;
  31. powerExpend = 0;
  32. devAddr = LINEAR_LTC2941_ADDRESS;
  33. }
  34. void LTC2941::initialize(void) {
  35. setBatteryAlert(VBAT_ALERT_OFF);
  36. setAlertConfig(ALERT_DISABLED);
  37. setPrescaler(PRESCALAR_M_128);
  38. setAccumulatedCharge(0xffff);
  39. }
  40. void LTC2941::setBatteryFullMAh(uint16_t mAh, bool flag) {
  41. uint8_t i = 0;
  42. float qLSB = 0, temp = mAh;
  43. if (mAh > LTC2941_BATTERY_MAX) {
  44. return;
  45. }
  46. qLSB = temp / 65536;
  47. for (i = 0; i < 8; i ++) {
  48. temp = (LTC2941_CHARGE_LSB * prescalarTable[i] * 0.05) / (resistor * 128);
  49. if (qLSB <= temp) {
  50. break;
  51. }
  52. }
  53. if (i >= 8) {
  54. return;
  55. }
  56. powerMaxAMh = mAh;
  57. prescalar = prescalarTable[i];
  58. qLSB = (LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
  59. temp = mAh;
  60. temp = temp / qLSB;
  61. mAh = (uint16_t)temp;
  62. if (flag) {
  63. setAccumulatedCharge(mAh);
  64. }
  65. }
  66. void LTC2941::setBatteryAlert(LTC2941_VBAT_ALERT voltage) {
  67. updateReg(LTC2941_CONTROL_REG, LTC2941_VBAT_ALERT_MASK, MEASURE_VBAT_ALERT_POS, voltage);
  68. }
  69. void LTC2941::setPrescaler(LTC2941_PRESCALAR prescale) {
  70. prescalar = prescalarTable[prescale];
  71. updateReg(LTC2941_CONTROL_REG, LTC2941_PRESCALAR_MASK, MEASURE_PRESCALAR_POS, prescale);
  72. }
  73. void LTC2941::setAlertConfig(LTC2941_ALERT_CONF config) {
  74. updateReg(LTC2941_CONTROL_REG, LTC2941_ALERT_MODE_MASK, MEASURE_ALERT_MODE_POS, config);
  75. }
  76. void LTC2941::setShutdown(bool enable) {
  77. updateReg(LTC2941_CONTROL_REG, LTC2941_SHUTDOWN_MASK, MEASURE_SHUTDOWN_POS, enable);
  78. }
  79. void LTC2941::setAccumulatedCharge(uint16_t thresh) {
  80. write16(LTC2941_ACCUM_CHARGE_MSB_REG, thresh);
  81. }
  82. void LTC2941::setChargeThresholdHigh(uint16_t thresh) {
  83. write16(LTC2941_CHARGE_THRESH_HIGH_MSB_REG, thresh);
  84. }
  85. void LTC2941::setChargeThresholdLow(uint16_t thresh) {
  86. write16(LTC2941_CHARGE_THRESH_LOW_MSB_REG, thresh);
  87. }
  88. uint8_t LTC2941::getStatus(void) {
  89. return read8(LTC2941_STATUS_REG);
  90. }
  91. float LTC2941::getCoulombs(void) {
  92. uint16_t data = 0;
  93. float coulomb = 0;
  94. data = read16(LTC2941_ACCUM_CHARGE_MSB_REG);
  95. coulomb = (float)(data * LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
  96. coulomb = coulomb * 3.6f;
  97. return coulomb;
  98. }
  99. float LTC2941::getmAh(void) {
  100. uint16_t data = 0;
  101. float mAh = 0;
  102. data = read16(LTC2941_ACCUM_CHARGE_MSB_REG);
  103. mAh = (float)(data * LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
  104. return mAh;
  105. }
  106. float LTC2941::getPercent(void) {
  107. float mAh = getmAh();
  108. return (mAh * 100 / powerMaxAMh);
  109. }
  110. float LTC2941::getCoulombsExpend(void) {
  111. uint16_t data = 0;
  112. float coulomb = 0;
  113. data = 0xffff - read16(LTC2941_ACCUM_CHARGE_MSB_REG);
  114. if (data == 0xffff) {
  115. data = 0;
  116. } else if (data >= 0xf000) {
  117. powerExpend += 0xf000;
  118. data -= 0xf000;
  119. setAccumulatedCharge(0xffff - data);
  120. }
  121. coulomb = (float)((data + powerExpend) * LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
  122. coulomb = coulomb * 3.6f;
  123. return coulomb;
  124. }
  125. float LTC2941::getmAhExpend(void) {
  126. uint16_t data = 0;
  127. float mAh = 0;
  128. data = 0xffff - read16(LTC2941_ACCUM_CHARGE_MSB_REG);
  129. if (data == 0xffff) {
  130. data = 0;
  131. } else if (data >= 0xf000) {
  132. powerExpend += 0xf000;
  133. data -= 0xf000;
  134. setAccumulatedCharge(0xffff - data);
  135. }
  136. mAh = (float)((data + powerExpend) * LTC2941_CHARGE_LSB * prescalar * 0.05) / (resistor * 128);
  137. return mAh;
  138. }
  139. void LTC2941::write8(uint8_t reg, uint8_t val) {
  140. Wire.beginTransmission(devAddr);
  141. Wire.write(reg);
  142. Wire.write(val);
  143. Wire.endTransmission();
  144. }
  145. void LTC2941::write16(uint8_t reg, uint16_t val) {
  146. Wire.beginTransmission(devAddr);
  147. Wire.write(reg);
  148. Wire.write(val >> 8);
  149. Wire.write(val & 0xff);
  150. Wire.endTransmission();
  151. }
  152. uint8_t LTC2941::read8(uint8_t reg) {
  153. uint8_t data = 0;
  154. Wire.beginTransmission(devAddr);
  155. Wire.write(reg);
  156. Wire.endTransmission(false);
  157. Wire.requestFrom((int16_t)devAddr, 1);
  158. while (Wire.available()) {
  159. data = Wire.read();
  160. }
  161. return data;
  162. }
  163. uint16_t LTC2941::read16(uint8_t reg) {
  164. uint16_t msb = 0, lsb = 0;
  165. Wire.beginTransmission(devAddr);
  166. Wire.write(reg);
  167. Wire.endTransmission(false);
  168. Wire.requestFrom((int16_t)devAddr, 2);
  169. while (Wire.available()) {
  170. msb = Wire.read();
  171. lsb = Wire.read();
  172. }
  173. return (lsb | (msb << 8));
  174. }
  175. void LTC2941::updateReg(uint8_t reg, uint8_t mask, uint8_t shift, uint8_t val) {
  176. uint8_t tmp;
  177. tmp = read8(reg);
  178. tmp &= mask;
  179. tmp |= (val << shift) & ~mask;
  180. write8(reg, tmp);
  181. }
  182. LTC2941 ltc2941;