ADS1115.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // I2Cdev library collection - ADS1115 I2C device class header file
  2. // Based on Texas Instruments ADS1113/4/5 datasheet, May 2009 (SBAS444B, revised October 2009)
  3. // Note that the ADS1115 uses 16-bit registers, not 8-bit registers.
  4. // 8/2/2011 by Jeff Rowberg <jeff@rowberg.net>
  5. // Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
  6. //
  7. // Changelog:
  8. // 2013-05-05 - Add debug information. Clean up Single Shot implementation
  9. // 2011-10-29 - added getDifferentialx() methods, F. Farzanegan
  10. // 2011-08-02 - initial release
  11. /* ============================================
  12. I2Cdev device library code is placed under the MIT license
  13. Copyright (c) 2011 Jeff Rowberg
  14. Permission is hereby granted, free of charge, to any person obtaining a copy
  15. of this software and associated documentation files (the "Software"), to deal
  16. in the Software without restriction, including without limitation the rights
  17. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. copies of the Software, and to permit persons to whom the Software is
  19. furnished to do so, subject to the following conditions:
  20. The above copyright notice and this permission notice shall be included in
  21. all copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  27. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  28. THE SOFTWARE.
  29. ===============================================
  30. */
  31. #ifndef _ADS1115_H_
  32. #define _ADS1115_H_
  33. #include "I2Cdev.h"
  34. // -----------------------------------------------------------------------------
  35. // Arduino-style "Serial.print" debug constant (uncomment to enable)
  36. // -----------------------------------------------------------------------------
  37. //#define ADS1115_SERIAL_DEBUG
  38. #define ADS1115_ADDRESS_ADDR_GND 0x48 // address pin low (GND)
  39. #define ADS1115_ADDRESS_ADDR_VDD 0x49 // address pin high (VCC)
  40. #define ADS1115_ADDRESS_ADDR_SDA 0x4A // address pin tied to SDA pin
  41. #define ADS1115_ADDRESS_ADDR_SCL 0x4B // address pin tied to SCL pin
  42. #define ADS1115_DEFAULT_ADDRESS ADS1115_ADDRESS_ADDR_GND
  43. #define ADS1115_RA_CONVERSION 0x00
  44. #define ADS1115_RA_CONFIG 0x01
  45. #define ADS1115_RA_LO_THRESH 0x02
  46. #define ADS1115_RA_HI_THRESH 0x03
  47. #define ADS1115_CFG_OS_BIT 15
  48. #define ADS1115_CFG_MUX_BIT 14
  49. #define ADS1115_CFG_MUX_LENGTH 3
  50. #define ADS1115_CFG_PGA_BIT 11
  51. #define ADS1115_CFG_PGA_LENGTH 3
  52. #define ADS1115_CFG_MODE_BIT 8
  53. #define ADS1115_CFG_DR_BIT 7
  54. #define ADS1115_CFG_DR_LENGTH 3
  55. #define ADS1115_CFG_COMP_MODE_BIT 4
  56. #define ADS1115_CFG_COMP_POL_BIT 3
  57. #define ADS1115_CFG_COMP_LAT_BIT 2
  58. #define ADS1115_CFG_COMP_QUE_BIT 1
  59. #define ADS1115_CFG_COMP_QUE_LENGTH 2
  60. #define ADS1115_MUX_P0_N1 0x00 // default
  61. #define ADS1115_MUX_P0_N3 0x01
  62. #define ADS1115_MUX_P1_N3 0x02
  63. #define ADS1115_MUX_P2_N3 0x03
  64. #define ADS1115_MUX_P0_NG 0x04
  65. #define ADS1115_MUX_P1_NG 0x05
  66. #define ADS1115_MUX_P2_NG 0x06
  67. #define ADS1115_MUX_P3_NG 0x07
  68. #define ADS1115_PGA_6P144 0x00
  69. #define ADS1115_PGA_4P096 0x01
  70. #define ADS1115_PGA_2P048 0x02 // default
  71. #define ADS1115_PGA_1P024 0x03
  72. #define ADS1115_PGA_0P512 0x04
  73. #define ADS1115_PGA_0P256 0x05
  74. #define ADS1115_PGA_0P256B 0x06
  75. #define ADS1115_PGA_0P256C 0x07
  76. #define ADS1115_MV_6P144 0.187500
  77. #define ADS1115_MV_4P096 0.125000
  78. #define ADS1115_MV_2P048 0.062500 // default
  79. #define ADS1115_MV_1P024 0.031250
  80. #define ADS1115_MV_0P512 0.015625
  81. #define ADS1115_MV_0P256 0.007813
  82. #define ADS1115_MV_0P256B 0.007813
  83. #define ADS1115_MV_0P256C 0.007813
  84. #define ADS1115_MODE_CONTINUOUS 0x00
  85. #define ADS1115_MODE_SINGLESHOT 0x01 // default
  86. #define ADS1115_RATE_8 0x00
  87. #define ADS1115_RATE_16 0x01
  88. #define ADS1115_RATE_32 0x02
  89. #define ADS1115_RATE_64 0x03
  90. #define ADS1115_RATE_128 0x04 // default
  91. #define ADS1115_RATE_250 0x05
  92. #define ADS1115_RATE_475 0x06
  93. #define ADS1115_RATE_860 0x07
  94. #define ADS1115_COMP_MODE_HYSTERESIS 0x00 // default
  95. #define ADS1115_COMP_MODE_WINDOW 0x01
  96. #define ADS1115_COMP_POL_ACTIVE_LOW 0x00 // default
  97. #define ADS1115_COMP_POL_ACTIVE_HIGH 0x01
  98. #define ADS1115_COMP_LAT_NON_LATCHING 0x00 // default
  99. #define ADS1115_COMP_LAT_LATCHING 0x01
  100. #define ADS1115_COMP_QUE_ASSERT1 0x00
  101. #define ADS1115_COMP_QUE_ASSERT2 0x01
  102. #define ADS1115_COMP_QUE_ASSERT4 0x02
  103. #define ADS1115_COMP_QUE_DISABLE 0x03 // default
  104. // -----------------------------------------------------------------------------
  105. // Arduino-style "Serial.print" debug constant (uncomment to enable)
  106. // -----------------------------------------------------------------------------
  107. //#define ADS1115_SERIAL_DEBUG
  108. class ADS1115 {
  109. public:
  110. ADS1115();
  111. ADS1115(uint8_t address);
  112. void initialize();
  113. bool testConnection();
  114. // SINGLE SHOT utilities
  115. bool pollConversion(uint16_t max_retries);
  116. void triggerConversion();
  117. // Read the current CONVERSION register
  118. int16_t getConversion(bool triggerAndPoll=true);
  119. // Differential
  120. int16_t getConversionP0N1();
  121. int16_t getConversionP0N3();
  122. int16_t getConversionP1N3();
  123. int16_t getConversionP2N3();
  124. // Single-ended
  125. int16_t getConversionP0GND();
  126. int16_t getConversionP1GND();
  127. int16_t getConversionP2GND();
  128. int16_t getConversionP3GND();
  129. // Utility
  130. float getMilliVolts(bool triggerAndPoll=true);
  131. float getMvPerCount();
  132. // CONFIG register
  133. bool isConversionReady();
  134. uint8_t getMultiplexer();
  135. void setMultiplexer(uint8_t mux);
  136. uint8_t getGain();
  137. void setGain(uint8_t gain);
  138. bool getMode();
  139. void setMode(bool mode);
  140. uint8_t getRate();
  141. void setRate(uint8_t rate);
  142. bool getComparatorMode();
  143. void setComparatorMode(bool mode);
  144. bool getComparatorPolarity();
  145. void setComparatorPolarity(bool polarity);
  146. bool getComparatorLatchEnabled();
  147. void setComparatorLatchEnabled(bool enabled);
  148. uint8_t getComparatorQueueMode();
  149. void setComparatorQueueMode(uint8_t mode);
  150. void setConversionReadyPinMode();
  151. // *_THRESH registers
  152. int16_t getLowThreshold();
  153. void setLowThreshold(int16_t threshold);
  154. int16_t getHighThreshold();
  155. void setHighThreshold(int16_t threshold);
  156. // DEBUG
  157. void showConfigRegister();
  158. private:
  159. uint8_t devAddr;
  160. uint16_t buffer[2];
  161. bool devMode;
  162. uint8_t muxMode;
  163. uint8_t pgaMode;
  164. };
  165. #endif /* _ADS1115_H_ */