ADS1115.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. // I2Cdev library collection - ADS1115 I2C device class
  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. Rename methods to match datasheet.
  9. // 2011-11-06 - added getVoltage, F. Farzanegan
  10. // 2011-10-29 - added getDifferentialx() methods, F. Farzanegan
  11. // 2011-08-02 - initial release
  12. /* ============================================
  13. I2Cdev device library code is placed under the MIT license
  14. Copyright (c) 2011 Jeff Rowberg
  15. Permission is hereby granted, free of charge, to any person obtaining a copy
  16. of this software and associated documentation files (the "Software"), to deal
  17. in the Software without restriction, including without limitation the rights
  18. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  19. copies of the Software, and to permit persons to whom the Software is
  20. furnished to do so, subject to the following conditions:
  21. The above copyright notice and this permission notice shall be included in
  22. all copies or substantial portions of the Software.
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. THE SOFTWARE.
  30. ===============================================
  31. */
  32. #include "ADS1115.h"
  33. /** Default constructor, uses default I2C address.
  34. * @see ADS1115_DEFAULT_ADDRESS
  35. */
  36. ADS1115::ADS1115() {
  37. devAddr = ADS1115_DEFAULT_ADDRESS;
  38. }
  39. /** Specific address constructor.
  40. * @param address I2C address
  41. * @see ADS1115_DEFAULT_ADDRESS
  42. * @see ADS1115_ADDRESS_ADDR_GND
  43. * @see ADS1115_ADDRESS_ADDR_VDD
  44. * @see ADS1115_ADDRESS_ADDR_SDA
  45. * @see ADS1115_ADDRESS_ADDR_SDL
  46. */
  47. ADS1115::ADS1115(uint8_t address) {
  48. devAddr = address;
  49. }
  50. /** Power on and prepare for general usage.
  51. * This device is ready to use automatically upon power-up. It defaults to
  52. * single-shot read mode, P0/N1 mux, 2.048v gain, 128 samples/sec, default
  53. * comparator with hysterysis, active-low polarity, non-latching comparator,
  54. * and comparater-disabled operation.
  55. */
  56. void ADS1115::initialize() {
  57. setMultiplexer(ADS1115_MUX_P0_N1);
  58. setGain(ADS1115_PGA_2P048);
  59. setMode(ADS1115_MODE_SINGLESHOT);
  60. setRate(ADS1115_RATE_128);
  61. setComparatorMode(ADS1115_COMP_MODE_HYSTERESIS);
  62. setComparatorPolarity(ADS1115_COMP_POL_ACTIVE_LOW);
  63. setComparatorLatchEnabled(ADS1115_COMP_LAT_NON_LATCHING);
  64. setComparatorQueueMode(ADS1115_COMP_QUE_DISABLE);
  65. }
  66. /** Verify the I2C connection.
  67. * Make sure the device is connected and responds as expected.
  68. * @return True if connection is valid, false otherwise
  69. */
  70. bool ADS1115::testConnection() {
  71. return I2Cdev::readWord(devAddr, ADS1115_RA_CONVERSION, buffer) == 1;
  72. }
  73. /** Poll the operational status bit until the conversion is finished
  74. * Retry at most 'max_retries' times
  75. * conversion is finished, then return true;
  76. * @see ADS1115_CFG_OS_BIT
  77. * @return True if data is available, false otherwise
  78. */
  79. bool ADS1115::pollConversion(uint16_t max_retries) {
  80. for(uint16_t i = 0; i < max_retries; i++) {
  81. if (isConversionReady()) return true;
  82. }
  83. return false;
  84. }
  85. /** Read differential value based on current MUX configuration.
  86. * The default MUX setting sets the device to get the differential between the
  87. * AIN0 and AIN1 pins. There are 8 possible MUX settings, but if you are using
  88. * all four input pins as single-end voltage sensors, then the default option is
  89. * not what you want; instead you will need to set the MUX to compare the
  90. * desired AIN* pin with GND. There are shortcut methods (getConversion*) to do
  91. * this conveniently, but you can also do it manually with setMultiplexer()
  92. * followed by this method.
  93. *
  94. * In single-shot mode, this register may not have fresh data. You need to write
  95. * a 1 bit to the MSB of the CONFIG register to trigger a single read/conversion
  96. * before this will be populated with fresh data. This technique is not as
  97. * effortless, but it has enormous potential to save power by only running the
  98. * comparison circuitry when needed.
  99. *
  100. * @param triggerAndPoll If true (and only in singleshot mode) the conversion trigger
  101. * will be executed and the conversion results will be polled.
  102. * @return 16-bit signed differential value
  103. * @see getConversionP0N1();
  104. * @see getConversionPON3();
  105. * @see getConversionP1N3();
  106. * @see getConversionP2N3();
  107. * @see getConversionP0GND();
  108. * @see getConversionP1GND();
  109. * @see getConversionP2GND();
  110. * @see getConversionP3GND);
  111. * @see setMultiplexer();
  112. * @see ADS1115_RA_CONVERSION
  113. * @see ADS1115_MUX_P0_N1
  114. * @see ADS1115_MUX_P0_N3
  115. * @see ADS1115_MUX_P1_N3
  116. * @see ADS1115_MUX_P2_N3
  117. * @see ADS1115_MUX_P0_NG
  118. * @see ADS1115_MUX_P1_NG
  119. * @see ADS1115_MUX_P2_NG
  120. * @see ADS1115_MUX_P3_NG
  121. */
  122. int16_t ADS1115::getConversion(bool triggerAndPoll) {
  123. if (triggerAndPoll && devMode == ADS1115_MODE_SINGLESHOT) {
  124. triggerConversion();
  125. pollConversion(I2CDEV_DEFAULT_READ_TIMEOUT);
  126. }
  127. I2Cdev::readWord(devAddr, ADS1115_RA_CONVERSION, buffer);
  128. return buffer[0];
  129. }
  130. /** Get AIN0/N1 differential.
  131. * This changes the MUX setting to AIN0/N1 if necessary, triggers a new
  132. * measurement (also only if necessary), then gets the differential value
  133. * currently in the CONVERSION register.
  134. * @return 16-bit signed differential value
  135. * @see getConversion()
  136. */
  137. int16_t ADS1115::getConversionP0N1() {
  138. if (muxMode != ADS1115_MUX_P0_N1) setMultiplexer(ADS1115_MUX_P0_N1);
  139. return getConversion();
  140. }
  141. /** Get AIN0/N3 differential.
  142. * This changes the MUX setting to AIN0/N3 if necessary, triggers a new
  143. * measurement (also only if necessary), then gets the differential value
  144. * currently in the CONVERSION register.
  145. * @return 16-bit signed differential value
  146. * @see getConversion()
  147. */
  148. int16_t ADS1115::getConversionP0N3() {
  149. if (muxMode != ADS1115_MUX_P0_N3) setMultiplexer(ADS1115_MUX_P0_N3);
  150. return getConversion();
  151. }
  152. /** Get AIN1/N3 differential.
  153. * This changes the MUX setting to AIN1/N3 if necessary, triggers a new
  154. * measurement (also only if necessary), then gets the differential value
  155. * currently in the CONVERSION register.
  156. * @return 16-bit signed differential value
  157. * @see getConversion()
  158. */
  159. int16_t ADS1115::getConversionP1N3() {
  160. if (muxMode != ADS1115_MUX_P1_N3) setMultiplexer(ADS1115_MUX_P1_N3);
  161. return getConversion();
  162. }
  163. /** Get AIN2/N3 differential.
  164. * This changes the MUX setting to AIN2/N3 if necessary, triggers a new
  165. * measurement (also only if necessary), then gets the differential value
  166. * currently in the CONVERSION register.
  167. * @return 16-bit signed differential value
  168. * @see getConversion()
  169. */
  170. int16_t ADS1115::getConversionP2N3() {
  171. if (muxMode != ADS1115_MUX_P2_N3) setMultiplexer(ADS1115_MUX_P2_N3);
  172. return getConversion();
  173. }
  174. /** Get AIN0/GND differential.
  175. * This changes the MUX setting to AIN0/GND if necessary, triggers a new
  176. * measurement (also only if necessary), then gets the differential value
  177. * currently in the CONVERSION register.
  178. * @return 16-bit signed differential value
  179. * @see getConversion()
  180. */
  181. int16_t ADS1115::getConversionP0GND() {
  182. if (muxMode != ADS1115_MUX_P0_NG) setMultiplexer(ADS1115_MUX_P0_NG);
  183. return getConversion();
  184. }
  185. /** Get AIN1/GND differential.
  186. * This changes the MUX setting to AIN1/GND if necessary, triggers a new
  187. * measurement (also only if necessary), then gets the differential value
  188. * currently in the CONVERSION register.
  189. * @return 16-bit signed differential value
  190. * @see getConversion()
  191. */
  192. int16_t ADS1115::getConversionP1GND() {
  193. if (muxMode != ADS1115_MUX_P1_NG) setMultiplexer(ADS1115_MUX_P1_NG);
  194. return getConversion();
  195. }
  196. /** Get AIN2/GND differential.
  197. * This changes the MUX setting to AIN2/GND if necessary, triggers a new
  198. * measurement (also only if necessary), then gets the differential value
  199. * currently in the CONVERSION register.
  200. * @return 16-bit signed differential value
  201. * @see getConversion()
  202. */
  203. int16_t ADS1115::getConversionP2GND() {
  204. if (muxMode != ADS1115_MUX_P2_NG) setMultiplexer(ADS1115_MUX_P2_NG);
  205. return getConversion();
  206. }
  207. /** Get AIN3/GND differential.
  208. * This changes the MUX setting to AIN3/GND if necessary, triggers a new
  209. * measurement (also only if necessary), then gets the differential value
  210. * currently in the CONVERSION register.
  211. * @return 16-bit signed differential value
  212. * @see getConversion()
  213. */
  214. int16_t ADS1115::getConversionP3GND() {
  215. if (muxMode != ADS1115_MUX_P3_NG) setMultiplexer(ADS1115_MUX_P3_NG);
  216. return getConversion();
  217. }
  218. /** Get the current voltage reading
  219. * Read the current differential and return it multiplied
  220. * by the constant for the current gain. mV is returned to
  221. * increase the precision of the voltage
  222. * @param triggerAndPoll If true (and only in singleshot mode) the conversion trigger
  223. * will be executed and the conversion results will be polled.
  224. */
  225. float ADS1115::getMilliVolts(bool triggerAndPoll) {
  226. switch (pgaMode) {
  227. case ADS1115_PGA_6P144:
  228. return (getConversion(triggerAndPoll) * ADS1115_MV_6P144);
  229. break;
  230. case ADS1115_PGA_4P096:
  231. return (getConversion(triggerAndPoll) * ADS1115_MV_4P096);
  232. break;
  233. case ADS1115_PGA_2P048:
  234. return (getConversion(triggerAndPoll) * ADS1115_MV_2P048);
  235. break;
  236. case ADS1115_PGA_1P024:
  237. return (getConversion(triggerAndPoll) * ADS1115_MV_1P024);
  238. break;
  239. case ADS1115_PGA_0P512:
  240. return (getConversion(triggerAndPoll) * ADS1115_MV_0P512);
  241. break;
  242. case ADS1115_PGA_0P256:
  243. case ADS1115_PGA_0P256B:
  244. case ADS1115_PGA_0P256C:
  245. return (getConversion(triggerAndPoll) * ADS1115_MV_0P256);
  246. break;
  247. }
  248. }
  249. /**
  250. * Return the current multiplier for the PGA setting.
  251. *
  252. * This may be directly retreived by using getMilliVolts(),
  253. * but this causes an independent read. This function could
  254. * be used to average a number of reads from the getConversion()
  255. * getConversionx() functions and cut downon the number of
  256. * floating-point calculations needed.
  257. *
  258. */
  259. float ADS1115::getMvPerCount() {
  260. switch (pgaMode) {
  261. case ADS1115_PGA_6P144:
  262. return ADS1115_MV_6P144;
  263. break;
  264. case ADS1115_PGA_4P096:
  265. return ADS1115_MV_4P096;
  266. break;
  267. case ADS1115_PGA_2P048:
  268. return ADS1115_MV_2P048;
  269. break;
  270. case ADS1115_PGA_1P024:
  271. return ADS1115_MV_1P024;
  272. break;
  273. case ADS1115_PGA_0P512:
  274. return ADS1115_MV_0P512;
  275. break;
  276. case ADS1115_PGA_0P256:
  277. case ADS1115_PGA_0P256B:
  278. case ADS1115_PGA_0P256C:
  279. return ADS1115_MV_0P256;
  280. break;
  281. }
  282. }
  283. // CONFIG register
  284. /** Get operational status.
  285. * @return Current operational status (false for active conversion, true for inactive)
  286. * @see ADS1115_RA_CONFIG
  287. * @see ADS1115_CFG_OS_BIT
  288. */
  289. bool ADS1115::isConversionReady() {
  290. I2Cdev::readBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_OS_BIT, buffer);
  291. return buffer[0];
  292. }
  293. /** Trigger a new conversion.
  294. * Writing to this bit will only have effect while in power-down mode (no conversions active).
  295. * @see ADS1115_RA_CONFIG
  296. * @see ADS1115_CFG_OS_BIT
  297. */
  298. void ADS1115::triggerConversion() {
  299. I2Cdev::writeBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_OS_BIT, 1);
  300. }
  301. /** Get multiplexer connection.
  302. * @return Current multiplexer connection setting
  303. * @see ADS1115_RA_CONFIG
  304. * @see ADS1115_CFG_MUX_BIT
  305. * @see ADS1115_CFG_MUX_LENGTH
  306. */
  307. uint8_t ADS1115::getMultiplexer() {
  308. I2Cdev::readBitsW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_MUX_BIT, ADS1115_CFG_MUX_LENGTH, buffer);
  309. muxMode = (uint8_t)buffer[0];
  310. return muxMode;
  311. }
  312. /** Set multiplexer connection. Continous mode may fill the conversion register
  313. * with data before the MUX setting has taken effect. A stop/start of the conversion
  314. * is done to reset the values.
  315. * @param mux New multiplexer connection setting
  316. * @see ADS1115_MUX_P0_N1
  317. * @see ADS1115_MUX_P0_N3
  318. * @see ADS1115_MUX_P1_N3
  319. * @see ADS1115_MUX_P2_N3
  320. * @see ADS1115_MUX_P0_NG
  321. * @see ADS1115_MUX_P1_NG
  322. * @see ADS1115_MUX_P2_NG
  323. * @see ADS1115_MUX_P3_NG
  324. * @see ADS1115_RA_CONFIG
  325. * @see ADS1115_CFG_MUX_BIT
  326. * @see ADS1115_CFG_MUX_LENGTH
  327. */
  328. void ADS1115::setMultiplexer(uint8_t mux) {
  329. if (I2Cdev::writeBitsW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_MUX_BIT, ADS1115_CFG_MUX_LENGTH, mux)) {
  330. muxMode = mux;
  331. if (devMode == ADS1115_MODE_CONTINUOUS) {
  332. // Force a stop/start
  333. setMode(ADS1115_MODE_SINGLESHOT);
  334. getConversion();
  335. setMode(ADS1115_MODE_CONTINUOUS);
  336. }
  337. }
  338. }
  339. /** Get programmable gain amplifier level.
  340. * @return Current programmable gain amplifier level
  341. * @see ADS1115_RA_CONFIG
  342. * @see ADS1115_CFG_PGA_BIT
  343. * @see ADS1115_CFG_PGA_LENGTH
  344. */
  345. uint8_t ADS1115::getGain() {
  346. I2Cdev::readBitsW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_PGA_BIT, ADS1115_CFG_PGA_LENGTH, buffer);
  347. pgaMode=(uint8_t)buffer[0];
  348. return pgaMode;
  349. }
  350. /** Set programmable gain amplifier level.
  351. * Continous mode may fill the conversion register
  352. * with data before the gain setting has taken effect. A stop/start of the conversion
  353. * is done to reset the values.
  354. * @param gain New programmable gain amplifier level
  355. * @see ADS1115_PGA_6P144
  356. * @see ADS1115_PGA_4P096
  357. * @see ADS1115_PGA_2P048
  358. * @see ADS1115_PGA_1P024
  359. * @see ADS1115_PGA_0P512
  360. * @see ADS1115_PGA_0P256
  361. * @see ADS1115_RA_CONFIG
  362. * @see ADS1115_CFG_PGA_BIT
  363. * @see ADS1115_CFG_PGA_LENGTH
  364. */
  365. void ADS1115::setGain(uint8_t gain) {
  366. if (I2Cdev::writeBitsW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_PGA_BIT, ADS1115_CFG_PGA_LENGTH, gain)) {
  367. pgaMode = gain;
  368. if (devMode == ADS1115_MODE_CONTINUOUS) {
  369. // Force a stop/start
  370. setMode(ADS1115_MODE_SINGLESHOT);
  371. getConversion();
  372. setMode(ADS1115_MODE_CONTINUOUS);
  373. }
  374. }
  375. }
  376. /** Get device mode.
  377. * @return Current device mode
  378. * @see ADS1115_MODE_CONTINUOUS
  379. * @see ADS1115_MODE_SINGLESHOT
  380. * @see ADS1115_RA_CONFIG
  381. * @see ADS1115_CFG_MODE_BIT
  382. */
  383. bool ADS1115::getMode() {
  384. I2Cdev::readBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_MODE_BIT, buffer);
  385. devMode = buffer[0];
  386. return devMode;
  387. }
  388. /** Set device mode.
  389. * @param mode New device mode
  390. * @see ADS1115_MODE_CONTINUOUS
  391. * @see ADS1115_MODE_SINGLESHOT
  392. * @see ADS1115_RA_CONFIG
  393. * @see ADS1115_CFG_MODE_BIT
  394. */
  395. void ADS1115::setMode(bool mode) {
  396. if (I2Cdev::writeBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_MODE_BIT, mode)) {
  397. devMode = mode;
  398. }
  399. }
  400. /** Get data rate.
  401. * @return Current data rate
  402. * @see ADS1115_RA_CONFIG
  403. * @see ADS1115_CFG_DR_BIT
  404. * @see ADS1115_CFG_DR_LENGTH
  405. */
  406. uint8_t ADS1115::getRate() {
  407. I2Cdev::readBitsW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_DR_BIT, ADS1115_CFG_DR_LENGTH, buffer);
  408. return (uint8_t)buffer[0];
  409. }
  410. /** Set data rate.
  411. * @param rate New data rate
  412. * @see ADS1115_RATE_8
  413. * @see ADS1115_RATE_16
  414. * @see ADS1115_RATE_32
  415. * @see ADS1115_RATE_64
  416. * @see ADS1115_RATE_128
  417. * @see ADS1115_RATE_250
  418. * @see ADS1115_RATE_475
  419. * @see ADS1115_RATE_860
  420. * @see ADS1115_RA_CONFIG
  421. * @see ADS1115_CFG_DR_BIT
  422. * @see ADS1115_CFG_DR_LENGTH
  423. */
  424. void ADS1115::setRate(uint8_t rate) {
  425. I2Cdev::writeBitsW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_DR_BIT, ADS1115_CFG_DR_LENGTH, rate);
  426. }
  427. /** Get comparator mode.
  428. * @return Current comparator mode
  429. * @see ADS1115_COMP_MODE_HYSTERESIS
  430. * @see ADS1115_COMP_MODE_WINDOW
  431. * @see ADS1115_RA_CONFIG
  432. * @see ADS1115_CFG_COMP_MODE_BIT
  433. */
  434. bool ADS1115::getComparatorMode() {
  435. I2Cdev::readBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_COMP_MODE_BIT, buffer);
  436. return buffer[0];
  437. }
  438. /** Set comparator mode.
  439. * @param mode New comparator mode
  440. * @see ADS1115_COMP_MODE_HYSTERESIS
  441. * @see ADS1115_COMP_MODE_WINDOW
  442. * @see ADS1115_RA_CONFIG
  443. * @see ADS1115_CFG_COMP_MODE_BIT
  444. */
  445. void ADS1115::setComparatorMode(bool mode) {
  446. I2Cdev::writeBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_COMP_MODE_BIT, mode);
  447. }
  448. /** Get comparator polarity setting.
  449. * @return Current comparator polarity setting
  450. * @see ADS1115_COMP_POL_ACTIVE_LOW
  451. * @see ADS1115_COMP_POL_ACTIVE_HIGH
  452. * @see ADS1115_RA_CONFIG
  453. * @see ADS1115_CFG_COMP_POL_BIT
  454. */
  455. bool ADS1115::getComparatorPolarity() {
  456. I2Cdev::readBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_COMP_POL_BIT, buffer);
  457. return buffer[0];
  458. }
  459. /** Set comparator polarity setting.
  460. * @param polarity New comparator polarity setting
  461. * @see ADS1115_COMP_POL_ACTIVE_LOW
  462. * @see ADS1115_COMP_POL_ACTIVE_HIGH
  463. * @see ADS1115_RA_CONFIG
  464. * @see ADS1115_CFG_COMP_POL_BIT
  465. */
  466. void ADS1115::setComparatorPolarity(bool polarity) {
  467. I2Cdev::writeBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_COMP_POL_BIT, polarity);
  468. }
  469. /** Get comparator latch enabled value.
  470. * @return Current comparator latch enabled value
  471. * @see ADS1115_COMP_LAT_NON_LATCHING
  472. * @see ADS1115_COMP_LAT_LATCHING
  473. * @see ADS1115_RA_CONFIG
  474. * @see ADS1115_CFG_COMP_LAT_BIT
  475. */
  476. bool ADS1115::getComparatorLatchEnabled() {
  477. I2Cdev::readBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_COMP_LAT_BIT, buffer);
  478. return buffer[0];
  479. }
  480. /** Set comparator latch enabled value.
  481. * @param enabled New comparator latch enabled value
  482. * @see ADS1115_COMP_LAT_NON_LATCHING
  483. * @see ADS1115_COMP_LAT_LATCHING
  484. * @see ADS1115_RA_CONFIG
  485. * @see ADS1115_CFG_COMP_LAT_BIT
  486. */
  487. void ADS1115::setComparatorLatchEnabled(bool enabled) {
  488. I2Cdev::writeBitW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_COMP_LAT_BIT, enabled);
  489. }
  490. /** Get comparator queue mode.
  491. * @return Current comparator queue mode
  492. * @see ADS1115_COMP_QUE_ASSERT1
  493. * @see ADS1115_COMP_QUE_ASSERT2
  494. * @see ADS1115_COMP_QUE_ASSERT4
  495. * @see ADS1115_COMP_QUE_DISABLE
  496. * @see ADS1115_RA_CONFIG
  497. * @see ADS1115_CFG_COMP_QUE_BIT
  498. * @see ADS1115_CFG_COMP_QUE_LENGTH
  499. */
  500. uint8_t ADS1115::getComparatorQueueMode() {
  501. I2Cdev::readBitsW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_COMP_QUE_BIT, ADS1115_CFG_COMP_QUE_LENGTH, buffer);
  502. return (uint8_t)buffer[0];
  503. }
  504. /** Set comparator queue mode.
  505. * @param mode New comparator queue mode
  506. * @see ADS1115_COMP_QUE_ASSERT1
  507. * @see ADS1115_COMP_QUE_ASSERT2
  508. * @see ADS1115_COMP_QUE_ASSERT4
  509. * @see ADS1115_COMP_QUE_DISABLE
  510. * @see ADS1115_RA_CONFIG
  511. * @see ADS1115_CFG_COMP_QUE_BIT
  512. * @see ADS1115_CFG_COMP_QUE_LENGTH
  513. */
  514. void ADS1115::setComparatorQueueMode(uint8_t mode) {
  515. I2Cdev::writeBitsW(devAddr, ADS1115_RA_CONFIG, ADS1115_CFG_COMP_QUE_BIT, ADS1115_CFG_COMP_QUE_LENGTH, mode);
  516. }
  517. // *_THRESH registers
  518. /** Get low threshold value.
  519. * @return Current low threshold value
  520. * @see ADS1115_RA_LO_THRESH
  521. */
  522. int16_t ADS1115::getLowThreshold() {
  523. I2Cdev::readWord(devAddr, ADS1115_RA_LO_THRESH, buffer);
  524. return buffer[0];
  525. }
  526. /** Set low threshold value.
  527. * @param threshold New low threshold value
  528. * @see ADS1115_RA_LO_THRESH
  529. */
  530. void ADS1115::setLowThreshold(int16_t threshold) {
  531. I2Cdev::writeWord(devAddr, ADS1115_RA_LO_THRESH, threshold);
  532. }
  533. /** Get high threshold value.
  534. * @return Current high threshold value
  535. * @see ADS1115_RA_HI_THRESH
  536. */
  537. int16_t ADS1115::getHighThreshold() {
  538. I2Cdev::readWord(devAddr, ADS1115_RA_HI_THRESH, buffer);
  539. return buffer[0];
  540. }
  541. /** Set high threshold value.
  542. * @param threshold New high threshold value
  543. * @see ADS1115_RA_HI_THRESH
  544. */
  545. void ADS1115::setHighThreshold(int16_t threshold) {
  546. I2Cdev::writeWord(devAddr, ADS1115_RA_HI_THRESH, threshold);
  547. }
  548. /** Configures ALERT/RDY pin as a conversion ready pin.
  549. * It does this by setting the MSB of the high threshold register to '1' and the MSB
  550. * of the low threshold register to '0'. COMP_POL and COMP_QUE bits will be set to '0'.
  551. * Note: ALERT/RDY pin requires a pull up resistor.
  552. */
  553. void ADS1115::setConversionReadyPinMode() {
  554. I2Cdev::writeBitW(devAddr, ADS1115_RA_HI_THRESH, 15, 1);
  555. I2Cdev::writeBitW(devAddr, ADS1115_RA_LO_THRESH, 15, 0);
  556. setComparatorPolarity(0);
  557. setComparatorQueueMode(0);
  558. }
  559. // Create a mask between two bits
  560. unsigned createMask(unsigned a, unsigned b) {
  561. unsigned mask = 0;
  562. for (unsigned i=a; i<=b; i++)
  563. mask |= 1 << i;
  564. return mask;
  565. }
  566. uint16_t shiftDown(uint16_t extractFrom, int places) {
  567. return (extractFrom >> places);
  568. }
  569. uint16_t getValueFromBits(uint16_t extractFrom, int high, int length) {
  570. int low= high-length +1;
  571. uint16_t mask = createMask(low ,high);
  572. return shiftDown(extractFrom & mask, low);
  573. }
  574. /** Show all the config register settings
  575. */
  576. void ADS1115::showConfigRegister() {
  577. I2Cdev::readWord(devAddr, ADS1115_RA_CONFIG, buffer);
  578. uint16_t configRegister =buffer[0];
  579. #ifdef ADS1115_SERIAL_DEBUG
  580. Serial.print("Register is:");
  581. Serial.println(configRegister,BIN);
  582. Serial.print("OS:\t");
  583. Serial.println(getValueFromBits(configRegister,
  584. ADS1115_CFG_OS_BIT,1), BIN);
  585. Serial.print("MUX:\t");
  586. Serial.println(getValueFromBits(configRegister,
  587. ADS1115_CFG_MUX_BIT,ADS1115_CFG_MUX_LENGTH), BIN);
  588. Serial.print("PGA:\t");
  589. Serial.println(getValueFromBits(configRegister,
  590. ADS1115_CFG_PGA_BIT,ADS1115_CFG_PGA_LENGTH), BIN);
  591. Serial.print("MODE:\t");
  592. Serial.println(getValueFromBits(configRegister,
  593. ADS1115_CFG_MODE_BIT,1), BIN);
  594. Serial.print("DR:\t");
  595. Serial.println(getValueFromBits(configRegister,
  596. ADS1115_CFG_DR_BIT,ADS1115_CFG_DR_LENGTH), BIN);
  597. Serial.print("CMP_MODE:\t");
  598. Serial.println(getValueFromBits(configRegister,
  599. ADS1115_CFG_COMP_MODE_BIT,1), BIN);
  600. Serial.print("CMP_POL:\t");
  601. Serial.println(getValueFromBits(configRegister,
  602. ADS1115_CFG_COMP_POL_BIT,1), BIN);
  603. Serial.print("CMP_LAT:\t");
  604. Serial.println(getValueFromBits(configRegister,
  605. ADS1115_CFG_COMP_LAT_BIT,1), BIN);
  606. Serial.print("CMP_QUE:\t");
  607. Serial.println(getValueFromBits(configRegister,
  608. ADS1115_CFG_COMP_QUE_BIT,ADS1115_CFG_COMP_QUE_LENGTH), BIN);
  609. #endif
  610. };