test

Committer:
etiene32
Date:
Wed Mar 06 14:01:46 2019 +0000
Revision:
0:cf2317282e61
a

Who changed what in which revision?

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