ADC_ADS1100.ino 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Description: Use ADC Unit to convert 0 ~ 12V analog voltage into 16-bit data and display it on the screen.
  3. */
  4. #include <M5Stack.h>
  5. #include <Wire.h>
  6. #include "ADS1100.h"
  7. ADS1100 ads;
  8. void setup(void)
  9. {
  10. M5.begin(true, false, false);
  11. M5.Power.begin();
  12. Serial.begin(115200);
  13. M5.Lcd.fillScreen(BLACK);
  14. M5.Lcd.setTextColor(ORANGE);
  15. // The address can be changed making the option of connecting multiple devices
  16. ads.getAddr_ADS1100(ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND)
  17. // The ADC gain (PGA), Device operating mode, Data rate
  18. // can be changed via the following functions
  19. ads.setGain(GAIN_ONE); // 1x gain(default)
  20. // ads.setGain(GAIN_TWO); // 2x gain
  21. // ads.setGain(GAIN_FOUR); // 4x gain
  22. // ads.setGain(GAIN_EIGHT); // 8x gain
  23. ads.setMode(MODE_CONTIN); // Continuous conversion mode (default)
  24. // ads.setMode(MODE_SINGLE); // Single-conversion mode
  25. ads.setRate(RATE_8); // 8SPS (default)
  26. // ads.setRate(RATE_16); // 16SPS
  27. // ads.setRate(RATE_32); // 32SPS
  28. // ads.setRate(RATE_128); // 128SPS
  29. ads.setOSMode(OSMODE_SINGLE); // Set to start a single-conversion
  30. ads.begin();
  31. }
  32. void loop(void)
  33. {
  34. byte error;
  35. int8_t address;
  36. address = ads.ads_i2cAddress;
  37. // The i2c_scanner uses the return value of
  38. // the Write.endTransmisstion to see if
  39. // a device did acknowledge to the address.
  40. Wire.beginTransmission(address);
  41. error = Wire.endTransmission();
  42. if (error == 0)
  43. {
  44. int16_t result;
  45. Serial.println("Getting Differential Reading from ADS1100");
  46. Serial.println(" ");
  47. result = ads.Measure_Differential();
  48. Serial.print("Digital Value of Analog Input between Channel 0 and 1: ");
  49. Serial.println(result);
  50. M5.Lcd.fillScreen(BLACK);
  51. char data[20] = { 0 };
  52. sprintf(data, "%d", result);
  53. M5.Lcd.drawCentreString(data, 160, 100, 4);
  54. Serial.println(" ");
  55. Serial.println(" *************************** ");
  56. Serial.println(" ");
  57. }
  58. else
  59. {
  60. Serial.println("ADS1100 Disconnected!");
  61. Serial.println(" ");
  62. Serial.println(" ************ ");
  63. Serial.println(" ");
  64. M5.Lcd.setTextFont(4);
  65. M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
  66. M5.Lcd.drawString("No Found ADC sensor.",20, 100, 4);
  67. }
  68. delay(1000);
  69. }