Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract and expose getFsRange() #94

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions Adafruit_ADS1X15.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,39 +276,49 @@ int16_t Adafruit_ADS1X15::getLastConversionResults() {

/**************************************************************************/
/*!
@brief Compute volts for the given raw counts.

@param counts the ADC reading in raw counts
@brief Return the current fs range for the configured gain

@return the ADC reading in volts
@return the fsRange for the configured gain, or zero.
Zero should not be possible thereby indicating error
*/
/**************************************************************************/
float Adafruit_ADS1X15::computeVolts(int16_t counts) {
float Adafruit_ADS1X15::getFsRange() {
// see data sheet Table 3
float fsRange;
switch (m_gain) {
case GAIN_TWOTHIRDS:
fsRange = 6.144f;
return 6.144f;
break;
case GAIN_ONE:
fsRange = 4.096f;
return 4.096f;
break;
case GAIN_TWO:
fsRange = 2.048f;
return 2.048f;
break;
case GAIN_FOUR:
fsRange = 1.024f;
return 1.024f;
break;
case GAIN_EIGHT:
fsRange = 0.512f;
return 0.512f;
break;
case GAIN_SIXTEEN:
fsRange = 0.256f;
return 0.256f;
break;
default:
fsRange = 0.0f;
return 0.0f;
}
return counts * (fsRange / (32768 >> m_bitShift));
}

/**************************************************************************/
/*!
@brief Compute volts for the given raw counts.

@param counts the ADC reading in raw counts

@return the ADC reading in volts
*/
/**************************************************************************/
float Adafruit_ADS1X15::computeVolts(int16_t counts) {
return counts * (getFsRange() / (32768 >> m_bitShift));
}

/**************************************************************************/
Expand Down
3 changes: 2 additions & 1 deletion Adafruit_ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ constexpr uint16_t MUX_BY_CHANNEL[] = {
ADS1X15_REG_CONFIG_MUX_SINGLE_1, ///< Single-ended AIN1
ADS1X15_REG_CONFIG_MUX_SINGLE_2, ///< Single-ended AIN2
ADS1X15_REG_CONFIG_MUX_SINGLE_3 ///< Single-ended AIN3
}; ///< MUX config by channel
}; ///< MUX config by channel

#define ADS1X15_REG_CONFIG_PGA_MASK (0x0E00) ///< PGA Mask
#define ADS1X15_REG_CONFIG_PGA_6_144V (0x0000) ///< +/-6.144V range = Gain 2/3
Expand Down Expand Up @@ -162,6 +162,7 @@ class Adafruit_ADS1X15 {
int16_t readADC_Differential_2_3();
void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
int16_t getLastConversionResults();
float getFsRange();
float computeVolts(int16_t counts);
void setGain(adsGain_t gain);
adsGain_t getGain();
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ readADC_Differential_0_1 KEYWORD2
readADC_Differential_2_3 KEYWORD2
startComparator_SingleEnded KEYWORD2
getLastConversionResults KEYWORD2
getFsRange KEYWORD1
computeVolts KEYWORD2
setGain KEYWORD2
getGain KEYWORD2
Expand Down
Loading