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