Adafruit_ADS1015.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /**************************************************************************/
  2. /*!
  3. @file Adafruit_ADS1015.cpp
  4. @author K.Townsend (Adafruit Industries)
  5. @license BSD (see license.txt)
  6. Driver for the ADS1015/ADS1115 ADC
  7. This is a library for the Adafruit MPL115A2 breakout
  8. ----> https://www.adafruit.com/products/???
  9. Adafruit invests time and resources providing this open source code,
  10. please support Adafruit and open-source hardware by purchasing
  11. products from Adafruit!
  12. @section HISTORY
  13. v1.0 - First release
  14. */
  15. /**************************************************************************/
  16. #if ARDUINO >= 100
  17. #include "Arduino.h"
  18. #else
  19. #include "WProgram.h"
  20. #endif
  21. #include <Wire.h>
  22. #include "Adafruit_ADS1015.h"
  23. /**************************************************************************/
  24. /*!
  25. @brief Abstract away platform differences in Arduino wire library
  26. */
  27. /**************************************************************************/
  28. static uint8_t i2cread(void) {
  29. #if ARDUINO >= 100
  30. return Wire.read();
  31. #else
  32. return Wire.receive();
  33. #endif
  34. }
  35. /**************************************************************************/
  36. /*!
  37. @brief Abstract away platform differences in Arduino wire library
  38. */
  39. /**************************************************************************/
  40. static void i2cwrite(uint8_t x) {
  41. #if ARDUINO >= 100
  42. Wire.write((uint8_t)x);
  43. #else
  44. Wire.send(x);
  45. #endif
  46. }
  47. /**************************************************************************/
  48. /*!
  49. @brief Writes 16-bits to the specified destination register
  50. */
  51. /**************************************************************************/
  52. static void writeRegister(uint8_t i2cAddress, uint8_t reg, uint16_t value) {
  53. Wire.beginTransmission(i2cAddress);
  54. i2cwrite((uint8_t)reg);
  55. i2cwrite((uint8_t)(value>>8));
  56. i2cwrite((uint8_t)(value & 0xFF));
  57. Wire.endTransmission();
  58. }
  59. /**************************************************************************/
  60. /*!
  61. @brief Writes 16-bits to the specified destination register
  62. */
  63. /**************************************************************************/
  64. static uint16_t readRegister(uint8_t i2cAddress, uint8_t reg) {
  65. Wire.beginTransmission(i2cAddress);
  66. i2cwrite(ADS1015_REG_POINTER_CONVERT);
  67. Wire.endTransmission();
  68. Wire.requestFrom(i2cAddress, (uint8_t)2);
  69. return ((i2cread() << 8) | i2cread());
  70. }
  71. /**************************************************************************/
  72. /*!
  73. @brief Instantiates a new ADS1015 class w/appropriate properties
  74. */
  75. /**************************************************************************/
  76. Adafruit_ADS1015::Adafruit_ADS1015(uint8_t i2cAddress)
  77. {
  78. m_i2cAddress = i2cAddress;
  79. m_conversionDelay = ADS1015_CONVERSIONDELAY;
  80. m_bitShift = 4;
  81. m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
  82. }
  83. /**************************************************************************/
  84. /*!
  85. @brief Instantiates a new ADS1115 class w/appropriate properties
  86. */
  87. /**************************************************************************/
  88. Adafruit_ADS1115::Adafruit_ADS1115(uint8_t i2cAddress)
  89. {
  90. m_i2cAddress = i2cAddress;
  91. m_conversionDelay = ADS1115_CONVERSIONDELAY;
  92. m_bitShift = 0;
  93. m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
  94. }
  95. /**************************************************************************/
  96. /*!
  97. @brief Sets up the HW (reads coefficients values, etc.)
  98. */
  99. /**************************************************************************/
  100. void Adafruit_ADS1015::begin() {
  101. Wire.begin();
  102. }
  103. /**************************************************************************/
  104. /*!
  105. @brief Sets the gain and input voltage range
  106. */
  107. /**************************************************************************/
  108. void Adafruit_ADS1015::setGain(adsGain_t gain)
  109. {
  110. m_gain = gain;
  111. }
  112. /**************************************************************************/
  113. /*!
  114. @brief Gets a gain and input voltage range
  115. */
  116. /**************************************************************************/
  117. adsGain_t Adafruit_ADS1015::getGain()
  118. {
  119. return m_gain;
  120. }
  121. /**************************************************************************/
  122. /*!
  123. @brief Gets a single-ended ADC reading from the specified channel
  124. */
  125. /**************************************************************************/
  126. uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) {
  127. if (channel > 3)
  128. {
  129. return 0;
  130. }
  131. // Start with default values
  132. uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
  133. ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
  134. ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
  135. ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
  136. ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
  137. ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
  138. // Set PGA/voltage range
  139. config |= m_gain;
  140. // Set single-ended input channel
  141. switch (channel)
  142. {
  143. case (0):
  144. config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
  145. break;
  146. case (1):
  147. config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
  148. break;
  149. case (2):
  150. config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
  151. break;
  152. case (3):
  153. config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
  154. break;
  155. }
  156. // Set 'start single-conversion' bit
  157. config |= ADS1015_REG_CONFIG_OS_SINGLE;
  158. // Write config register to the ADC
  159. writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
  160. // Wait for the conversion to complete
  161. //delay(m_conversionDelay);
  162. // Read the conversion results
  163. // Shift 12-bit results right 4 bits for the ADS1015
  164. return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
  165. }
  166. /**************************************************************************/
  167. /*!
  168. @brief Reads the conversion results, measuring the voltage
  169. difference between the P (AIN0) and N (AIN1) input. Generates
  170. a signed value since the difference can be either
  171. positive or negative.
  172. */
  173. /**************************************************************************/
  174. int16_t Adafruit_ADS1015::readADC_Differential_0_1() {
  175. // Start with default values
  176. uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
  177. ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
  178. ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
  179. ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
  180. ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
  181. ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
  182. // Set PGA/voltage range
  183. config |= m_gain;
  184. // Set channels
  185. config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N
  186. // Set 'start single-conversion' bit
  187. config |= ADS1015_REG_CONFIG_OS_SINGLE;
  188. // Write config register to the ADC
  189. writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
  190. // Wait for the conversion to complete
  191. delay(m_conversionDelay);
  192. // Read the conversion results
  193. uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
  194. if (m_bitShift == 0)
  195. {
  196. return (int16_t)res;
  197. }
  198. else
  199. {
  200. // Shift 12-bit results right 4 bits for the ADS1015,
  201. // making sure we keep the sign bit intact
  202. if (res > 0x07FF)
  203. {
  204. // negative number - extend the sign to 16th bit
  205. res |= 0xF000;
  206. }
  207. return (int16_t)res;
  208. }
  209. }
  210. /**************************************************************************/
  211. /*!
  212. @brief Reads the conversion results, measuring the voltage
  213. difference between the P (AIN2) and N (AIN3) input. Generates
  214. a signed value since the difference can be either
  215. positive or negative.
  216. */
  217. /**************************************************************************/
  218. int16_t Adafruit_ADS1015::readADC_Differential_2_3() {
  219. // Start with default values
  220. uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
  221. ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
  222. ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
  223. ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
  224. ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
  225. ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
  226. // Set PGA/voltage range
  227. config |= m_gain;
  228. // Set channels
  229. config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N
  230. // Set 'start single-conversion' bit
  231. config |= ADS1015_REG_CONFIG_OS_SINGLE;
  232. // Write config register to the ADC
  233. writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
  234. // Wait for the conversion to complete
  235. delay(m_conversionDelay);
  236. // Read the conversion results
  237. uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
  238. if (m_bitShift == 0)
  239. {
  240. return (int16_t)res;
  241. }
  242. else
  243. {
  244. // Shift 12-bit results right 4 bits for the ADS1015,
  245. // making sure we keep the sign bit intact
  246. if (res > 0x07FF)
  247. {
  248. // negative number - extend the sign to 16th bit
  249. res |= 0xF000;
  250. }
  251. return (int16_t)res;
  252. }
  253. }
  254. /**************************************************************************/
  255. /*!
  256. @brief Sets up the comparator to operate in basic mode, causing the
  257. ALERT/RDY pin to assert (go from high to low) when the ADC
  258. value exceeds the specified threshold.
  259. This will also set the ADC in continuous conversion mode.
  260. */
  261. /**************************************************************************/
  262. void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold)
  263. {
  264. // Start with default values
  265. uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match
  266. ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode
  267. ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
  268. ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
  269. ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
  270. ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode
  271. ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode
  272. // Set PGA/voltage range
  273. config |= m_gain;
  274. // Set single-ended input channel
  275. switch (channel)
  276. {
  277. case (0):
  278. config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
  279. break;
  280. case (1):
  281. config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
  282. break;
  283. case (2):
  284. config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
  285. break;
  286. case (3):
  287. config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
  288. break;
  289. }
  290. // Set the high threshold register
  291. // Shift 12-bit results left 4 bits for the ADS1015
  292. writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift);
  293. // Write config register to the ADC
  294. writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
  295. }
  296. /**************************************************************************/
  297. /*!
  298. @brief In order to clear the comparator, we need to read the
  299. conversion results. This function reads the last conversion
  300. results without changing the config value.
  301. */
  302. /**************************************************************************/
  303. int16_t Adafruit_ADS1015::getLastConversionResults()
  304. {
  305. // Wait for the conversion to complete
  306. delay(m_conversionDelay);
  307. // Read the conversion results
  308. uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
  309. if (m_bitShift == 0)
  310. {
  311. return (int16_t)res;
  312. }
  313. else
  314. {
  315. // Shift 12-bit results right 4 bits for the ADS1015,
  316. // making sure we keep the sign bit intact
  317. if (res > 0x07FF)
  318. {
  319. // negative number - extend the sign to 16th bit
  320. res |= 0xF000;
  321. }
  322. return (int16_t)res;
  323. }
  324. }