Ultra-Small, Low-Power, I2C-Compatible, 860-SPS, 16-Bit ADCs With Internal Reference, Oscillator, and Programmable Comparator
Diff: ADS111X.h
- Revision:
- 3:4a9619b441f0
- Parent:
- 1:8593e6fcf0c3
- Child:
- 4:7853bced749c
--- a/ADS111X.h Thu Jun 18 16:11:01 2020 +0000 +++ b/ADS111X.h Fri Jun 19 10:16:03 2020 +0000 @@ -22,7 +22,115 @@ /** Example: @code +#include "mbed.h" +#include "ADS111X.h" +ADS111X myADS111X ( I2C_SDA, I2C_SCL, ADS111X::ADS111X_ADDRESS_GND, 100000, ADS111X::DEVICE_ADS1115 ); // I2C_SDA | I2C_SCL | DEVICE_ADS1115 +Serial pc ( USBTX, USBRX ); // tx, rx + +DigitalOut myled ( LED1 ); +Ticker newAction; + + +//@brief Constants. + + +//@brief Variables. +volatile uint32_t myState; // State that indicates when to perform a new sample + + +//@brief FUNCTION PROTOTYPES +void changeDATA ( void ); + + +//@brief FUNCTION FOR APPLICATION MAIN ENTRY. +int main() +{ + ADS111X::ADS111X_status_t aux; + ADS111X::ADS111X_data_t myADS111X_Data; + + pc.baud ( 115200 ); + + myled = 1; + wait(3); + myled = 0; + + // Perform a softreset + aux = myADS111X.ADS111X_SoftReset (); + wait_ms ( 500U ); + + // Input multiplexor configuration ( channels ): AINp = AIN0 | AINn = GND + myADS111X_Data.config.mux = ADS111X::CONFIG_MUX_AINP_AIN0_AND_AINN_GND; + aux = myADS111X.ADS111X_SetMux ( myADS111X_Data ); + + // Gain: ±4.096V + myADS111X_Data.config.pga = ADS111X::CONFIG_PGA_FSR_4_096_V; + aux = myADS111X.ADS111X_SetGain ( myADS111X_Data ); + + // Mode: Single-shot + myADS111X_Data.config.mode = ADS111X::CONFIG_MODE_SINGLE_SHOT; + aux = myADS111X.ADS111X_SetMode ( myADS111X_Data.config ); + + // Data rate: 1600 SPS + myADS111X_Data.config.dr = ADS111X::CONFIG_DR_128_SPS; + aux = myADS111X.ADS111X_SetDataRate ( myADS111X_Data.config ); + + // Comparator: Disabled + myADS111X_Data.config.comp_que = ADS111X::CONFIG_COMP_QUE_DISABLED; + aux = myADS111X.ADS111X_SetComparator ( myADS111X_Data ); + + myState = 0UL; // Reset the variable + newAction.attach( &changeDATA, 1U ); // the address of the function to be attached ( changeDATA ) and the interval ( 1s ) + + // Let the callbacks take care of everything + while(1) { + sleep(); + + if ( myState == 1UL ) { + myled = 1U; + + // Trigger a new conversion + aux = myADS111X.ADS111X_StartSingleConversion (); + + // Wait until the conversion is completed + do { + aux = myADS111X.ADS111X_GetOS ( &myADS111X_Data.config ); + } while( ( myADS111X_Data.config.os & ADS111X::CONFIG_OS_MASK ) == ADS111X::CONFIG_OS_BUSY ); // [TODO] Too dangerous! the uC may get stuck here + // [WORKAROUND] Insert a counter. + // Get the result + aux = myADS111X.ADS111X_GetConversion ( &myADS111X_Data ); + + // Send data through the UART + pc.printf ( "V: %d mV\r\n", (int32_t)( 1000 * myADS111X_Data.conversion.conversion ) ); + + + // Reset the variables + myState = 0UL; + myled = 0U; + } + } +} + + +// @brief changeDATA ( void ) +// +// @details It changes myState variable +// +// @param[in] N/A +// +// @param[out] N/A. +// +// @return N/A. +// +// @author Manuel Caballero +// @date 18/June/2020 +// @version 18/June/2020 The ORIGIN +// @pre N/A +// @warning N/A. +void changeDATA ( void ) +{ + myState = 1UL; +} @endcode */ @@ -37,10 +145,10 @@ * @brief DEFAULT ADDRESSES */ typedef enum { - ADS111X_ADDRESS_GND = 0b1001000, /*!< I2C slave address byte, ADDR = GND */ - ADS111X_ADDRESS_VDD = 0b1001001, /*!< I2C slave address byte, ADDR = VDD */ - ADS111X_ADDRESS_SDA = 0b1001010, /*!< I2C slave address byte, ADDR = SDA */ - ADS111X_ADDRESS_SCL = 0b1001011 /*!< I2C slave address byte, ADDR = SCL */ + ADS111X_ADDRESS_GND = ( 0b1001000 << 1U ), /*!< I2C slave address byte, ADDR = GND */ + ADS111X_ADDRESS_VDD = ( 0b1001001 << 1U ), /*!< I2C slave address byte, ADDR = VDD */ + ADS111X_ADDRESS_SDA = ( 0b1001010 << 1U ), /*!< I2C slave address byte, ADDR = SDA */ + ADS111X_ADDRESS_SCL = ( 0b1001011 << 1U ) /*!< I2C slave address byte, ADDR = SCL */ } ADS111X_addresses_t; @@ -106,12 +214,12 @@ */ typedef enum { CONFIG_PGA_MASK = ( 0b111 << 9U ), /*!< PGA mask */ - CONFIG_PGA_FSR_6_144_V = ( 0b000 << 9U ), /*!< FSR = �6.144 V */ - CONFIG_PGA_FSR_4_096_V = ( 0b001 << 9U ), /*!< FSR = �4.096 V */ - CONFIG_PGA_FSR_2_048_V = ( 0b010 << 9U ), /*!< FSR = �2.048 V [ Default ] */ - CONFIG_PGA_FSR_1_024_V = ( 0b011 << 9U ), /*!< FSR = �1.024 V */ - CONFIG_PGA_FSR_0_512_V = ( 0b100 << 9U ), /*!< FSR = �0.512 V */ - CONFIG_PGA_FSR_0_256_V = ( 0b101 << 9U ) /*!< FSR = �0.256 V */ + CONFIG_PGA_FSR_6_144_V = ( 0b000 << 9U ), /*!< FSR = ±6.144 V */ + CONFIG_PGA_FSR_4_096_V = ( 0b001 << 9U ), /*!< FSR = ±4.096 V */ + CONFIG_PGA_FSR_2_048_V = ( 0b010 << 9U ), /*!< FSR = ±2.048 V [ Default ] */ + CONFIG_PGA_FSR_1_024_V = ( 0b011 << 9U ), /*!< FSR = ±1.024 V */ + CONFIG_PGA_FSR_0_512_V = ( 0b100 << 9U ), /*!< FSR = ±0.512 V */ + CONFIG_PGA_FSR_0_256_V = ( 0b101 << 9U ) /*!< FSR = ±0.256 V */ } ADS111X_config_pga_t;