Electronic – ADC value conversion

adccconversion

I am getting full scale ADC value from 0 to 16348. I am trying to convert it to 4-20mA and then to 0-25 bar pressure, as I am ultimately measuring pressure.

I am using the y = mx+c equation but failing miserably. Can any one please guide so that I can learn something.

I am trying something like

4 = m*0 +c
c = 4.

20 = m*16348 +4.

m = 0.0009789.

My controller is not supporting floating points…hence it's creating trouble for me. Can anyone help me out>?

Best Answer

Since your microcontroller doesn't support floating point, then you need to scale the numbers up and then divide back down. To do so, you will need to use long (32-bit) arithmetic.

For the ADC, 4 ma = 0 and 20 ma = 16384. Therefore the difference 16 ma is also represented by 16384.

If we take the full scale reading of the bar graph, 25, and divide by 16384, we get: 0.001526. But we can't use that value directly since it is floating point. So instead we take one million, and do the same thing. 1000000 / 16384 = 61.03 Now we have a number close enough to an integer which we can work with.

If we know take the reading from the ADC, multiply by 61, and divide by 40000 (which is 1000000 / 25), then we will have a number in the range 0-25. To try this out, if we take a value equal to half the range (16384/2), then for 8192 * 61 / 40000 we get 12.49 or half of 25 (but since its integer division, it will be rounded down to 12)

If, for some other application, you wanted an integer value in scaled hundreds (i.e. multiplied by 100), you could divide by 400 instead of 40000 and get 1249 (representing 12.49).

So the code is something like this (I've used casting to make sure all the necessary calculation are done as longs)

#define CONSTANT1 = (1000000L / 16384L)       // 61    
#define CONSTANT2 = (1000000L / 25L)          // 40000

unsigned short ADC_value;
unsigned short bar_value;

ADC_value = get_ADC();
bar_value = (unsigned char)(((unsigned long)ADC_Value * CONSTANT1) / CONSTANT2);