AMeter_ADS1115.ino 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Description: Measure current and display
  3. EEPROM (0x51) 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 "ammeter.h"
  8. #include "M5Stack.h"
  9. Ammeter ammeter;
  10. float page512_volt = 2000.0F;
  11. int16_t volt_raw_list[10];
  12. uint8_t raw_now_ptr = 0;
  13. int16_t adc_raw = 0;
  14. int16_t hope = 0.0;
  15. ammeterGain_t now_gain = PAG_512;
  16. void setup(void) {
  17. M5.begin();
  18. Wire.begin();
  19. ammeter.setMode(SINGLESHOT);
  20. ammeter.setRate(RATE_8);
  21. ammeter.setGain(PAG_512);
  22. hope = page512_volt / ammeter.resolution;
  23. // | PAG | Max Input Voltage(V) |
  24. // | PAG_6144 | 128 |
  25. // | PAG_4096 | 64 |
  26. // | PAG_2048 | 32 |
  27. // | PAG_512 | 16 |
  28. // | PAG_256 | 8 |
  29. M5.Lcd.fillScreen(BLACK);
  30. M5.Lcd.setTextFont(4);
  31. M5.Lcd.setCursor(52, 210);
  32. M5.Lcd.printf("2A");
  33. M5.Lcd.setCursor(230, 210);
  34. M5.Lcd.printf("SAVE");
  35. // bool result1 = ammeter.saveCalibration2EEPROM(PAG_256, 1024, 1024);
  36. // delay(10);
  37. }
  38. void loop(void) {
  39. M5.update();
  40. if (M5.BtnA.wasPressed()) {
  41. ammeter.setMode(SINGLESHOT);
  42. ammeter.setRate(RATE_8);
  43. ammeter.setGain(PAG_512);
  44. now_gain = PAG_512;
  45. hope = page512_volt / ammeter.resolution;
  46. for (uint8_t i = 0; i < 10; i++) {
  47. volt_raw_list[i] = 0;
  48. }
  49. }
  50. if (M5.BtnC.wasPressed()) {
  51. bool success = ammeter.saveCalibration2EEPROM(now_gain, hope, adc_raw);
  52. M5.Lcd.setCursor(230, 210);
  53. if (success) {
  54. M5.Lcd.setTextColor(GREEN, BLACK);
  55. } else {
  56. M5.Lcd.setTextColor(RED, BLACK);
  57. }
  58. M5.Lcd.printf("SAVE");
  59. delay(300);
  60. M5.Lcd.setCursor(230, 210);
  61. M5.Lcd.setTextColor(WHITE, BLACK);
  62. M5.Lcd.printf("SAVE");
  63. ammeter.setGain(now_gain);
  64. }
  65. float current = ammeter.getCurrent();
  66. volt_raw_list[raw_now_ptr] = ammeter.adc_raw;
  67. raw_now_ptr = (raw_now_ptr == 9) ? 0 : (raw_now_ptr + 1);
  68. int count = 0;
  69. int total = 0;
  70. for (uint8_t i = 0; i < 10; i++) {
  71. if (volt_raw_list[i] == 0) {
  72. continue ;
  73. }
  74. total += volt_raw_list[i];
  75. count += 1;
  76. }
  77. if (count == 0) {
  78. adc_raw = 0;
  79. } else {
  80. adc_raw = total / count;
  81. }
  82. M5.Lcd.setTextColor(WHITE, BLACK);
  83. M5.Lcd.setCursor(10, 10);
  84. M5.Lcd.printf("Hope volt: %.2f mA \r\n", page512_volt);
  85. M5.Lcd.setCursor(10, 40);
  86. M5.Lcd.printf("Hope ADC: %d \r\n", hope);
  87. M5.Lcd.setTextColor(WHITE, BLACK);
  88. M5.Lcd.setCursor(10, 80);
  89. M5.Lcd.printf("Cal volt: %.2f mA \r\n", current);
  90. M5.Lcd.setTextColor(WHITE, BLACK);
  91. M5.Lcd.setCursor(10, 110);
  92. M5.Lcd.printf("Cal ADC: %.0f \r\n", adc_raw * ammeter.calibration_factor);
  93. M5.Lcd.setCursor(10, 150);
  94. if (abs(adc_raw) <= hope * 1.005 && abs(adc_raw) >= hope * 0.995) {
  95. M5.Lcd.setTextColor(GREEN, BLACK);
  96. } else {
  97. M5.Lcd.setTextColor(RED, BLACK);
  98. }
  99. M5.Lcd.printf("RAW ADC: %d \r\n", adc_raw);
  100. }