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