ADS1100.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**************************************************************************/
  2. /*
  3. Distributed with a free-will license.
  4. Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
  5. ADS1100
  6. This code is designed to work with the ADS1100_I2CADC I2C Mini Module available from ControlEverything.com.
  7. https://www.controleverything.com/content/Analog-Digital-Converters?sku=ADS1100_I2CADC#tabs-0-product_tabset-2
  8. */
  9. /**************************************************************************/
  10. #if ARDUINO >= 100
  11. #include "Arduino.h"
  12. #else
  13. #include "WProgram.h"
  14. #endif
  15. #include <Wire.h>
  16. #include "ADS1100.h"
  17. /**************************************************************************/
  18. /*
  19. Abstract away platform differences in Arduino wire library
  20. */
  21. /**************************************************************************/
  22. static uint8_t i2cread(void)
  23. {
  24. #if ARDUINO >= 100
  25. return Wire.read();
  26. #else
  27. return Wire.receive();
  28. #endif
  29. }
  30. /**************************************************************************/
  31. /*
  32. Abstract away platform differences in Arduino wire library
  33. */
  34. /**************************************************************************/
  35. static void i2cwrite(uint8_t x)
  36. {
  37. #if ARDUINO >= 100
  38. Wire.write((uint8_t)x);
  39. #else
  40. Wire.send(x);
  41. #endif
  42. }
  43. /**************************************************************************/
  44. /*
  45. Writes 8-bits to the destination register
  46. */
  47. /**************************************************************************/
  48. static void writeRegister(uint8_t i2cAddress, uint8_t value)
  49. {
  50. Wire.beginTransmission(i2cAddress);
  51. i2cwrite((uint8_t)value);
  52. Wire.endTransmission();
  53. }
  54. /**************************************************************************/
  55. /*
  56. Reads 16-bits from the destination register
  57. */
  58. /**************************************************************************/
  59. static uint16_t readRegister(uint8_t i2cAddress)
  60. {
  61. Wire.beginTransmission(i2cAddress);
  62. Wire.endTransmission();
  63. Wire.requestFrom(i2cAddress, (uint8_t)2);
  64. return (int16_t)((i2cread() << 8) | i2cread());
  65. }
  66. /**************************************************************************/
  67. /*
  68. Instantiates a new ADS1100 class with appropriate properties
  69. */
  70. /**************************************************************************/
  71. void ADS1100::getAddr_ADS1100(uint8_t i2cAddress)
  72. {
  73. ads_i2cAddress = i2cAddress;
  74. ads_conversionDelay = ADS1100_CONVERSIONDELAY;
  75. }
  76. /**************************************************************************/
  77. /*
  78. Sets up the Hardware
  79. */
  80. /**************************************************************************/
  81. void ADS1100::begin()
  82. {
  83. Wire.begin();
  84. }
  85. /**************************************************************************/
  86. /*
  87. Sets the Operational status/single-shot conversion start
  88. This determines the operational status of the device
  89. */
  90. /**************************************************************************/
  91. void ADS1100::setOSMode(adsOSMode_t osmode)
  92. {
  93. ads_osmode = osmode;
  94. }
  95. /**************************************************************************/
  96. /*
  97. Gets the Operational status/single-shot conversion start
  98. */
  99. /**************************************************************************/
  100. adsOSMode_t ADS1100::getOSMode()
  101. {
  102. return ads_osmode;
  103. }
  104. /**************************************************************************/
  105. /*
  106. Sets the Device operating mode
  107. This controls the current operational mode of the ADS1100
  108. */
  109. /**************************************************************************/
  110. void ADS1100::setMode(adsMode_t mode)
  111. {
  112. ads_mode = mode;
  113. }
  114. /**************************************************************************/
  115. /*
  116. Gets the Device operating mode
  117. */
  118. /**************************************************************************/
  119. adsMode_t ADS1100::getMode()
  120. {
  121. return ads_mode;
  122. }
  123. /**************************************************************************/
  124. /*
  125. Sets the Date Rate
  126. This controls the data rate setting
  127. */
  128. /**************************************************************************/
  129. void ADS1100::setRate(adsRate_t rate)
  130. {
  131. ads_rate = rate;
  132. }
  133. /**************************************************************************/
  134. /*
  135. Gets the Date Rate
  136. */
  137. /**************************************************************************/
  138. adsRate_t ADS1100::getRate()
  139. {
  140. return ads_rate;
  141. }
  142. /**************************************************************************/
  143. /*
  144. Sets the gain and input voltage range
  145. This configures the programmable gain amplifier
  146. */
  147. /**************************************************************************/
  148. void ADS1100::setGain(adsGain_t gain)
  149. {
  150. ads_gain = gain;
  151. }
  152. /**************************************************************************/
  153. /*
  154. Gets a gain and input voltage range
  155. */
  156. /**************************************************************************/
  157. adsGain_t ADS1100::getGain()
  158. {
  159. return ads_gain;
  160. }
  161. /**************************************************************************/
  162. /*
  163. Reads the conversion results, measuring the voltage
  164. difference between the P and N input
  165. Generates a signed value since the difference can be either
  166. positive or negative
  167. */
  168. /**************************************************************************/
  169. int16_t ADS1100::Measure_Differential()
  170. {
  171. // Start with default values
  172. uint16_t config;
  173. // Set Operational status/single-shot conversion start
  174. config |= ads_osmode;
  175. // Set Device operating mode
  176. config |= ads_mode;
  177. // Set Data rate
  178. config |= ads_rate;
  179. // Set PGA/voltage range
  180. config |= ads_gain;
  181. // Write config register to the ADC
  182. writeRegister(ads_i2cAddress, config);
  183. // Wait for the conversion to complete
  184. delay(ads_conversionDelay);
  185. // Read the conversion results
  186. uint16_t raw_adc = readRegister(ads_i2cAddress);
  187. return (int16_t)raw_adc;
  188. }