Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ADS111X.h
00001 /** 00002 * @brief ADS111X.h 00003 * @details Ultra-Small, Low-Power, I2C-Compatible, 860-SPS, 16-Bit ADCs With Internal Reference, Oscillator, and Programmable Comparator. 00004 * Header file. 00005 * 00006 * 00007 * @return N/A 00008 * 00009 * @author Manuel Caballero 00010 * @date 18/June/2020 00011 * @version 18/June/2020 The ORIGIN 00012 * @pre N/A. 00013 * @warning N/A 00014 * @pre This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ). 00015 */ 00016 #ifndef ADS111X_H 00017 #define ADS111X_H 00018 00019 #include "mbed.h" 00020 00021 00022 /** 00023 Example: 00024 @code 00025 #include "mbed.h" 00026 #include "ADS111X.h" 00027 00028 ADS111X myADS111X ( I2C_SDA, I2C_SCL, ADS111X::ADS111X_ADDRESS_GND, 100000, ADS111X::DEVICE_ADS1115 ); // I2C_SDA | I2C_SCL | DEVICE_ADS1115 00029 Serial pc ( USBTX, USBRX ); // tx, rx 00030 00031 DigitalOut myled ( LED1 ); 00032 Ticker newAction; 00033 00034 00035 //@brief Constants. 00036 00037 00038 //@brief Variables. 00039 volatile uint32_t myState; // State that indicates when to perform a new sample 00040 00041 00042 //@brief FUNCTION PROTOTYPES 00043 void changeDATA ( void ); 00044 00045 00046 //@brief FUNCTION FOR APPLICATION MAIN ENTRY. 00047 int main() 00048 { 00049 ADS111X::ADS111X_status_t aux; 00050 ADS111X::ADS111X_data_t myADS111X_Data; 00051 00052 pc.baud ( 115200 ); 00053 00054 myled = 1; 00055 wait(3); 00056 myled = 0; 00057 00058 // Perform a softreset 00059 aux = myADS111X.ADS111X_SoftReset (); 00060 wait_ms ( 500U ); 00061 00062 // Input multiplexor configuration ( channels ): AINp = AIN0 | AINn = GND 00063 myADS111X_Data.config.mux = ADS111X::CONFIG_MUX_AINP_AIN0_AND_AINN_GND; 00064 aux = myADS111X.ADS111X_SetMux ( myADS111X_Data ); 00065 00066 // Gain: ±4.096V 00067 myADS111X_Data.config.pga = ADS111X::CONFIG_PGA_FSR_4_096_V; 00068 aux = myADS111X.ADS111X_SetGain ( myADS111X_Data ); 00069 00070 // Mode: Single-shot 00071 myADS111X_Data.config.mode = ADS111X::CONFIG_MODE_SINGLE_SHOT; 00072 aux = myADS111X.ADS111X_SetMode ( myADS111X_Data.config ); 00073 00074 // Data rate: 1600 SPS 00075 myADS111X_Data.config.dr = ADS111X::CONFIG_DR_128_SPS; 00076 aux = myADS111X.ADS111X_SetDataRate ( myADS111X_Data.config ); 00077 00078 // Comparator: Disabled 00079 myADS111X_Data.config.comp_que = ADS111X::CONFIG_COMP_QUE_DISABLED; 00080 aux = myADS111X.ADS111X_SetComparator ( myADS111X_Data ); 00081 00082 myState = 0UL; // Reset the variable 00083 newAction.attach( &changeDATA, 1U ); // the address of the function to be attached ( changeDATA ) and the interval ( 1s ) 00084 00085 // Let the callbacks take care of everything 00086 while(1) { 00087 sleep(); 00088 00089 if ( myState == 1UL ) { 00090 myled = 1U; 00091 00092 // Trigger a new conversion 00093 aux = myADS111X.ADS111X_StartSingleConversion (); 00094 00095 // Wait until the conversion is completed 00096 do { 00097 aux = myADS111X.ADS111X_GetOS ( &myADS111X_Data.config ); 00098 } while( ( myADS111X_Data.config.os & ADS111X::CONFIG_OS_MASK ) == ADS111X::CONFIG_OS_BUSY ); // [TODO] Too dangerous! the uC may get stuck here 00099 // [WORKAROUND] Insert a counter. 00100 // Get the result 00101 aux = myADS111X.ADS111X_GetConversion ( &myADS111X_Data ); 00102 00103 // Send data through the UART 00104 pc.printf ( "V: %d mV\r\n", (int32_t)( 1000 * myADS111X_Data.conversion.conversion ) ); 00105 00106 00107 // Reset the variables 00108 myState = 0UL; 00109 myled = 0U; 00110 } 00111 } 00112 } 00113 00114 00115 // @brief changeDATA ( void ) 00116 // 00117 // @details It changes myState variable 00118 // 00119 // @param[in] N/A 00120 // 00121 // @param[out] N/A. 00122 // 00123 // @return N/A. 00124 // 00125 // @author Manuel Caballero 00126 // @date 18/June/2020 00127 // @version 18/June/2020 The ORIGIN 00128 // @pre N/A 00129 // @warning N/A. 00130 void changeDATA ( void ) 00131 { 00132 myState = 1UL; 00133 } 00134 @endcode 00135 */ 00136 00137 00138 /*! 00139 Library for the ADS111X Ultra-Small, Low-Power, I2C-Compatible, 860-SPS, 16-Bit ADCs With Internal Reference, Oscillator, and Programmable Comparator. 00140 */ 00141 class ADS111X 00142 { 00143 public: 00144 /** 00145 * @brief DEFAULT ADDRESSES 00146 */ 00147 typedef enum { 00148 ADS111X_ADDRESS_GND = ( 0b1001000 << 1U ), /*!< I2C slave address byte, ADDR = GND */ 00149 ADS111X_ADDRESS_VDD = ( 0b1001001 << 1U ), /*!< I2C slave address byte, ADDR = VDD */ 00150 ADS111X_ADDRESS_SDA = ( 0b1001010 << 1U ), /*!< I2C slave address byte, ADDR = SDA */ 00151 ADS111X_ADDRESS_SCL = ( 0b1001011 << 1U ) /*!< I2C slave address byte, ADDR = SCL */ 00152 } ADS111X_addresses_t; 00153 00154 00155 00156 /** 00157 * @brief REGISTER MAP 00158 */ 00159 typedef enum { 00160 ADS111X_CONVERSION = 0x00, /*!< Conversion register */ 00161 ADS111X_CONFIG = 0x01, /*!< Config register */ 00162 ADS111X_LO_THRESH = 0x02, /*!< Lo threshold register */ 00163 ADS111X_HI_THRESH = 0x03, /*!< Hi threshold register */ 00164 ADS111X_RESET_COMMAND = 0x06 /*!< Reset command ( with a general call ) */ 00165 } ADS111X_register_map_t; 00166 00167 00168 00169 /** 00170 * @brief DEVICE. 00171 * NOTE: The user MUST define which device to use: ADS1013, ADS1014 or ADS1015. 00172 */ 00173 typedef enum { 00174 DEVICE_ADS1113 = 0x00, /*!< Device: ADS1113 */ 00175 DEVICE_ADS1114 = 0x01, /*!< Device: ADS1114 */ 00176 DEVICE_ADS1115 = 0x02 /*!< Device: ADS1115 */ 00177 } ADS111X_device_t; 00178 00179 00180 00181 /** 00182 * @brief CONFIG REGISTER. ( Default: 0x8583 ) 00183 * NOTE: The 16-bit Config register is used to control the operating mode, input selection, data rate, full-scale range, and 00184 * comparator modes. 00185 */ 00186 /* OS <15> 00187 * NOTE: Operational status or single-shot conversion start. 00188 */ 00189 typedef enum { 00190 CONFIG_OS_MASK = ( 1U << 15U ), /*!< OS mask */ 00191 CONFIG_OS_BUSY = ( 1U << 15U ), /*!< Device is not currently performing a conversion/Start a single conversion (when in power-down state) [Default] */ 00192 CONFIG_OS_NOT_BUSY = ( 0U << 15U ) /*!< Device is currently performing a conversion */ 00193 } ADS111X_config_os_t; 00194 00195 00196 /* MUX <14:12> 00197 * NOTE: Input multiplexer configuration ( ADS1115 only ). 00198 */ 00199 typedef enum { 00200 CONFIG_MUX_MASK = ( 0b111 << 12U ), /*!< MUX mask */ 00201 CONFIG_MUX_AINP_AIN0_AND_AINN_AIN1 = ( 0b000 << 12U ), /*!< AINP = AIN0 and AINN = AIN1 [ Default ] */ 00202 CONFIG_MUX_AINP_AIN0_AND_AINN_AIN3 = ( 0b001 << 12U ), /*!< AINP = AIN0 and AINN = AIN3 */ 00203 CONFIG_MUX_AINP_AIN1_AND_AINN_AIN3 = ( 0b010 << 12U ), /*!< AINP = AIN1 and AINN = AIN3 */ 00204 CONFIG_MUX_AINP_AIN2_AND_AINN_AIN3 = ( 0b011 << 12U ), /*!< AINP = AIN2 and AINN = AIN3 */ 00205 CONFIG_MUX_AINP_AIN0_AND_AINN_GND = ( 0b100 << 12U ), /*!< AINP = AIN0 and AINN = GND */ 00206 CONFIG_MUX_AINP_AIN1_AND_AINN_GND = ( 0b101 << 12U ), /*!< AINP = AIN1 and AINN = GND */ 00207 CONFIG_MUX_AINP_AIN2_AND_AINN_GND = ( 0b110 << 12U ), /*!< AINP = AIN2 and AINN = GND */ 00208 CONFIG_MUX_AINP_AIN3_AND_AINN_GND = ( 0b111 << 12U ) /*!< AINP = AIN3 and AINN = GND */ 00209 } ADS111X_config_mux_t ; 00210 00211 00212 /* PGA <11:9> 00213 * NOTE: Programmable gain amplifier configuration ( These bits serve NO function on the ADS1113 ). 00214 */ 00215 typedef enum { 00216 CONFIG_PGA_MASK = ( 0b111 << 9U ), /*!< PGA mask */ 00217 CONFIG_PGA_FSR_6_144_V = ( 0b000 << 9U ), /*!< FSR = ±6.144 V */ 00218 CONFIG_PGA_FSR_4_096_V = ( 0b001 << 9U ), /*!< FSR = ±4.096 V */ 00219 CONFIG_PGA_FSR_2_048_V = ( 0b010 << 9U ), /*!< FSR = ±2.048 V [ Default ] */ 00220 CONFIG_PGA_FSR_1_024_V = ( 0b011 << 9U ), /*!< FSR = ±1.024 V */ 00221 CONFIG_PGA_FSR_0_512_V = ( 0b100 << 9U ), /*!< FSR = ±0.512 V */ 00222 CONFIG_PGA_FSR_0_256_V = ( 0b101 << 9U ) /*!< FSR = ±0.256 V */ 00223 } ADS111X_config_pga_t ; 00224 00225 00226 /* MODE <8> 00227 * NOTE: Device operating mode. 00228 */ 00229 typedef enum { 00230 CONFIG_MODE_MASK = ( 1U << 8U ), /*!< MODE mask */ 00231 CONFIG_MODE_CONTINUOUS_CONVERSION = ( 0U << 8U ), /*!< Continuous-conversion mode */ 00232 CONFIG_MODE_SINGLE_SHOT = ( 1U << 8U ) /*!< Single-shot mode or power-down state [ Default ] */ 00233 } ADS111X_config_mode_t ; 00234 00235 00236 /* DR <7:5> 00237 * NOTE: Data rate. 00238 */ 00239 typedef enum { 00240 CONFIG_DR_MASK = ( 0b111 << 5U ), /*!< DR mask */ 00241 CONFIG_DR_8_SPS = ( 0b000 << 5U ), /*!< 8 SPS */ 00242 CONFIG_DR_16_SPS = ( 0b001 << 5U ), /*!< 16 SPS */ 00243 CONFIG_DR_32_SPS = ( 0b010 << 5U ), /*!< 32 SPS */ 00244 CONFIG_DR_64_SPS = ( 0b011 << 5U ), /*!< 64 SPS */ 00245 CONFIG_DR_128_SPS = ( 0b100 << 5U ), /*!< 128 SPS [ Default ] */ 00246 CONFIG_DR_250_SPS = ( 0b101 << 5U ), /*!< 250 SPS */ 00247 CONFIG_DR_475_SPS = ( 0b110 << 5U ), /*!< 475 SPS */ 00248 CONFIG_DR_860_SPS = ( 0b111 << 5U ) /*!< 860 SPS */ 00249 } ADS111X_config_dr_t ; 00250 00251 00252 /* COMP_MODE <4> 00253 * NOTE: Comparator mode ( ADS1114 and ADS1115 only ) 00254 */ 00255 typedef enum { 00256 CONFIG_COMP_MODE_MASK = ( 1U << 4U ), /*!< COMP_MODE mask */ 00257 CONFIG_COMP_MODE_TRADITIONAL_COMPARATOR = ( 0U << 4U ), /*!< Traditional comparator [ Default ] */ 00258 CONFIG_COMP_MODE_WINDOW_COMPARATOR = ( 1U << 4U ) /*!< Window comparator */ 00259 } ADS111X_config_comp_mode_t ; 00260 00261 00262 /* COMP_POL <3> 00263 * NOTE: Comparator polarity ( ADS1114 and ADS1115 only ) 00264 */ 00265 typedef enum { 00266 CONFIG_COMP_POL_MASK = ( 1U << 3U ), /*!< COMP_POL mask */ 00267 CONFIG_COMP_POL_ACTIVE_LOW = ( 0U << 3U ), /*!< Active low [ Default ] */ 00268 CONFIG_COMP_POL_ACTIVE_HIGH = ( 1U << 3U ) /*!< Active high */ 00269 } ADS111X_config_comp_pol_t ; 00270 00271 00272 /* COMP_LAT <2> 00273 * NOTE: Latching comparator ( ADS1114 and ADS1115 only ) 00274 */ 00275 typedef enum { 00276 CONFIG_COMP_LAT_MASK = ( 1U << 2U ), /*!< COMP_LAT mask */ 00277 CONFIG_COMP_LAT_NONLATCHING_COMPARATOR = ( 0U << 2U ), /*!< Nonlatching comparator [ Default ] */ 00278 CONFIG_COMP_LAT_LATCHING_COMPARATOR = ( 1U << 2U ) /*!< Latching comparator */ 00279 } ADS111X_config_comp_lat_t ; 00280 00281 00282 /* COMP_QUE <1:0> 00283 * NOTE: Comparator queue and disable ( ADS1114 and ADS1115 only ) 00284 */ 00285 typedef enum { 00286 CONFIG_COMP_QUE_MASK = ( 0b11 << 0U ), /*!< COMP_QUE mask */ 00287 CONFIG_COMP_QUE_ASSERT_AFTER_ONE_CONVERSION = ( 0b00 << 0U ), /*!< Assert after one conversion */ 00288 CONFIG_COMP_QUE_ASSERT_AFTER_TWO_CONVERSION = ( 0b01 << 0U ), /*!< Assert after two conversions */ 00289 CONFIG_COMP_QUE_ASSERT_AFTER_FOUR_CONVERSION = ( 0b10 << 0U ), /*!< Assert after four conversions */ 00290 CONFIG_COMP_QUE_DISABLED = ( 0b11 << 0U ) /*!< Disable comparator and set ALERT/RDY pin to high-impedance [ Default ] */ 00291 } ADS111X_config_comp_que_t ; 00292 00293 00294 00295 /** 00296 * @brief LO_THRESH REGISTER. ( Default: 0x8000 ) 00297 */ 00298 /* LO_THRESH <15:0> 00299 * NOTE: N/A. 00300 */ 00301 typedef enum { 00302 LO_THRESH_MASK = 0xFFFF /*!< LO_THRESH mask */ 00303 } ADS111X_lo_thresh_t; 00304 00305 00306 00307 /** 00308 * @brief HI_THRESH REGISTER. ( Default: 0x7FFF ) 00309 */ 00310 /* HI_THRESH <15:0> 00311 * NOTE: N/A. 00312 */ 00313 typedef enum { 00314 HI_THRESH_MASK = 0xFFFF /*!< HI_THRESH mask */ 00315 } ADS111X_hi_thresh_t; 00316 00317 00318 00319 00320 00321 #ifndef ADS111X_VECTOR_STRUCT_H 00322 #define ADS111X_VECTOR_STRUCT_H 00323 /* Configuration parameters */ 00324 typedef struct { 00325 ADS111X_config_os_t os; /*!< Operational status */ 00326 ADS111X_config_mux_t mux; /*!< Input multiplexer configuration (ADS1015 only) */ 00327 ADS111X_config_pga_t pga; /*!< Programmable gain amplifier configuration (not ADS1013) */ 00328 ADS111X_config_mode_t mode; /*!< Device operating mode */ 00329 ADS111X_config_dr_t dr; /*!< Data rate */ 00330 ADS111X_config_comp_mode_t comp_mode; /*!< Comparator mode (ADS1014 and ADS1015 only) */ 00331 ADS111X_config_comp_pol_t comp_pol; /*!< Comparator polarity (ADS1014 and ADS1015 only) */ 00332 ADS111X_config_comp_lat_t comp_lat; /*!< Latching comparator (ADS1014 and ADS1015 only) */ 00333 ADS111X_config_comp_que_t comp_que; /*!< Comparator queue and disable (ADS1014 and ADS1015 only) */ 00334 } ADS111X_config_t; 00335 00336 00337 /* Thresholds: High and low thresholds */ 00338 typedef struct { 00339 int16_t lo_thresh; /*!< Low threshold value */ 00340 int16_t hi_thresh; /*!< High threshold value */ 00341 } ADS111X_thresh_t; 00342 00343 00344 /* Result Conversion: Raw value and conversion value */ 00345 typedef struct { 00346 float conversion; /*!< Conversion value */ 00347 int16_t raw_conversion; /*!< Raw conversion value */ 00348 } ADS111X_conversion_t; 00349 00350 00351 00352 /* USER: User's global variables */ 00353 typedef struct { 00354 /* Output */ 00355 ADS111X_conversion_t conversion; /*!< Conversion values */ 00356 00357 /* Configuration */ 00358 ADS111X_config_t config; /*!< Configuration register */ 00359 00360 /* Thresholds */ 00361 ADS111X_thresh_t thresh; /*!< High/Low threshold values */ 00362 } ADS111X_data_t; 00363 #endif 00364 00365 00366 /** 00367 * @brief INTERNAL CONSTANTS 00368 */ 00369 typedef enum { 00370 ADS111X_SUCCESS = 0U, /*!< I2C communication success */ 00371 ADS111X_FAILURE = 1U, /*!< I2C communication failure */ 00372 ADS111X_DEVICE_NOT_SUPPORTED = 2U, /*!< Device not supported */ 00373 ADS111X_VALUE_OUT_OF_RANGE = 3U, /*!< Value aout of range */ 00374 ADS111X_DATA_CORRUPTED = 4U, /*!< D and lo/hi threshold data */ 00375 I2C_SUCCESS = 0U /*!< I2C communication was fine */ 00376 } ADS111X_status_t; 00377 00378 00379 00380 00381 /** Create an ADS111X object connected to the specified I2C pins. 00382 * 00383 * @param sda I2C data pin 00384 * @param scl I2C clock pin 00385 * @param addr I2C slave address 00386 * @param freq I2C frequency 00387 * @param device Device to use: ADS1113, ADS1114 or ADS1115 00388 */ 00389 ADS111X ( PinName sda, PinName scl, uint32_t addr, uint32_t freq, ADS111X_device_t device ); 00390 00391 /** Delete ADS111X object. 00392 */ 00393 ~ADS111X(); 00394 00395 /** It triggers a softreset. 00396 */ 00397 ADS111X_status_t ADS111X_SoftReset ( void ); 00398 00399 /** It gets the raw conversion value. 00400 */ 00401 ADS111X_status_t ADS111X_GetRawConversion ( ADS111X_conversion_t* myRawD ); 00402 00403 /** It gets the conversion value. 00404 */ 00405 ADS111X_status_t ADS111X_GetConversion ( ADS111X_data_t* myD ); 00406 00407 /** It starts a new single conversion. 00408 */ 00409 ADS111X_status_t ADS111X_StartSingleConversion ( void ); 00410 00411 /** It checks if the device is not currently performing a conversion. 00412 */ 00413 ADS111X_status_t ADS111X_GetOS ( ADS111X_config_t* myADS111X ); 00414 00415 /** It sets input multiplexer configuration ( ADS1015 only ). 00416 */ 00417 ADS111X_status_t ADS111X_SetMux ( ADS111X_data_t myADS111X ); 00418 00419 /** It gets input multiplexer configuration ( ADS1015 only ). 00420 */ 00421 ADS111X_status_t ADS111X_GetMux ( ADS111X_data_t* myADS111X ); 00422 00423 /** It sets programmable gain amplifier ( not ADS1013 ). 00424 */ 00425 ADS111X_status_t ADS111X_SetGain ( ADS111X_data_t myPGA ); 00426 00427 /** It gets programmable gain amplifier ( not ADS1013 ). 00428 */ 00429 ADS111X_status_t ADS111X_GetGain ( ADS111X_data_t* myPGA ); 00430 00431 /** It sets the device operating mode. 00432 */ 00433 ADS111X_status_t ADS111X_SetMode ( ADS111X_config_t myMode ); 00434 00435 /** It gets the device operating mode. 00436 */ 00437 ADS111X_status_t ADS111X_GetMode ( ADS111X_config_t* myMode ); 00438 00439 /** It sets the data rate. 00440 */ 00441 ADS111X_status_t ADS111X_SetDataRate ( ADS111X_config_t myDR ); 00442 00443 /** It gets the data rate. 00444 */ 00445 ADS111X_status_t ADS111X_GetDataRate ( ADS111X_config_t* myDR ); 00446 00447 /** It sets the comparator configuration. 00448 */ 00449 ADS111X_status_t ADS111X_SetComparator ( ADS111X_data_t myCOMP ); 00450 00451 /** It gets the comparator configuration. 00452 */ 00453 ADS111X_status_t ADS111X_GetComparator ( ADS111X_data_t* myCOMP ); 00454 00455 /** It sets the low threshold value. 00456 */ 00457 ADS111X_status_t ADS111X_SetLowThresholdValue ( ADS111X_thresh_t myLoThres ); 00458 00459 /** It gets the low threshold value. 00460 */ 00461 ADS111X_status_t ADS111X_GetLowThresholdValue ( ADS111X_thresh_t* myLoThres ); 00462 00463 /** It sets the high threshold value. 00464 */ 00465 ADS111X_status_t ADS111X_SetHighThresholdValue ( ADS111X_thresh_t myHiThres ); 00466 00467 /** It gets the high threshold value. 00468 */ 00469 ADS111X_status_t ADS111X_GetHighThresholdValue ( ADS111X_thresh_t* myHiThres ); 00470 00471 00472 00473 private: 00474 I2C _i2c; 00475 uint32_t _ADS111X_Addr; 00476 ADS111X_device_t _device; 00477 }; 00478 00479 #endif
Generated on Thu Jul 14 2022 05:51:18 by
