A library for ADS1015 and ADS1115 from Texas Instruments.
Fork of ADS1015 by
Adafruit_ADS1015.h@0:8174d9ceeca1, 2013-11-10 (annotated)
- Committer:
- arve0
- Date:
- Sun Nov 10 18:22:27 2013 +0000
- Revision:
- 0:8174d9ceeca1
- Child:
- 2:d864e21d4e58
- Child:
- 3:fb735fb343de
Ported from Adafruit's arduino library
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 | 0:8174d9ceeca1 | 80 | #define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second |
arve0 | 0:8174d9ceeca1 | 81 | #define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second |
arve0 | 0:8174d9ceeca1 | 82 | #define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second |
arve0 | 0:8174d9ceeca1 | 83 | #define ADS1015_REG_CONFIG_DR_920SPS (0x0060) // 920 samples per second |
arve0 | 0:8174d9ceeca1 | 84 | #define ADS1015_REG_CONFIG_DR_1600SPS (0x0080) // 1600 samples per second (default) |
arve0 | 0:8174d9ceeca1 | 85 | #define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) // 2400 samples per second |
arve0 | 0:8174d9ceeca1 | 86 | #define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) // 3300 samples per second |
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 | 0:8174d9ceeca1 | 107 | typedef enum |
arve0 | 0:8174d9ceeca1 | 108 | { |
arve0 | 0:8174d9ceeca1 | 109 | GAIN_TWOTHIRDS = ADS1015_REG_CONFIG_PGA_6_144V, |
arve0 | 0:8174d9ceeca1 | 110 | GAIN_ONE = ADS1015_REG_CONFIG_PGA_4_096V, |
arve0 | 0:8174d9ceeca1 | 111 | GAIN_TWO = ADS1015_REG_CONFIG_PGA_2_048V, |
arve0 | 0:8174d9ceeca1 | 112 | GAIN_FOUR = ADS1015_REG_CONFIG_PGA_1_024V, |
arve0 | 0:8174d9ceeca1 | 113 | GAIN_EIGHT = ADS1015_REG_CONFIG_PGA_0_512V, |
arve0 | 0:8174d9ceeca1 | 114 | GAIN_SIXTEEN = ADS1015_REG_CONFIG_PGA_0_256V |
arve0 | 0:8174d9ceeca1 | 115 | } adsGain_t; |
arve0 | 0:8174d9ceeca1 | 116 | |
arve0 | 0:8174d9ceeca1 | 117 | class Adafruit_ADS1015 |
arve0 | 0:8174d9ceeca1 | 118 | { |
arve0 | 0:8174d9ceeca1 | 119 | protected: |
arve0 | 0:8174d9ceeca1 | 120 | // Instance-specific properties |
arve0 | 0:8174d9ceeca1 | 121 | uint8_t m_i2cAddress; |
arve0 | 0:8174d9ceeca1 | 122 | uint8_t m_conversionDelay; |
arve0 | 0:8174d9ceeca1 | 123 | uint8_t m_bitShift; |
arve0 | 0:8174d9ceeca1 | 124 | adsGain_t m_gain; |
arve0 | 0:8174d9ceeca1 | 125 | I2C* m_i2c; |
arve0 | 0:8174d9ceeca1 | 126 | |
arve0 | 0:8174d9ceeca1 | 127 | |
arve0 | 0:8174d9ceeca1 | 128 | public: |
arve0 | 0:8174d9ceeca1 | 129 | 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 | 130 | uint16_t readADC_SingleEnded(uint8_t channel); |
arve0 | 0:8174d9ceeca1 | 131 | int16_t readADC_Differential_0_1(void); |
arve0 | 0:8174d9ceeca1 | 132 | int16_t readADC_Differential_2_3(void); |
arve0 | 0:8174d9ceeca1 | 133 | void startComparator_SingleEnded(uint8_t channel, int16_t threshold); |
arve0 | 0:8174d9ceeca1 | 134 | int16_t getLastConversionResults(); |
arve0 | 0:8174d9ceeca1 | 135 | void setGain(adsGain_t gain); |
arve0 | 0:8174d9ceeca1 | 136 | adsGain_t getGain(void); |
arve0 | 0:8174d9ceeca1 | 137 | |
arve0 | 0:8174d9ceeca1 | 138 | private: |
arve0 | 0:8174d9ceeca1 | 139 | uint16_t readRegister(uint8_t i2cAddress, uint8_t reg); |
arve0 | 0:8174d9ceeca1 | 140 | void writeRegister(uint8_t i2cAddress, uint8_t reg, uint16_t value); |
arve0 | 0:8174d9ceeca1 | 141 | }; |
arve0 | 0:8174d9ceeca1 | 142 | |
arve0 | 0:8174d9ceeca1 | 143 | // Derive from ADS1105 & override construction to set properties |
arve0 | 0:8174d9ceeca1 | 144 | class Adafruit_ADS1115 : public Adafruit_ADS1015 |
arve0 | 0:8174d9ceeca1 | 145 | { |
arve0 | 0:8174d9ceeca1 | 146 | public: |
arve0 | 0:8174d9ceeca1 | 147 | Adafruit_ADS1115(I2C* i2c, uint8_t i2cAddress = ADS1015_ADDRESS); |
arve0 | 0:8174d9ceeca1 | 148 | |
arve0 | 0:8174d9ceeca1 | 149 | private: |
arve0 | 0:8174d9ceeca1 | 150 | }; |