/**************************************************************************/
-/*!
+/*!
@file ads1015.c
@author K. Townsend (microBuilder.eu)
-
+
@brief Drivers for the TI ADS1015 12-Bit I2C ADC
@section DESCRIPTION
static bool _ads1015Initialised = false;
/**************************************************************************/
-/*!
+/*!
@brief Sends a single command byte over I2C
*/
/**************************************************************************/
}
/**************************************************************************/
-/*!
+/*!
@brief Reads a 16 bit values over I2C
*/
/**************************************************************************/
I2CMasterBuffer[0] = ADS1015_ADDRESS; // I2C device address
I2CMasterBuffer[1] = reg; // Command register
// Append address w/read bit
- I2CMasterBuffer[2] = ADS1015_ADDRESS | ADS1015_READBIT;
+ I2CMasterBuffer[2] = ADS1015_ADDRESS | ADS1015_READBIT;
i2cEngine();
// Shift values to create properly formed integer
}
/**************************************************************************/
-/*!
+/*!
@brief Initialises the I2C block
*/
/**************************************************************************/
}
/**************************************************************************/
-/*!
+/*!
@brief Reads the 12-bit conversion results from the specified channel
*/
/**************************************************************************/
}
/**************************************************************************/
-/*!
+/*!
@brief Reads the 12-bit conversion results, measuring the voltage
difference between the P (AIN0) and N (AIN1) input. Generates
a signed 12-bit value since the difference can be either
}
/**************************************************************************/
-/*!
+/*!
@brief Reads the 12-bit conversion results, measuring the voltage
difference between the P (AIN2) and N (AIN3) input. Generates
a signed 12-bit value since the difference can be either
return error;
}
+/**************************************************************************/
+/*!
+ @brief Sets up the comparator to operate in basic mode, causing the
+ ALERT/RDY pin to assert (go from high to low) when the ADC
+ value exceeds the specified threshold.
+
+ This will also set the ADC in continuous conversion mode.
+
+ @section EXAMPLE
+
+ @code
+ ads1015Init();
+
+ // Setup 3V comparator on channel 0
+ ads1015StartComparator_SingleEnded(0, 1000);
+
+ int16_t results;
+ while(1)
+ {
+ // Need to read to clear com bit once it's set
+ ads1015GetLastConversionResults(&results);
+ printf("%d\r\n", results);
+ }
+ @endcode
+*/
+/**************************************************************************/
+ads1015Error_t ads1015StartComparator_SingleEnded(uint8_t channel, int16_t threshold)
+{
+ uint16_t value;
+
+ ads1015Error_t error = ADS1015_ERROR_OK;
+
+ if (!(_ads1015Initialised))
+ {
+ ads1015Init();
+ }
+
+ // Start with default values
+ uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match
+ ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode
+ ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
+ ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
+ ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
+ ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode
+ ADS1015_REG_CONFIG_PGA_6_144V | // +/- 6.144V range (limited to VDD +0.3V max!)
+ ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode
+
+ // Set single-ended input channel
+ switch (channel)
+ {
+ case (0):
+ config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
+ break;
+ case (1):
+ config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
+ break;
+ case (2):
+ config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
+ break;
+ case (3):
+ config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
+ break;
+ }
+
+ // Set the high threshold register
+ error = ads1015WriteRegister(ADS1015_REG_POINTER_HITHRESH, threshold << 4);
+ if (error) return error;
+
+ // Write config register to the ADC
+ error = ads1015WriteRegister(ADS1015_REG_POINTER_CONFIG, config);
+ if (error) return error;
+}
+
+/**************************************************************************/
+/*!
+ @brief In order to clear the comparator, we need to read the
+ conversion results. This function reads the last conversion
+ results without changing the config value.
+*/
+/**************************************************************************/
+ads1015Error_t ads1015GetLastConversionResults(int16_t *value)
+{
+ ads1015Error_t error = ADS1015_ERROR_OK;
+
+ if (!(_ads1015Initialised))
+ {
+ ads1015Init();
+ }
+
+ // Wait for the conversion to complete
+ systickDelay(1);
+
+ // Read the conversion results
+ error = ina219Read16(ADS1015_REG_POINTER_CONVERT, value);
+ if (error) return error;
+
+ // Shift results 4-bits to the right
+ *value = *value >> 4;
+
+ return error;
+}
+
+
+
+
/**************************************************************************/
-/*!
+/*!
@file ads1015.h
@author K. Townsend (microBuilder.eu)
#define ADS1015_REG_CONFIG_MODE_CONTIN (0x0000) // Continuous conversion mode
#define ADS1015_REG_CONFIG_MODE_SINGLE (0x0100) // Power-down single-shot mode (default)
- #define ADS1015_REG_CONFIG_DR_MASK (0x00E0)
+ #define ADS1015_REG_CONFIG_DR_MASK (0x00E0)
#define ADS1015_REG_CONFIG_DR_128SPS (0x0000) // 128 samples per second
#define ADS1015_REG_CONFIG_DR_250SPS (0x0020) // 250 samples per second
#define ADS1015_REG_CONFIG_DR_490SPS (0x0040) // 490 samples per second
ads1015Error_t ads1015ReadADC_SingleEnded(uint8_t channel, uint16_t *value);
ads1015Error_t ads1015ReadADC_Differential_0_1(int16_t *value);
ads1015Error_t ads1015ReadADC_Differential_2_3(int16_t *value);
+ads1015Error_t ads1015StartComparator_SingleEnded(uint8_t channel, int16_t threshold);
+ads1015Error_t ads1015GetLastConversionResults(int16_t *value);
#endif