help with ADC 2 Complement calulation - Page 1
Forum section: electronics / projects

author: gogoman
author_rank: Regular Contributor
timestamp: October 03, 2020, 11:58:00 pm
content: Hello, I'm trying to unwrap my head around a ADC and it's 2's complement output.Question 1, Can someone show me how to convert, +1V and -1V input to ADC counts?using a generic ADC with differential inputs, which is reference to groundGiven: ADC output is two complement.Vref is 2.5V, reference to gndADC resolution is 16bit's,Volts to bits 2.5V/ 2^16 = 38uV/bit.ADC common mode voltage is 1.25V, Reference to groundInputs to the ADC is In+ and In-Question 2, because of the sign bit, does the ADC loose resolution, why would this be the case?Thanks

author: RandallMcRee
author_rank: Frequent Contributor
timestamp: October 04, 2020, 03:42:22 am
content: Not sure what you are confused about. The ADC I am using is an LTC2499, 24 bits, differential inputs. The output is a 24 bit signed binary number with a few top digits denoting under or overflow. The output is always relative to VREF which in this case is +4.096 volts. So the total range is (theoretically) 4096/2^^24. The common mode is 2.048v. The negative range is represented by 0-2.048V and the positive range is 2.048 to 4.096v. A "0" denotes 2.047 volts, in reality Vref/2.I verified all of this empirically just by feeding in known voltages. 'Cause I'm a newbie with ADCs.Hope that makes sense.Here is the explanation of the ADC range from the datasheet, I found it helped me to understand:

author: gogoman
author_rank: Regular Contributor
timestamp: October 04, 2020, 06:56:50 am
content: Hello, I'm stuck on the fact that Vref is divided by 2 which effectively splits the range by two with the number of bits (12 bits, 0V to 2.048V and 12 bits 0 to -2.048V.Would you mind sharing the calculation to convert a given voltage to ADC counts.thanks

author: Siwastaja
author_rank: Super Contributor
timestamp: October 04, 2020, 08:27:05 am
content: 2's complement is the name of the most commonly used number system in practically all microprocessors today.There is absolutely no need to think about "sign bit" or do anything with it.Just use the result as-is. Because it's 16 bits, you don't even need to shift it; just store in int16_t type (in C99) or equivalent and you can access the range -32768..+32767 through that variable.

author: gogoman
author_rank: Regular Contributor
timestamp: October 04, 2020, 02:38:43 pm
content: Hello,given a 16 bit converter, the resolution is = Vref/ADC Resolution = 2.5v/2^16 = 38uV. the input voltage to the adc is .1volt. The ADC output is in two complementusing this formula "ADC voltage = ADC cnts * 38uV" the calculated ADC count is 1/2 the measured.using this formula "ADC Voltage = ADC cnts * 76uV" (38uV *2) the calculation and measure ADC count agree.(corrected formula)what I'm i doing wrongthanks

author: Siwastaja
author_rank: Super Contributor
timestamp: October 04, 2020, 03:08:13 pm
content: With differential inputs, it measures from -Vref to +Vref.Say you have inputs "+" and "-". When "+" input is below "-" input, the result is negative voltage. When the "+" input is above "-" input, the result is positive voltage.Measuring from -2.5V to +2.5V means range of 5V. -2.5V equals the most negative number, -32768. +2.5V equals the most positive number, +32767.You should be dividing 5V by 2^16 because the measurement range is 5 volts.

author: gogoman
author_rank: Regular Contributor
timestamp: October 04, 2020, 03:55:32 pm
content: hello, for the math to workout:Calculated positive Adc voltage = (2.5V/32767) * Adc CountsCalculated negative Adc voltage = (2.5V/-32768) * Adc Countsis this correct

author: RandallMcRee
author_rank: Frequent Contributor
timestamp: October 04, 2020, 04:24:32 pm
content: Quote from: gogoman on October 04, 2020, 06:56:50 amNo, no. Half of a 24 bit range is 23 bits. 23 bits not 12. Think about it.2^^24 = 16777216. half of that is 8388608. That is 2^^23, not 2^^12.That is at least one of your misconceptions.My adc gives me a signed number as output, so the if the input (V+ - V-) is below 2.5 volts the adc outputs a negative number and if the input is above 2.5v it is a positive number.[Let me clarify a little: the input to the ADC is the output of an instrumentation amplifier. The V- input to the ADC is the common mode voltage supplied to the Instr. amp. V+ in to the ADC is the Instr. amp output. Third diagram in my initial post]If the ADC gave me simply a number of counts instead, I would do the same calculation but subtract the common mode voltage from the result. There is not one calculation for negative and one for positive. (Review the diagram I posted earlier it shows this clearly?!)Here is the formula for the LTC2499:Code:float Ard2499::ltc2499ReadVoltage(){long value = ltc2499Read();if (LTC2499_OVERRANGE_POSITIVE == value)return(1e38f);if (LTC2499_OVERRANGE_NEGATIVE == value)return(-1 * 1e37f);return ((value / 16777216.0f) * (referenceMillivolts / 2000.0f));}Hopefully this starts to make sense.

author: gogoman
author_rank: Regular Contributor
timestamp: October 04, 2020, 04:52:57 pm
content: Hello, RandallMcree, thanks for sharing the code."The ADC I am using is an LTC2499, 24 bits, differential inputs. The output is a 24 bit signed binary number with a few top digits denoting under or overflow. The output is always relative to VREF which in this case is +4.096 volts. So the total range is (theoretically) 4096/2^^24. The common mode is 2.048v. The negative range is represented by 0-2.048V and the positive range is 2.048 to 4.096v. A "0" denotes 2.047 volts, in reality Vref/2."From the above, and plugging it into your code:ADC Voltage = ((value / 16777216.0f) * (referenceMillivolts / 2000.0f));Value = ADC counts16777216 = 2^^24 ADC resolutionReference Milivolts = 4.096V = 4096mVTo convert mV to Volts = divide Reference/2000I'm in the process of reviewing, but is the above correct