VMeter_ADS1115.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Description: Measure voltage and display
  3. EEPROM (0x53) has built-in calibration parameters when leaving the factory.
  4. Please do not write to the EEPROM, otherwise the calibration data will be overwritten and the measurement results will be inaccurate
  5. */
  6. #include <Wire.h>
  7. #include "voltmeter.h"
  8. #include "M5Stack.h"
  9. Voltmeter voltmeter;
  10. float page512_volt = 5000.0F;
  11. float page4096_volt = 60000.0F;
  12. int16_t volt_raw_list[10];
  13. uint8_t raw_now_ptr = 0;
  14. int16_t adc_raw = 0;
  15. int16_t hope = 0.0;
  16. voltmeterGain_t now_gain = PAG_512;
  17. void setup(void) {
  18. M5.begin();
  19. Wire.begin();
  20. voltmeter.setMode(SINGLESHOT);
  21. voltmeter.setRate(RATE_8);
  22. voltmeter.setGain(PAG_512);
  23. hope = page512_volt / voltmeter.resolution;
  24. // | PAG | Max Input Voltage(V) |
  25. // | PAG_6144 | 128 |
  26. // | PAG_4096 | 64 |
  27. // | PAG_2048 | 32 |
  28. // | PAG_512 | 16 |
  29. // | PAG_256 | 8 |
  30. M5.Lcd.fillScreen(BLACK);
  31. M5.Lcd.setTextFont(4);
  32. M5.Lcd.setCursor(52, 210);
  33. M5.Lcd.printf("5V");
  34. M5.Lcd.setCursor(138, 210);
  35. M5.Lcd.printf("60V");
  36. M5.Lcd.setCursor(230, 210);
  37. M5.Lcd.printf("SAVE");
  38. // bool result1 = voltmeter.saveCalibration2EEPROM(PAG_256, 1024, 1024);
  39. // delay(10);
  40. }
  41. void loop(void) {
  42. M5.update();
  43. if (M5.BtnA.wasPressed()) {
  44. voltmeter.setMode(SINGLESHOT);
  45. voltmeter.setRate(RATE_8);
  46. voltmeter.setGain(PAG_512);
  47. now_gain = PAG_512;
  48. hope = page512_volt / voltmeter.resolution;
  49. for (uint8_t i = 0; i < 10; i++) {
  50. volt_raw_list[i] = 0;
  51. }
  52. }
  53. if (M5.BtnB.wasPressed()) {
  54. voltmeter.setMode(SINGLESHOT);
  55. voltmeter.setRate(RATE_8);
  56. voltmeter.setGain(PAG_4096);
  57. now_gain = PAG_4096;
  58. hope = page4096_volt / voltmeter.resolution;
  59. for (uint8_t i = 0; i < 10; i++) {
  60. volt_raw_list[i] = 0;
  61. }
  62. }
  63. // if (M5.BtnC.wasPressed()) {
  64. // bool success = voltmeter.saveCalibration2EEPROM(now_gain, hope, adc_raw);
  65. // M5.Lcd.setCursor(230, 210);
  66. //
  67. // if (success) {
  68. // M5.Lcd.setTextColor(GREEN, BLACK);
  69. // } else {
  70. // M5.Lcd.setTextColor(RED, BLACK);
  71. // }
  72. //
  73. // M5.Lcd.printf("SAVE");
  74. //
  75. // delay(300);
  76. // M5.Lcd.setCursor(230, 210);
  77. // M5.Lcd.setTextColor(WHITE, BLACK);
  78. // M5.Lcd.printf("SAVE");
  79. //
  80. // voltmeter.setGain(now_gain);
  81. // }
  82. voltmeter.getVoltage();
  83. volt_raw_list[raw_now_ptr] = voltmeter.adc_raw;
  84. raw_now_ptr = (raw_now_ptr == 9) ? 0 : (raw_now_ptr + 1);
  85. int count = 0;
  86. int total = 0;
  87. for (uint8_t i = 0; i < 10; i++) {
  88. if (volt_raw_list[i] == 0) {
  89. continue ;
  90. }
  91. total += volt_raw_list[i];
  92. count += 1;
  93. }
  94. if (count == 0) {
  95. adc_raw = 0;
  96. } else {
  97. adc_raw = total / count;
  98. }
  99. M5.Lcd.setTextColor(WHITE, BLACK);
  100. if (now_gain == PAG_512) {
  101. M5.Lcd.setCursor(10, 10);
  102. M5.Lcd.printf("Hope volt: %.2f mv \r\n", page512_volt);
  103. } else {
  104. M5.Lcd.setCursor(10, 10);
  105. M5.Lcd.printf("Hope volt: %.2f mv \r\n", page4096_volt);
  106. }
  107. M5.Lcd.setCursor(10, 40);
  108. M5.Lcd.printf("Hope ADC: %d \r\n", hope);
  109. M5.Lcd.setTextColor(WHITE, BLACK);
  110. M5.Lcd.setCursor(10, 80);
  111. M5.Lcd.printf("Cal volt: %.2f mv \r\n", adc_raw * voltmeter.resolution * voltmeter.calibration_factor);
  112. M5.Lcd.setTextColor(WHITE, BLACK);
  113. M5.Lcd.setCursor(10, 110);
  114. M5.Lcd.printf("Cal ADC: %.0f \r\n", adc_raw * voltmeter.calibration_factor);
  115. M5.Lcd.setCursor(10, 150);
  116. if (adc_raw <= hope * 1.001 && adc_raw >= hope * 0.999) {
  117. M5.Lcd.setTextColor(GREEN, BLACK);
  118. } else {
  119. M5.Lcd.setTextColor(RED, BLACK);
  120. }
  121. M5.Lcd.printf("RAW ADC: %d \r\n", adc_raw);
  122. // M5.Lcd.setCursor(10, 110);
  123. // M5.Lcd.printf("Actual RAW: %d \r\n", adc_raw);
  124. // // M5.Lcd.printf("Actual RAW: %d \r\n", voltmeter.adc_raw);
  125. // M5.Lcd.setCursor(10, 140);
  126. // M5.Lcd.printf("Hope RAW: %d \r\n", hope);
  127. }