Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Help with 24 bit ADC - LTC2499


  • Please log in to reply
7 replies to this topic

#1 Ryan Mick

Ryan Mick

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationSacramento, CA

Posted 13 April 2012 - 04:38 PM

I have recently ordered 2 LTC2499 24 bit ADC's. I should receive them shortly but I need help understanding how to convert the output, binary manipulation is not my strong point. On page 16 of the datasheet it gives the output format of the reading as 4 bytes (total of 32 bit numbered 31 to 0). Bit 31 is the sign bit and I know how to find out if its value (byte1 & 0x80 == sign). Bit 30 is described as the MSB but is not part of the 24 bit result, which means bits 29 to bit 6 are the 24 bit result and bits 5 to 0 are ignored. So after I get the sign how do I properly clear the up bits that are not needed and then properly read the value. The other catch is that the sheet says the result is in binary two's compliment, which just confused me even more when I looked up what that meant. Any help would be greatly appreciated in learning how to convert this value.

Thanks,
Ryan

#2 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 13 April 2012 - 04:53 PM

Ryan, this could help?
http://forums.netdui...c-ltc2400-help/
Biggest fault of Netduino? It runs by electricity.

#3 Ryan Mick

Ryan Mick

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationSacramento, CA

Posted 13 April 2012 - 05:27 PM

Thanks Mario, I had looked at that post several times before I posted but the usable bit pattern is different for that chip and not in two's compliment format from what I can tell. And they divide the final value by 16 to clear the last 4 bits (this chip would need clear the last 6), which I totally do not understand how that would clear the last 4 bits. Like I said, binary manipulation is not my strong point and I haven't found anything that makes it easier for me to understand.

#4 Geancarlo2

Geancarlo2

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 13 April 2012 - 08:16 PM

You have to take into account the bit 30 to work with the value as a "25 bit number in two's compliment". I just educated myself about this on this page http://www.cs.cornel...4/twoscomp.html
The following piece of code is a sketch of how to convert the value, or at least what I understood.


uint value=fsbbbbbbbbbbbbbbbbbbbbbbbbxxxxx(binary);
int signedvalue;
float vref=10.0;
float fullscale=vref/2.0;
float vin;

//mask 0xC0000000=11000000000000000000000000000000
if(value ==0xC0000000)
{
	//overflow condition Vin>=5V
	signedvalue=0xFFFFFF;
}
//mask 0x3FFFFFFF=00111111111111111111111111111111
else if( value == 0x3FFFFFFF)
{
	//underflow condition Vin<-5V
	signedvalue=-0xFFFFFF;
}
else
{
	//mask 0x7FFFFFE0=01111111111111111111111111100000
	value=(value & 0x7FFFFFE0)>>5;
	if(value==0)
	{
		signedvalue=0;
	}
	//mask  0xFFFFFF=00000000111111111111111111111111
	else if(value<=0xFFFFFF)
	{
		signedvalue=value;
	}
	//(value>0xFFFFFF) sign bit is 1
	else
	{
		//flipping affects bits 25~31 too
		// so remove that; AND 0x1FFFFFF
		signedvalue=-((!value & 0x1FFFFFF)+1);
	}
}
vin=((float)signedvalue)*((float)0xFFFFFF)*fullscale;


edit:fixed "&&"s->"&"

Edited by Geancarlo2, 13 April 2012 - 10:40 PM.


#5 Ryan Mick

Ryan Mick

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationSacramento, CA

Posted 13 April 2012 - 09:49 PM

Thanks Geancario2, I will look at the link. But remember the value is being received as 4 individual bytes, so it has to be pieced together first. I just received the chips today, now I need to assemble them and look at the values under a known analog value to see the resulting bits.

#6 Geancarlo2

Geancarlo2

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts

Posted 13 April 2012 - 10:46 PM

No problem uint value=byte1 || (byte2<<8) || (byte3<<16) || (byte4<<24); Note that byte4 is the most significant byte and first read so you can determine an over/under flow condition before completing a full 4 bytes transmission. Good luck.

#7 Ryan Mick

Ryan Mick

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationSacramento, CA

Posted 29 April 2012 - 09:00 PM

Ok, I have the ADC and I am able to get some values out of it. I have a constant 1.5v going to channel 0 with a 5v ref voltage. Something about the way I am trying to figure out this 2's complement thing isn't working though. There returned values are:

byte0: 10101000
byte1: 10000100
byte2: 11101110
byte3: 01101101

The last bit of byte0 determines if it is a positive #. According to the datasheet a 1 is positive. The bit to it's right is the msb but not part of the value it is a status bit. The 24 bits following that are the value of the channel, the last 6 bits are discarded. As I have said before, binary manipulation is not my strong point so if someone can see what I am missing I would appreciate any feedback.

byte0 byte1 byte2 byte3
10101000 10000100 11101110 01101101 <--original values
00101000 10000100 11101110 01000000 <--modified values

            Int32 lReturn = 0;

            //Is this a positive #
            bool sig = false;
            if ((buffer[0] & 0x20) == 0) {
                sig = true;
            }

            //Clear bits 31 and 30
            buffer[0] &= 0x1F;
            lReturn |= buffer[0];

            lReturn <<= 8;

            lReturn |= buffer[1];
            lReturn <<= 8;

            lReturn |= buffer[2];
            lReturn <<= 8;

            //Clear bits 5-0
            buffer[3] &= 0xC0;
            lReturn |= buffer[3];

            //2's compliment so flip the bits
            lReturn = ~lReturn;
            //Add 1
            lReturn += 0x01;


#8 Ryan Mick

Ryan Mick

    Advanced Member

  • Members
  • PipPipPip
  • 36 posts
  • LocationSacramento, CA

Posted 01 May 2012 - 03:20 AM

Ok, a little closer I think. I have this wired to a trim pot and when I adjust it I get some unexpected values.

            byte byte0 = buffer[0];
            byte byte1 = buffer[1];
            byte byte2 = buffer[2];
            byte byte3 = buffer[3];

            int i = 0;

            bool sig = false;
            if ((byte0 & 0x20) == 0) {
                sig = true;
            }

            byte0 = (byte)~byte0;
            byte0 &= 0x3F;
            Debug.Print("i is now");
            i = byte0 << 24;

            byte1 = (byte)~byte1;
            i |= byte1 << 16;

            byte2 = (byte)~byte2;
            i |= byte2 << 8;

            byte3 = (byte)~byte3;
            byte3 &= 0xC0;
            i |= byte3 ;

            i = (i >> 6)+1;

            if (sig) {
                i = i * -1;
            }

            lReturn = i;





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.