# Arduino Uno & 24 Bit ADC - 2's Compliment to Binary conversion

Hello.
I have an Arduino uno reading output values from a 24 bit adc ADS1231 I am able to get the data in 2’s compliment format as expected (datasheet attached page 12), but the problem is I would like it to be in Decimal format with a positive range as an example (0 to 20000) instead of (800000h to 7FFFFFh) and or at least initially a format like (-10000 to + 9999). Please help with how I could do this conversion. I know that 2’s compliment to Decimal this is what I have to do:

first check if the number is negative or positive by looking at the sign bit. If it is positive, simply convert it to decimal. If it is negative, make it positive by inverting the bits and adding one. Then, convert the result to decimal. The negative of this number is the value of the original binary. But how do I actually do this in an arduino platform c. To read the adc values at the moment I am using:

{
//24–>21 bits
clock ();
clock ();
clock ();
… etc

Do you know the pulseIn() function of the Arduino environment?

The Arduino stores negative numbers in two's compliments too, so you can read in the value, extend to 32bit and convert it by pointer swapping or a union.

X = bit24 & adcvalue; // zero-ing the rest of the adcvalue bits except the sign bit.
if(X==bit24) // if sign bit is = 1
{
}
else // sign bit is = 0
{
}

I have used this code to convert the 24 bit adc value to 32 bits which seems to work fine now if I press my sensor for positive push: (zero+offset) to 0x7FFFFF and (0+offset) to 0xFF8000000 for negative push.
24 bits for positive and 32 bit for negative which seems to be working as expected.
Now I just need to know how to convert this number to something more meaningful like instead of going from negative full scale 0xFF8000000 to 0 midpoint to 0x7FFFFF positive full scale. I would like to have it like 0 to X where 0 is equivalent to 0xFF8000000, X/2 is equivalent to midpoint and X is the postive full scale. How do I achieve this code?
ps. No I do not know about pulseln sorry

Is this what you're trying to do?

``````count=count^0x800000;         // convert 24 bit ADC two's comp output as follows

// two's comp min =  0x800000 = -8388608; converted = 0
// two's comp zero = 0x000000 =        0; converted = 2^23 = 8,388,608
// two's comp max =  0x7FFFFF = +8388607; converted = (2^24)-1 = 16,777,215
``````

PS: your post title is confusing... it says you want convert two's complement to BINARY. It seems you actually want to convert and shift two's comp to a positive DECIMAL. But your post is confusing as well. So who knows.......

Once you have sign-extended the 24-bit number to a 32-bit number, its range is from -8388608 to +8388607. Simply adding 8388608 to it converts it to the range from zero to 16777215.

Pete

Thank you all.
Problem solved!