JT

Committer:
jortronm2
Date:
Thu Jan 04 03:39:28 2018 +0000
Revision:
0:4096be81b17f
No Changes

Who changed what in which revision?

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