A library for ADS1015 and ADS1115 from Texas Instruments.
Fork of ADS1015 by
v1.3 - Added ADS1115_REG_CONFIG_DR & m_dataRate Jul 21. 2014 - Corrected m_conversionDelay - Now there is readADC() that returns counts and readADC_V() that returns voltage
Adafruit_ADS1015.h@1:a628fdaed351, 2014-05-21 (annotated)
- Committer:
- Sambo007
- Date:
- Wed May 21 13:12:24 2014 +0000
- Revision:
- 1:a628fdaed351
- Parent:
- 0:8174d9ceeca1
v1.2 - Fixed error in readADC_SingleEnded() sign bit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arve0 | 0:8174d9ceeca1 | 1 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 2 | /*! |
arve0 | 0:8174d9ceeca1 | 3 | @file Adafruit_ADS1015.h |
arve0 | 0:8174d9ceeca1 | 4 | @author K. Townsend (Adafruit Industries) |
arve0 | 0:8174d9ceeca1 | 5 | @license BSD (see LICENSE.txt) |
arve0 | 0:8174d9ceeca1 | 6 | |
arve0 | 0:8174d9ceeca1 | 7 | Ported to mbed by Arve Seljebu - arve0.github.io |
arve0 | 0:8174d9ceeca1 | 8 | |
arve0 | 0:8174d9ceeca1 | 9 | This is a library for the Adafruit ADS1015 breakout board |
arve0 | 0:8174d9ceeca1 | 10 | ----> https://www.adafruit.com/products/1083 |
arve0 | 0:8174d9ceeca1 | 11 | |
arve0 | 0:8174d9ceeca1 | 12 | Adafruit invests time and resources providing this open source code, |
arve0 | 0:8174d9ceeca1 | 13 | please support Adafruit and open-source hardware by purchasing |
arve0 | 0:8174d9ceeca1 | 14 | products from Adafruit! |
arve0 | 0:8174d9ceeca1 | 15 | |
arve0 | 0:8174d9ceeca1 | 16 | @section HISTORY |
arve0 | 0:8174d9ceeca1 | 17 | |
Sambo007 | 1:a628fdaed351 | 18 | v1.0 - First release |
Sambo007 | 1:a628fdaed351 | 19 | v1.1 - Added ADS1115 support - W. Earl |
Sambo007 | 1:a628fdaed351 | 20 | v1.1.1 - Ported to mbed - Arve Seljebu |
Sambo007 | 1:a628fdaed351 | 21 | v1.2 - Fixed error in readADC_SingleEnded() sign bit - Sam Berjawi |
arve0 | 0:8174d9ceeca1 | 22 | */ |
arve0 | 0:8174d9ceeca1 | 23 | /**************************************************************************/ |
arve0 | 0:8174d9ceeca1 | 24 | |
arve0 | 0:8174d9ceeca1 | 25 | #include <mbed.h> |
arve0 | 0:8174d9ceeca1 | 26 | |
arve0 | 0:8174d9ceeca1 | 27 | /*========================================================================= |
arve0 | 0:8174d9ceeca1 | 28 | I2C ADDRESS/BITS |
arve0 | 0:8174d9ceeca1 | 29 | -----------------------------------------------------------------------*/ |
arve0 | 0:8174d9ceeca1 | 30 | #define ADS1015_ADDRESS (0x48) // 1001 000 (ADDR = GND) |
arve0 | 0:8174d9ceeca1 | 31 | /*=========================================================================*/ |
arve0 | 0:8174d9ceeca1 | 32 | |
arve0 | 0:8174d9ceeca1 | 33 | /*========================================================================= |
arve0 | 0:8174d9ceeca1 | 34 | CONVERSION DELAY (in mS) |
arve0 | 0:8174d9ceeca1 | 35 | -----------------------------------------------------------------------*/ |
arve0 | 0:8174d9ceeca1 | 36 | #define ADS1015_CONVERSIONDELAY (1) |
arve0 | 0:8174d9ceeca1 | 37 | #define ADS1115_CONVERSIONDELAY (8) |
arve0 | 0:8174d9ceeca1 | 38 | /*=========================================================================*/ |
arve0 | 0:8174d9ceeca1 | 39 | |
arve0 | 0:8174d9ceeca1 | 40 | /*========================================================================= |
arve0 | 0:8174d9ceeca1 | 41 | POINTER REGISTER |
arve0 | 0:8174d9ceeca1 | 42 | -----------------------------------------------------------------------*/ |
arve0 | 0:8174d9ceeca1 | 43 | #define ADS1015_REG_POINTER_MASK (0x03) |
arve0 | 0:8174d9ceeca1 | 44 | #define ADS1015_REG_POINTER_CONVERT (0x00) |
arve0 | 0:8174d9ceeca1 | 45 | #define ADS1015_REG_POINTER_CONFIG (0x01) |
arve0 | 0:8174d9ceeca1 | 46 | #define ADS1015_REG_POINTER_LOWTHRESH (0x02) |
arve0 | 0:8174d9ceeca1 | 47 | #define ADS1015_REG_POINTER_HITHRESH (0x03) |
arve0 | 0:8174d9ceeca1 | 48 | /*=========================================================================*/ |
arve0 | 0:8174d9ceeca1 | 49 | |
arve0 | 0:8174d9ceeca1 | 50 | /*========================================================================= |
arve0 | 0:8174d9ceeca1 | 51 | CONFIG REGISTER |
arve0 | 0:8174d9ceeca1 | 52 | -----------------------------------------------------------------------*/ |
arve0 | 0:8174d9ceeca1 | 53 | #define ADS1015_REG_CONFIG_OS_MASK (0x8000) |
arve0 | 0:8174d9ceeca1 | 54 | #define ADS1015_REG_CONFIG_OS_SINGLE (0x8000) // Write: Set to start a single-conversion |
arve0 | 0:8174d9ceeca1 | 55 | #define ADS1015_REG_CONFIG_OS_BUSY (0x0000) // Read: Bit = 0 when conversion is in progress |
arve0 | 0:8174d9ceeca1 | 56 | #define ADS1015_REG_CONFIG_OS_NOTBUSY (0x8000) // Read: Bit = 1 when device is not performing a conversion |
arve0 | 0:8174d9ceeca1 | 57 | |
arve0 | 0:8174d9ceeca1 | 58 | #define ADS1015_REG_CONFIG_MUX_MASK (0x7000) |
arve0 | 0:8174d9ceeca1 | 59 | #define ADS1015_REG_CONFIG_MUX_DIFF_0_1 (0x0000) // Differential P = AIN0, N = AIN1 (default) |
arve0 | 0:8174d9ceeca1 | 60 | #define ADS1015_REG_CONFIG_MUX_DIFF_0_3 (0x1000) // Differential P = AIN0, N = AIN3 |
arve0 | 0:8174d9ceeca1 | 61 | #define ADS1015_REG_CONFIG_MUX_DIFF_1_3 (0x2000) // Differential P = AIN1, N = AIN3 |
arve0 | 0:8174d9ceeca1 | 62 | #define ADS1015_REG_CONFIG_MUX_DIFF_2_3 (0x3000) // Differential P = AIN2, N = AIN3 |
arve0 | 0:8174d9ceeca1 | 63 | #define ADS1015_REG_CONFIG_MUX_SINGLE_0 (0x4000) // Single-ended AIN0 |
arve0 | 0:8174d9ceeca1 | 64 | #define ADS1015_REG_CONFIG_MUX_SINGLE_1 (0x5000) // Single-ended AIN1 |
arve0 | 0:8174d9ceeca1 | 65 | #define ADS1015_REG_CONFIG_MUX_SINGLE_2 (0x6000) // Single-ended AIN2 |
arve0 | 0:8174d9ceeca1 | 66 | #define ADS1015_REG_CONFIG_MUX_SINGLE_3 (0x7000) // Single-ended AIN3 |
arve0 | 0:8174d9ceeca1 | 67 | |
arve0 | 0:8174d9ceeca1 | 68 | #define ADS1015_REG_CONFIG_PGA_MASK (0x0E00) |
arve0 | 0:8174d9ceeca1 | 69 | #define ADS1015_REG_CONFIG_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3 |
arve0 | 0:8174d9ceeca1 | 70 | #define ADS1015_REG_CONFIG_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1 |
arve0 | 0:8174d9ceeca1 | 71 | #define ADS1015_REG_CONFIG_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default) |
arve0 | 0:8174d9ceeca1 | 72 | #define ADS1015_REG_CONFIG_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4 |
arve0 | 0:8174d9ceeca1 | 73 | #define ADS1015_REG_CONFIG_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8 |
arve0 | 0:8174d9ceeca1 | 74 | #define ADS1015_REG_CONFIG_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16 |
arve0 | 0:8174d9ceeca1 | 75 | |
arve0 | 0:8174d9ceeca1 | 76 | #define ADS1015_REG_CONFIG_MODE_MASK (0x0100) |
arve0 | 0:8174d9ceeca1 | 77 | #define ADS1015_REG_CONFIG_MODE_CONTIN (0x0000) // Continuous conversion mode |
arve0 | 0:8174d9ceeca1 | 78 | #define ADS1015_REG_CONFIG_MODE_SINGLE (0x0100) // Power-down single-shot mode (default) |
arve0 | 0:8174d9ceeca1 | 79 | |
arve0 | 0:8174d9ceeca1 | 80 | #define ADS1015_REG_CONFIG_DR_MASK (0x00E0) |
arve0 | 0:8174d9ceeca1 | 81 | #define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second |
arve0 | 0:8174d9ceeca1 | 82 | #define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second |
arve0 | 0:8174d9ceeca1 | 83 | #define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second |
arve0 | 0:8174d9ceeca1 | 84 | #define ADS1015_REG_CONFIG_DR_920SPS (0x0060) // 920 samples per second |
arve0 | 0:8174d9ceeca1 | 85 | #define ADS1015_REG_CONFIG_DR_1600SPS (0x0080) // 1600 samples per second (default) |
arve0 | 0:8174d9ceeca1 | 86 | #define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) // 2400 samples per second |
arve0 | 0:8174d9ceeca1 | 87 | #define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) // 3300 samples per second |
arve0 | 0:8174d9ceeca1 | 88 | |
arve0 | 0:8174d9ceeca1 | 89 | #define ADS1015_REG_CONFIG_CMODE_MASK (0x0010) |
arve0 | 0:8174d9ceeca1 | 90 | #define ADS1015_REG_CONFIG_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default) |
arve0 | 0:8174d9ceeca1 | 91 | #define ADS1015_REG_CONFIG_CMODE_WINDOW (0x0010) // Window comparator |
arve0 | 0:8174d9ceeca1 | 92 | |
arve0 | 0:8174d9ceeca1 | 93 | #define ADS1015_REG_CONFIG_CPOL_MASK (0x0008) |
arve0 | 0:8174d9ceeca1 | 94 | #define ADS1015_REG_CONFIG_CPOL_ACTVLOW (0x0000) // ALERT/RDY pin is low when active (default) |
arve0 | 0:8174d9ceeca1 | 95 | #define ADS1015_REG_CONFIG_CPOL_ACTVHI (0x0008) // ALERT/RDY pin is high when active |
arve0 | 0:8174d9ceeca1 | 96 | |
arve0 | 0:8174d9ceeca1 | 97 | #define ADS1015_REG_CONFIG_CLAT_MASK (0x0004) // Determines if ALERT/RDY pin latches once asserted |
arve0 | 0:8174d9ceeca1 | 98 | #define ADS1015_REG_CONFIG_CLAT_NONLAT (0x0000) // Non-latching comparator (default) |
arve0 | 0:8174d9ceeca1 | 99 | #define ADS1015_REG_CONFIG_CLAT_LATCH (0x0004) // Latching comparator |
arve0 | 0:8174d9ceeca1 | 100 | |
arve0 | 0:8174d9ceeca1 | 101 | #define ADS1015_REG_CONFIG_CQUE_MASK (0x0003) |
arve0 | 0:8174d9ceeca1 | 102 | #define ADS1015_REG_CONFIG_CQUE_1CONV (0x0000) // Assert ALERT/RDY after one conversions |
arve0 | 0:8174d9ceeca1 | 103 | #define ADS1015_REG_CONFIG_CQUE_2CONV (0x0001) // Assert ALERT/RDY after two conversions |
arve0 | 0:8174d9ceeca1 | 104 | #define ADS1015_REG_CONFIG_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions |
arve0 | 0:8174d9ceeca1 | 105 | #define ADS1015_REG_CONFIG_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default) |
arve0 | 0:8174d9ceeca1 | 106 | /*=========================================================================*/ |
arve0 | 0:8174d9ceeca1 | 107 | |
arve0 | 0:8174d9ceeca1 | 108 | typedef enum |
arve0 | 0:8174d9ceeca1 | 109 | { |
arve0 | 0:8174d9ceeca1 | 110 | GAIN_TWOTHIRDS = ADS1015_REG_CONFIG_PGA_6_144V, |
arve0 | 0:8174d9ceeca1 | 111 | GAIN_ONE = ADS1015_REG_CONFIG_PGA_4_096V, |
arve0 | 0:8174d9ceeca1 | 112 | GAIN_TWO = ADS1015_REG_CONFIG_PGA_2_048V, |
arve0 | 0:8174d9ceeca1 | 113 | GAIN_FOUR = ADS1015_REG_CONFIG_PGA_1_024V, |
arve0 | 0:8174d9ceeca1 | 114 | GAIN_EIGHT = ADS1015_REG_CONFIG_PGA_0_512V, |
arve0 | 0:8174d9ceeca1 | 115 | GAIN_SIXTEEN = ADS1015_REG_CONFIG_PGA_0_256V |
arve0 | 0:8174d9ceeca1 | 116 | } adsGain_t; |
arve0 | 0:8174d9ceeca1 | 117 | |
arve0 | 0:8174d9ceeca1 | 118 | class Adafruit_ADS1015 |
arve0 | 0:8174d9ceeca1 | 119 | { |
arve0 | 0:8174d9ceeca1 | 120 | protected: |
arve0 | 0:8174d9ceeca1 | 121 | // Instance-specific properties |
arve0 | 0:8174d9ceeca1 | 122 | uint8_t m_i2cAddress; |
arve0 | 0:8174d9ceeca1 | 123 | uint8_t m_conversionDelay; |
arve0 | 0:8174d9ceeca1 | 124 | uint8_t m_bitShift; |
arve0 | 0:8174d9ceeca1 | 125 | adsGain_t m_gain; |
arve0 | 0:8174d9ceeca1 | 126 | I2C* m_i2c; |
arve0 | 0:8174d9ceeca1 | 127 | |
arve0 | 0:8174d9ceeca1 | 128 | |
arve0 | 0:8174d9ceeca1 | 129 | public: |
arve0 | 0:8174d9ceeca1 | 130 | Adafruit_ADS1015(I2C* i2c = 0, uint8_t i2cAddress = ADS1015_ADDRESS); // set i2c adress = 0 to allow ADS1115 to use this as default constructor |
arve0 | 0:8174d9ceeca1 | 131 | uint16_t readADC_SingleEnded(uint8_t channel); |
arve0 | 0:8174d9ceeca1 | 132 | int16_t readADC_Differential_0_1(void); |
arve0 | 0:8174d9ceeca1 | 133 | int16_t readADC_Differential_2_3(void); |
arve0 | 0:8174d9ceeca1 | 134 | void startComparator_SingleEnded(uint8_t channel, int16_t threshold); |
arve0 | 0:8174d9ceeca1 | 135 | int16_t getLastConversionResults(); |
arve0 | 0:8174d9ceeca1 | 136 | void setGain(adsGain_t gain); |
arve0 | 0:8174d9ceeca1 | 137 | adsGain_t getGain(void); |
arve0 | 0:8174d9ceeca1 | 138 | |
arve0 | 0:8174d9ceeca1 | 139 | private: |
arve0 | 0:8174d9ceeca1 | 140 | uint16_t readRegister(uint8_t i2cAddress, uint8_t reg); |
arve0 | 0:8174d9ceeca1 | 141 | void writeRegister(uint8_t i2cAddress, uint8_t reg, uint16_t value); |
arve0 | 0:8174d9ceeca1 | 142 | }; |
arve0 | 0:8174d9ceeca1 | 143 | |
arve0 | 0:8174d9ceeca1 | 144 | // Derive from ADS1105 & override construction to set properties |
arve0 | 0:8174d9ceeca1 | 145 | class Adafruit_ADS1115 : public Adafruit_ADS1015 |
arve0 | 0:8174d9ceeca1 | 146 | { |
arve0 | 0:8174d9ceeca1 | 147 | public: |
arve0 | 0:8174d9ceeca1 | 148 | Adafruit_ADS1115(I2C* i2c, uint8_t i2cAddress = ADS1015_ADDRESS); |
arve0 | 0:8174d9ceeca1 | 149 | |
arve0 | 0:8174d9ceeca1 | 150 | private: |
arve0 | 0:8174d9ceeca1 | 151 | }; |