Skip to content

Commit 7732e96

Browse files
committed
Add Example04. Update readRegisterRegion - needs Toolkit adderr branch
1 parent 76ec81d commit 7732e96

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

examples/Example04_DataReadyInterrupt/Example04_DataReadyInterrupt.ino

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bool interruptSeen = false; // A global flag to indicate if the DRDY interrupt h
3838
void dataReadyISR(void)
3939
{
4040
// Always keep interrupt Interrupt Service Routines as short as possible.
41-
// Do not try to print or perform bus reads and writes inside them.
41+
// Do not try to print or perform bus transfers inside them.
4242
// Here we only set the interruptSeen flag.
4343
// The ADC conversion result will be read in the loop().
4444
interruptSeen = true;
@@ -54,10 +54,12 @@ void setup()
5454
{
5555
delay(100); // Wait for the user to open the Serial Monitor
5656
};
57-
Serial.println("SparkFun ADS1219 Example");
57+
//Serial.println("SparkFun ADS1219 Example"); // Commented for the Serial Plotter
5858

5959
Wire.begin(); // Begin the I2C bus
6060

61+
Wire.setClock(400000); // We are OK at 100kHz, but 400kHz is better
62+
6163
// Initialize ADC - this also performs a soft reset
6264
while (myADC.begin() == false)
6365
{
@@ -68,7 +70,8 @@ void setup()
6870
// Configure the input multiplexer
6971
myADC.setInputMultiplexer(ADS1219_CONFIG_MUX_SINGLE_0); // Read the voltage between AIN0 and GND
7072

71-
// Configure the gain to x4, so we can measuure small voltages with higher resolution
73+
// Set the gain to x4, so we can measure small voltages with higher resolution
74+
// Note: we are limited to 0.512V max when using a gain of 4 and the internal 2.048V reference
7275
myADC.setGain(ADS1219_GAIN_4);
7376

7477
// Configure the data rate. The ADC will sample at 1000 samples per second
@@ -91,7 +94,7 @@ void loop()
9194
{
9295
interruptSeen = false; // Clear the flag ready for the next interrupt
9396
myADC.readConversion(); // Read the conversion result from the ADC. Store it internally.
94-
int32_t rawADC = myADC.getConversionRaw(); // Just to be different, read the raw ADC value.
95-
Serial.println(rawADC); // Print the raw ADC value
97+
float milliVolts = myADC.getConversionMillivolts(); // Convert to millivolts.
98+
Serial.println(milliVolts, 3); // Print milliVolts with 3 decimal places
9699
}
97100
}

src/SparkFun_ADS1219.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ bool SfeADS1219Driver::setInputMultiplexer(const ads1219_input_multiplexer_confi
8282

8383
/// @brief Configure the gain.
8484
/// @return True if successful, false otherwise.
85-
bool setGain(const ads1219_gain_config_t gain)
85+
bool SfeADS1219Driver::setGain(const ads1219_gain_config_t gain)
8686
{
8787
sfe_ads1219_reg_cfg_t config;
8888
if (_theBus->readRegisterByte(kSfeADS1219RegConfigRead, config.byte) != kSTkErrOk) // Read the config register
@@ -94,7 +94,7 @@ bool setGain(const ads1219_gain_config_t gain)
9494

9595
/// @brief Configure the data rate (samples per second).
9696
/// @return True if successful, false otherwise.
97-
bool setDataRate(const ads1219_data_rate_config_t rate)
97+
bool SfeADS1219Driver::setDataRate(const ads1219_data_rate_config_t rate)
9898
{
9999
sfe_ads1219_reg_cfg_t config;
100100
if (_theBus->readRegisterByte(kSfeADS1219RegConfigRead, config.byte) != kSTkErrOk) // Read the config register
@@ -105,7 +105,7 @@ bool setDataRate(const ads1219_data_rate_config_t rate)
105105

106106
/// @brief Configure the voltage reference.
107107
/// @return True if successful, false otherwise.
108-
bool setVoltageReference(const ads1219_vref_config_t vRef)
108+
bool SfeADS1219Driver::setVoltageReference(const ads1219_vref_config_t vRef)
109109
{
110110
sfe_ads1219_reg_cfg_t config;
111111
if (_theBus->readRegisterByte(kSfeADS1219RegConfigRead, config.byte) != kSTkErrOk) // Read the config register
@@ -120,7 +120,9 @@ bool setVoltageReference(const ads1219_vref_config_t vRef)
120120
bool SfeADS1219Driver::readConversion()
121121
{
122122
uint8_t rawBytes[3];
123-
bool result = (_theBus->readRegisterRegion(kSfeADS1219CommandReadData, (uint8_t *)rawBytes, 3) == 3);
123+
size_t readBytes;
124+
bool result = (_theBus->readRegisterRegion(kSfeADS1219CommandReadData, (uint8_t *)rawBytes, 3, readBytes) == kSTkErrOk);
125+
result = result && (readBytes == 3); // Check three bytes were returned
124126
if (result)
125127
{
126128
// Data is 3-bytes (24-bits), big-endian (MSB first).
@@ -166,6 +168,24 @@ bool SfeADS1219Driver::dataReady(void)
166168
return (result && (status.drdy == 1));
167169
}
168170

171+
/// @brief Read the ADS1219 Configuration Register into a sfe_ads1219_reg_cfg_t struct.
172+
/// @param config Pointer to the sfe_ads1219_reg_cfg_t struct to hold the register contents.
173+
/// @return True if successful, false otherwise.
174+
bool SfeADS1219Driver::getConfigurationRegister(sfe_ads1219_reg_cfg_t *config)
175+
{
176+
return (_theBus->readRegisterByte(kSfeADS1219RegConfigRead, config->byte) == kSTkErrOk); // Read the config register
177+
}
178+
179+
/// @brief Write a sfe_ads1219_reg_cfg_t struct into the ADS1219 Configuration Register.
180+
/// @param config A sfe_ads1219_reg_cfg_t struct holding the register contents.
181+
/// @return True if successful, false otherwise.
182+
bool SfeADS1219Driver::setConfigurationRegister(sfe_ads1219_reg_cfg_t config)
183+
{
184+
return (_theBus->writeRegisterByte(kSfeADS1219RegConfigWrite, config.byte) == kSTkErrOk); // Write the config register
185+
}
186+
187+
/// @brief PRIVATE: update the local pointer to the I2C bus.
188+
/// @param theBus Pointer to the bus object.
169189
void SfeADS1219Driver::setCommunicationBus(sfeTkArdI2C *theBus)
170190
{
171191
_theBus = theBus;

src/SparkFun_ADS1219.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,16 @@ class SfeADS1219Driver
210210
/// @return true if data is ready.
211211
bool dataReady(void);
212212

213+
/// @brief Read the ADS1219 Configuration Register into a sfe_ads1219_reg_cfg_t struct.
214+
/// @param config Pointer to the sfe_ads1219_reg_cfg_t struct to hold the register contents.
215+
/// @return True if successful, false otherwise.
216+
bool getConfigurationRegister(sfe_ads1219_reg_cfg_t *config);
217+
218+
/// @brief Write a sfe_ads1219_reg_cfg_t struct into the ADS1219 Configuration Register.
219+
/// @param config A sfe_ads1219_reg_cfg_t struct holding the register contents.
220+
/// @return True if successful, false otherwise.
221+
bool setConfigurationRegister(sfe_ads1219_reg_cfg_t config);
222+
213223
protected:
214224
/// @brief Sets the communication bus to the specified bus.
215225
/// @param theBus Bus to set as the communication devie.

0 commit comments

Comments
 (0)