voltmeter.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "voltmeter.h"
  2. #include "Wire.h"
  3. void Voltmeter::i2cBegin() {
  4. // Wire.begin();
  5. }
  6. bool Voltmeter::i2cReadBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff,
  7. uint16_t len) {
  8. Wire.beginTransmission(addr);
  9. Wire.write(reg_addr);
  10. uint8_t i = 0;
  11. if (Wire.endTransmission(false) == 0 &&
  12. Wire.requestFrom(addr, (uint8_t)len)) {
  13. while (Wire.available()) {
  14. buff[i++] = Wire.read();
  15. }
  16. return true;
  17. }
  18. return false;
  19. }
  20. bool Voltmeter::i2cWriteBytes(uint8_t addr, uint8_t reg_addr, uint8_t* buff,
  21. uint16_t len) {
  22. bool function_result = false;
  23. Wire.beginTransmission(addr);
  24. Wire.write(reg_addr);
  25. for (int i = 0; i < len; i++) {
  26. Wire.write(*(buff + i));
  27. }
  28. function_result = (Wire.endTransmission() == 0);
  29. return function_result;
  30. }
  31. bool Voltmeter::i2cReadU16(uint8_t addr, uint8_t reg_addr, uint16_t* value) {
  32. uint8_t read_buf[2] = {0x00, 0x00};
  33. bool result = i2cReadBytes(addr, reg_addr, read_buf, 2);
  34. *value = (read_buf[0] << 8) | read_buf[1];
  35. return result;
  36. }
  37. bool Voltmeter::i2cWriteU16(uint8_t addr, uint8_t reg_addr, uint16_t value) {
  38. uint8_t write_buf[2];
  39. write_buf[0] = value >> 8;
  40. write_buf[1] = value & 0xff;
  41. return i2cWriteBytes(addr, reg_addr, write_buf, 2);
  42. }
  43. float Voltmeter::getResolution(voltmeterGain_t gain) {
  44. switch (gain) {
  45. case PAG_6144:
  46. return ADS1115_MV_6144 / VOLTMETER_PRESSURE_COEFFICIENT;
  47. case PAG_4096:
  48. return ADS1115_MV_4096 / VOLTMETER_PRESSURE_COEFFICIENT;
  49. case PAG_2048:
  50. return ADS1115_MV_2048 / VOLTMETER_PRESSURE_COEFFICIENT;
  51. case PAG_1024:
  52. return ADS1115_MV_1024 / VOLTMETER_PRESSURE_COEFFICIENT;
  53. case PAG_512:
  54. return ADS1115_MV_512 / VOLTMETER_PRESSURE_COEFFICIENT;
  55. case PAG_256:
  56. return ADS1115_MV_256 / VOLTMETER_PRESSURE_COEFFICIENT;
  57. default:
  58. return ADS1115_MV_256 / VOLTMETER_PRESSURE_COEFFICIENT;
  59. };
  60. }
  61. uint8_t Voltmeter::getPGAEEEPROMAddr(voltmeterGain_t gain) {
  62. switch (gain) {
  63. case PAG_6144:
  64. return VOLTMETER_PAG_6144_CAL_ADDR;
  65. case PAG_4096:
  66. return VOLTMETER_PAG_4096_CAL_ADDR;
  67. case PAG_2048:
  68. return VOLTMETER_PAG_2048_CAL_ADDR;
  69. case PAG_1024:
  70. return VOLTMETER_PAG_1024_CAL_ADDR;
  71. case PAG_512:
  72. return VOLTMETER_PAG_512_CAL_ADDR;
  73. case PAG_256:
  74. return VOLTMETER_PAG_256_CAL_ADDR;
  75. default:
  76. return 0x00;
  77. };
  78. }
  79. uint16_t Voltmeter::getCoverTime(voltmeterRate_t rate) {
  80. switch (rate) {
  81. case RATE_8:
  82. return 1000 / 8;
  83. case RATE_16:
  84. return 1000 / 16;
  85. case RATE_32:
  86. return 1000 / 32;
  87. case RATE_64:
  88. return 1000 / 64;
  89. case RATE_128:
  90. return 1000 / 128;
  91. case RATE_250:
  92. return 1000 / 250;
  93. case RATE_475:
  94. return 1000 / 475;
  95. case RATE_860:
  96. return 1000 / 860;
  97. default:
  98. return 1000 / 128;
  99. };
  100. }
  101. Voltmeter::Voltmeter(uint8_t ads1115_addr, uint8_t eeprom_addr) {
  102. _ads1115_addr = ads1115_addr;
  103. _eeprom_addr = eeprom_addr;
  104. _gain = PAG_2048;
  105. _mode = SINGLESHOT;
  106. _rate = RATE_128;
  107. calibration_factor = 1;
  108. adc_raw = 0;
  109. resolution = getResolution(_gain);
  110. cover_time = getCoverTime(_rate);
  111. }
  112. void Voltmeter::setGain(voltmeterGain_t gain) {
  113. uint16_t reg_value = 0;
  114. bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, &reg_value);
  115. if (result == false) {
  116. return;
  117. }
  118. reg_value &= ~(0b0111 << 9);
  119. reg_value |= gain << 9;
  120. result = i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value);
  121. if (result) {
  122. _gain = gain;
  123. resolution = getResolution(gain);
  124. int16_t hope = 1;
  125. int16_t actual = 1;
  126. if (readCalibrationFromEEPROM(gain, &hope, &actual)) {
  127. calibration_factor = (double)hope / actual;
  128. }
  129. }
  130. }
  131. void Voltmeter::setRate(voltmeterRate_t rate) {
  132. uint16_t reg_value = 0;
  133. bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, &reg_value);
  134. if (result == false) {
  135. return;
  136. }
  137. reg_value &= ~(0b0111 << 5);
  138. reg_value |= rate << 5;
  139. result = i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value);
  140. if (result) {
  141. _rate = rate;
  142. cover_time = getCoverTime(_rate);
  143. }
  144. return;
  145. }
  146. void Voltmeter::setMode(voltmeterMode_t mode) {
  147. uint16_t reg_value = 0;
  148. bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, &reg_value);
  149. if (result == false) {
  150. return;
  151. }
  152. reg_value &= ~(0b0001 << 8);
  153. reg_value |= mode << 8;
  154. result = i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value);
  155. if (result) {
  156. _mode = mode;
  157. }
  158. return;
  159. }
  160. bool Voltmeter::isInConversion() {
  161. uint16_t value = 0x00;
  162. i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, &value);
  163. return (value & (1 << 15)) ? false : true;
  164. }
  165. void Voltmeter::startSingleConversion() {
  166. uint16_t reg_value = 0;
  167. bool result = i2cReadU16(_ads1115_addr, ADS1115_RA_CONFIG, &reg_value);
  168. if (result == false) {
  169. return;
  170. }
  171. reg_value &= ~(0b0001 << 15);
  172. reg_value |= 0x01 << 15;
  173. i2cWriteU16(_ads1115_addr, ADS1115_RA_CONFIG, reg_value);
  174. }
  175. float Voltmeter::getVoltage(bool calibration) {
  176. if (calibration) {
  177. return resolution * calibration_factor * getConversion() *
  178. VOLTMETER_MEASURING_DIR;
  179. } else {
  180. return resolution * getConversion() * VOLTMETER_MEASURING_DIR;
  181. }
  182. }
  183. int16_t Voltmeter::getAdcRaw() {
  184. uint16_t value = 0x00;
  185. i2cReadU16(_ads1115_addr, ADS1115_RA_CONVERSION, &value);
  186. adc_raw = value;
  187. return value;
  188. }
  189. int16_t Voltmeter::getConversion(uint16_t timeout) {
  190. if (_mode == SINGLESHOT) {
  191. startSingleConversion();
  192. delay(cover_time);
  193. uint64_t time = millis() + timeout;
  194. while (time > millis() && isInConversion())
  195. ;
  196. }
  197. return getAdcRaw();
  198. }
  199. bool Voltmeter::EEPORMWrite(uint8_t address, uint8_t* buff, uint8_t len) {
  200. return i2cWriteBytes(_eeprom_addr, address, buff, len);
  201. }
  202. bool Voltmeter::EEPORMRead(uint8_t address, uint8_t* buff, uint8_t len) {
  203. return i2cReadBytes(_eeprom_addr, address, buff, len);
  204. }
  205. bool Voltmeter::saveCalibration2EEPROM(voltmeterGain_t gain, int16_t hope,
  206. int16_t actual) {
  207. if (hope == 0 || actual == 0) {
  208. return false;
  209. }
  210. uint8_t buff[8];
  211. memset(buff, 0, 8);
  212. buff[0] = gain;
  213. buff[1] = hope >> 8;
  214. buff[2] = hope & 0xFF;
  215. buff[3] = actual >> 8;
  216. buff[4] = actual & 0xFF;
  217. for (uint8_t i = 0; i < 5; i++) {
  218. buff[5] ^= buff[i];
  219. }
  220. uint8_t addr = getPGAEEEPROMAddr(gain);
  221. return EEPORMWrite(addr, buff, 8);
  222. }
  223. bool Voltmeter::readCalibrationFromEEPROM(voltmeterGain_t gain, int16_t* hope,
  224. int16_t* actual) {
  225. uint8_t addr = getPGAEEEPROMAddr(gain);
  226. uint8_t buff[8];
  227. memset(buff, 0, 8);
  228. *hope = 1;
  229. *actual = 1;
  230. bool result = EEPORMRead(addr, buff, 8);
  231. if (result == false) {
  232. return false;
  233. }
  234. uint8_t xor_result = 0x00;
  235. for (uint8_t i = 0; i < 5; i++) {
  236. xor_result ^= buff[i];
  237. }
  238. if (xor_result != buff[5]) {
  239. return false;
  240. }
  241. *hope = (buff[1] << 8) | buff[2];
  242. *actual = (buff[3] << 8) | buff[4];
  243. return true;
  244. }