# ADC math 2.5V ref 10 bit

Discussion in 'Programmer's Corner' started by mcgyvr, Jan 21, 2013.

1. ### mcgyvr Thread Starter AAC Fanatic!

Oct 15, 2009
5,394
1,194
Feeling stupid today..
Need the formula to convert from a 10 bit ADC value to a voltage to be displayed on an LCD with 5V full scale and 2.5V ref op amp to ADC.. So 0 volts is 2.5V (or bit 511 or 512) and -75V is bit 0 (0V op amp) and 75V is bit 1023 (5V op amp)
Need only whole numbers displayed..no decimals

2. ### t06afre AAC Fanatic!

May 11, 2009
5,936
1,226
One bit will be equal to (2*75)/1024 And this will be the bit weight(BW)
The result will be (ADC result*BW)-75

mcgyvr likes this.
3. ### mcgyvr Thread Starter AAC Fanatic!

Oct 15, 2009
5,394
1,194
Excellent.. Seems to work..
Knew it was simple but on this Monday morning my brain is still sleeping (or sobering up)

Appreciate the help. Thanks

4. ### WBahn Moderator

Mar 31, 2012
23,550
7,207
In general, you can just use the two endpoints to find the necessary equation. From your description, I assume that you want to display the following as a function of the ADC outputs at the two extremes:

In general

Therefore

-75V = a*0 + b

So

b=-75V

+75V = a*1023 + b
+75V = a*1023 - 75V

So

a = 150V/1023

Hence

The difference between using 1023 and 1024 is negligible. You may want to tweak this depending on how you want the rounding to occur.

5. ### t06afre AAC Fanatic!

May 11, 2009
5,936
1,226
This has been debated before. And the correct thing is (range/2^n) there n is the number of bits

6. ### WBahn Moderator

Mar 31, 2012
23,550
7,207
And the OP specified that he wanted the top count, 1023, to map to +75V. It's up to him to adjust his preamp circuits to make that happen, but the conversion from ADC output to display value should be based on what he has stated his scaling intentions are.

7. ### t06afre AAC Fanatic!

May 11, 2009
5,936
1,226
Yes but in a 10 bit ADC you have 1024 possible values in the range 0 to 1023. 1023 is 0x3FF in hex or 0b1111111111 binary. The number 1024 require 11 bit in the binary system

8. ### mcgyvr Thread Starter AAC Fanatic!

Oct 15, 2009
5,394
1,194
The formula provided by t06 in his first post works perfectly. My op amp scales it properly and that formula displays the result properly.

and yes 1024 possible values from 0 to 1023.

9. ### WBahn Moderator

Mar 31, 2012
23,550
7,207
Unless you are doing proper rounding, when you put in 75V and get a value from the ADC of 1023, the using 1024 as your divisor you will get a displayed value of 74V.

v = 1023*(2*75)/1024 - 75 = 74.84 => 74

The above, of course, was assuming infinite precision arithmetic within the expression.

As stated previously, it is doubtful that you will see any discernable difference between using 1023 ad 1024. Probably the only time you would notice it is when you were maxed out and you noted that you never get a reading of +75V. The details of which one is "correct" is dependent on how you scale your analog circuitry. The conversion factor needs to match the conversion being factored.

In practice, a bigger issue isn't whether you use 1023 or 1024, but how you want to handle the rounding. My guess is that you would like the displayed value to be within 0.5V of the actual value. But the current conversion (whether you use 1024 or 1024) can be up to 1V off. So you might be better off saying that I want it to stop displaying -75V when the input gets to -74.5V and I want it to start displaying +75V whent eh input gets to +74.5V. Figure out the ADC outputs at these two points and then use those in the process I outlined earlier. That will probably be ADC values of 3 and 1020. The difference will minor, but I think you will like the behavior a bit better.

Now, having said all that, there is another issue that raises its head and that is how negative integers are rounded by your compiler. Are they truncated, meaning that they are rounded toward negative infinity, or are they rounded toward zero?

10. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,430
1,311
This has also been discussed a lot before. The correct way to do rounding without corrupting the scale is to add half the divisor before rounding down with the division;

v = (1023*2*75)+512/1024 -75 = 75

That will round every value up or down, to the nearest result LSB, while at the same time keeping the integrity of the scale size.

11. ### ErnieM AAC Fanatic!

Apr 24, 2011
7,969
1,827
WBahn makes a simple assumption that the system is linear and fits a straight line. Given the endpoints of the design he correctly assigns these to the expression for a straight line, and correctly computes the coefficients of that line. It is an approach I have successfully used in the past and will continue to use.

If there is another approach I have yet to see anyone POST THE MATH behind such.

12. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,430
1,311
Using n-1 (ie divide by 1023) is fine, IF you don't mind having scale units that are the wrong size, and you don't mind the problem where all the low values are rounded DOWN,and all the high values are rounded UP.

Those are two significant "math" problems and are two very good reasons not to use a corrupted scale size to try and fudge easy rounding.

Apart from those two problems, a +512 /1024 solution is much smaller code and faster computationally than a /1023 solution.

How many math reasons do you need to do a job the right way?

13. ### WBahn Moderator

Mar 31, 2012
23,550
7,207
You seem to be saying that how you map ADC output values to signal values is independent of how signal values are mapped to ADC values in the first place (i.e., how the analog electronics are calibrated). I have stated several times, both in this and in the other thread that has been mentioned, that these two mappings have to match each other. If that is where you work from, then everything else will fall out from there. Why is that a wrong statement? Why is 1024 the ONLY number that is right, independent of how the analog signal is calibrated?

14. ### WBahn Moderator

Mar 31, 2012
23,550
7,207
Let's take a real simple example.

I have a 3-bit ADC. The input signal is adjusted so that the ADC will output 000 when the input is -4.0V (i.e., between -4.5V and -3.5V) and will output 111 when the input is +3V (i.e., between +2.5V and +3.5V).

What is the scale size? By how much does the input signal change for each incremental change in the ADC output?

15. ### Markd77 Senior Member

Sep 7, 2009
2,796
595
From the PIC midrange MCU family reference manual:

It's entirely possible to design an ADC that works differently, but this is presumably the one in the PIC in question.

• ###### Screenshot-57.png
File size:
37.1 KB
Views:
123
Last edited: Jan 23, 2013
16. ### THE_RB AAC Fanatic!

Feb 11, 2008
5,430
1,311
It's not the only number! The CORRECT number is determined by the ratio scale of voltage to ADC units.

Assuming the PIC ADC full scale is 5v (as per post #1) the correct scaling of ADC units to voltage is a ratio of 1024:5 which is found with the math ADC*5 /1024.

If 150v is scaled to equal 5v as you would expect, with a 150v:5v resistor divider, then any correct math will end with /1024 as it should.

I design industrial data conversion hardware that needs to do flawless ratio conversion for a living, for 13 years now. I get a bit sick of arguing the same very basic points re how to do ratio math conversion or ADC ratio conversion on a PIC.

I've put up a new thread on the matter here;
for anyone that wants to know what is wrong with /1023 and how to do it right.

Sorry to the OP for going off topic, which is why I made a new thread.

17. ### t06afre AAC Fanatic!

May 11, 2009
5,936
1,226
In all ADC system. The conversion will be done against some sort of reference. Let us assume that we have PIC like 10 bit system with a reference equal 5.00000 volt. Let us name the 5 volt Full Scale(FS) What you and some other on this forum tend to forget. Is that the analog value represented by the all-ones code. And hence the highest value we can measure. Is not FS, but FS – 1 LSB. And one LSB will in this case be 5/1024.
For some free and good info. I will recommend this http://www.analog.com/library/analogDialogue/archives/39-06/data_conversion_handbook.html At least take a look at section Chapter 2: Fundamentals of Sampled Data Systems

18. ### WBahn Moderator

Mar 31, 2012
23,550
7,207
I'm not forgeting that that the all 1's code is not the nominal FS value.What you and some others seem to forget is that, when given the value represented by the all 1's code and the value represented by the all 0's code, that the FS value is not simply the difference of the two.