ADS1118.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #ifndef ADS1118_h
  2. #define ADS1118_h
  3. #include "Arduino.h"
  4. #include <SPI.h>
  5. #include <stdint.h>
  6. /**
  7. * Union representing the "config register" in 3 ways:
  8. * bits, word (16 bits) and nibbles (4 bits)
  9. * (See the datasheet [1] for more information)
  10. */
  11. ///Union configuration register
  12. union Config {
  13. ///Structure of the config register of the ADS1118. (See datasheet [1])
  14. struct {
  15. uint8_t reserved:1; ///< "Reserved" bit
  16. uint8_t noOperation:2; ///< "NOP" bits
  17. uint8_t pullUp:1; ///< "PULL_UP_EN" bit
  18. uint8_t sensorMode:1; ///< "TS_MODE" bit
  19. uint8_t rate:3; ///< "DR" bits
  20. uint8_t operatingMode:1;///< "MODE" bit
  21. uint8_t pga:3; ///< "PGA" bits
  22. uint8_t mux:3; ///< "MUX" bits
  23. uint8_t singleStart:1; ///< "SS" bit
  24. } bits;
  25. uint16_t word; ///< Representation in word (16-bits) format
  26. struct {
  27. uint8_t lsb; ///< Byte LSB
  28. uint8_t msb; ///< Byte MSB
  29. } byte; ///< Representation in bytes (8-bits) format
  30. };
  31. /**
  32. * Class representing the ADS1118 sensor chip
  33. * @author Alvaro Salazar <alvaro@denkitronik.com>
  34. */
  35. class ADS1118 {
  36. public:
  37. void begin(); ///< This method initialize the SPI port and the config register
  38. #if defined(__AVR__)
  39. ADS1118(uint8_t io_pin_cs); ///< Constructor
  40. #elif defined(ESP32)
  41. ADS1118(uint8_t io_pin_cs, SPIClass *spi = &SPI); ///< Constructor
  42. void begin(uint8_t sclk, uint8_t miso, uint8_t mosi); ///< This method initialize the SPI port and the config register
  43. #endif
  44. double getTemperature(); ///< Getting the temperature in degrees celsius from the internal sensor of the ADS1118
  45. uint16_t getADCValue(uint8_t inputs); ///< Getting a sample from the specified input
  46. bool getADCValueNoWait(uint8_t pin_drdy, uint16_t &value);
  47. bool getMilliVoltsNoWait(uint8_t pin_drdy, double &volts); ///< Getting the millivolts from the settled inputs
  48. double getMilliVolts(uint8_t inputs); ///< Getting the millivolts from the specified inputs
  49. double getMilliVolts(); ///< Getting the millivolts from the settled inputs
  50. void decodeConfigRegister(union Config configRegister); ///< Decoding a configRegister structure and then print it out to the Serial port
  51. void setSamplingRate(uint8_t samplingRate); ///< Setting the sampling rate specified in the config register
  52. void setFullScaleRange(uint8_t fsr);///< Setting the full scale range in the config register
  53. void setContinuousMode(); ///< Setting to continuous adquisition mode
  54. void setSingleShotMode(); ///< Setting to single shot adquisition and power down mode
  55. void disablePullup(); ///< Disabling the internal pull-up resistor of the DOUT pin
  56. void enablePullup(); ///< Enabling the internal pull-up resistor of the DOUT pin
  57. void setInputSelected(uint8_t input);///< Setting the inputs to be adquired in the config register.
  58. //Input multiplexer configuration selection for bits "MUX"
  59. //Differential inputs
  60. const uint8_t DIFF_0_1 = 0b000; ///< Differential input: Vin=A0-A1
  61. const uint8_t DIFF_0_3 = 0b001; ///< Differential input: Vin=A0-A3
  62. const uint8_t DIFF_1_3 = 0b010; ///< Differential input: Vin=A1-A3
  63. const uint8_t DIFF_2_3 = 0b011; ///< Differential input: Vin=A2-A3
  64. //Single ended inputs
  65. const uint8_t AIN_0 = 0b100; ///< Single ended input: Vin=A0
  66. const uint8_t AIN_1 = 0b101; ///< Single ended input: Vin=A1
  67. const uint8_t AIN_2 = 0b110; ///< Single ended input: Vin=A2
  68. const uint8_t AIN_3 = 0b111; ///< Single ended input: Vin=A3
  69. union Config configRegister; ///< Config register
  70. //Bit constants
  71. const uint32_t SCLK = 2000000;///< ADS1118 SCLK frequency: 4000000 Hz Maximum for ADS1118
  72. // Used by "SS" bit
  73. const uint8_t START_NOW = 1; ///< Start of conversion in single-shot mode
  74. // Used by "TS_MODE" bit
  75. const uint8_t ADC_MODE = 0; ///< External (inputs) voltage reading mode
  76. const uint8_t TEMP_MODE = 1; ///< Internal temperature sensor reading mode
  77. // Used by "MODE" bit
  78. const uint8_t CONTINUOUS = 0; ///< Continuous conversion mode
  79. const uint8_t SINGLE_SHOT = 1; ///< Single-shot conversion and power down mode
  80. // Used by "PULL_UP_EN" bit
  81. const uint8_t DOUT_PULLUP = 1; ///< Internal pull-up resistor enabled for DOUT ***DEFAULT
  82. const uint8_t DOUT_NO_PULLUP = 0; ///< Internal pull-up resistor disabled
  83. // Used by "NOP" bits
  84. const uint8_t VALID_CFG = 0b01; ///< Data will be written to Config register
  85. const uint8_t NO_VALID_CFG= 0b00; ///< Data won't be written to Config register
  86. // Used by "Reserved" bit
  87. const uint8_t RESERVED = 1; ///< Its value is always 1, reserved
  88. /*Full scale range (FSR) selection by "PGA" bits.
  89. [Warning: this could increase the noise and the effective number of bits (ENOB). See tables above]*/
  90. const uint8_t FSR_6144 = 0b000; ///< Range: ±6.144 v. LSB SIZE = 187.5μV
  91. const uint8_t FSR_4096 = 0b001; ///< Range: ±4.096 v. LSB SIZE = 125μV
  92. const uint8_t FSR_2048 = 0b010; ///< Range: ±2.048 v. LSB SIZE = 62.5μV ***DEFAULT
  93. const uint8_t FSR_1024 = 0b011; ///< Range: ±1.024 v. LSB SIZE = 31.25μV
  94. const uint8_t FSR_0512 = 0b100; ///< Range: ±0.512 v. LSB SIZE = 15.625μV
  95. const uint8_t FSR_0256 = 0b111; ///< Range: ±0.256 v. LSB SIZE = 7.8125μV
  96. /*Sampling rate selection by "DR" bits.
  97. [Warning: this could increase the noise and the effective number of bits (ENOB). See tables above]*/
  98. const uint8_t RATE_8SPS = 0b000; ///< 8 samples/s, Tconv=125ms
  99. const uint8_t RATE_16SPS = 0b001; ///< 16 samples/s, Tconv=62.5ms
  100. const uint8_t RATE_32SPS = 0b010; ///< 32 samples/s, Tconv=31.25ms
  101. const uint8_t RATE_64SPS = 0b011; ///< 64 samples/s, Tconv=15.625ms
  102. const uint8_t RATE_128SPS = 0b100; ///< 128 samples/s, Tconv=7.8125ms
  103. const uint8_t RATE_250SPS = 0b101; ///< 250 samples/s, Tconv=4ms
  104. const uint8_t RATE_475SPS = 0b110; ///< 475 samples/s, Tconv=2.105ms
  105. const uint8_t RATE_860SPS = 0b111; ///< 860 samples/s, Tconv=1.163ms
  106. private:
  107. #if defined(ESP32)
  108. SPIClass *pSpi;
  109. #endif
  110. uint8_t lastSensorMode=3; ///< Last sensor mode selected (ADC_MODE or TEMP_MODE or none)
  111. uint8_t cs; ///< Chip select pin (choose one)
  112. const float pgaFSR[8] = {6.144, 4.096, 2.048, 1.024, 0.512, 0.256, 0.256, 0.256};
  113. const uint8_t CONV_TIME[8]={125, 63, 32, 16, 8, 4, 3, 2}; ///< Array containing the conversions time in ms
  114. /*
  115. Table 1. Noise in μVRMS (μVPP) at VDD = 3.3 V [1]
  116. DATA RATE FSR (Full-Scale Range)
  117. (SPS) ±6.144 V ±4.096 V ±2.048 V ±1.024 V ±0.512 V ±0.256 V
  118. 8 187.5 (187.5) 125 (125) 62.5 (62.5) 31.25 (31.25) 15.62 (15.62) 7.81 (7.81)
  119. 16 187.5 (187.5) 125 (125) 62.5 (62.5) 31.25 (31.25) 15.62 (15.62) 7.81 (7.81)
  120. 32 187.5 (187.5) 125 (125) 62.5 (62.5) 31.25 (31.25) 15.62 (15.62) 7.81 (7.81)
  121. 64 187.5 (187.5) 125 (125) 62.5 (62.5) 31.25 (31.25) 15.62 (15.62) 7.81 (7.81)
  122. 128 187.5 (187.5) 125 (125) 62.5 (62.5) 31.25 (31.25) 15.62 (15.62) 7.81 (12.35)
  123. 250 187.5 (252.09) 125 (148.28) 62.5 (84.03) 31.25 (39.54) 15.62 (16.06) 7.81 (18.53)
  124. 475 187.5 (266.92) 125 (227.38) 62.5 (79.08) 31.25 (56.84) 15.62 (32.13) 7.81 (25.95)
  125. 860 187.5 (430.06) 125 (266.93) 62.5 (118.63) 31.25 (64.26) 15.62 (40.78) 7.81 (35.83)
  126. Table 2. ENOB from RMS Noise (Noise-Free Bits from Peak-to-Peak Noise) at VDD = 3.3 V
  127. DATA RATE FSR (Full-Scale Range)
  128. (SPS) ±6.144 V ±4.096 V ±2.048 V ±1.024 V ±0.512 V ±0.256 V
  129. 8 16 (16) 16 (16) 16 (16) 16 (16) 16 (16) 16 (16)
  130. 16 16 (16) 16 (16) 16 (16) 16 (16) 16 (16) 16 (16)
  131. 32 16 (16) 16 (16) 16 (16) 16 (16) 16 (16) 16 (16)
  132. 64 16 (16) 16 (16) 16 (16) 16 (16) 16 (16) 16 (16)
  133. 128 16 (16) 16 (16) 16 (16) 16 (16) 16 (16) 16 (15.33)
  134. 250 16 (15.57) 16 (15.75) 16 (15.57) 16 (15.66) 16 (15.96) 16 (14.75)
  135. 475 16 (15.49) 16 (15.13) 16 (15.66) 16 (15.13) 16 (14.95) 16 (14.26)
  136. 860 16 (14.8) 16 (14.9) 16 (15.07) 16 (14.95) 16 (14.61) 16 (13.8)
  137. [1] Texas Instruments, "ADS1118 Ultrasmall, Low-Power, SPI™-Compatible, 16-Bit Analog-to-Digital
  138. Converter with Internal Reference and Temperature Sensor", ADS1118 datasheet, SBAS457E [OCTOBER 2010–REVISED OCTOBER 2015].
  139. Note: This information is taken from http://www.ti.com
  140. Copyright © 2010–2015, Texas Instruments Incorporated
  141. */
  142. };
  143. #endif