Reading Signed Data in Two Parts

Status
Not open for further replies.

kingneb

Newbie level 3
Joined
Sep 7, 2010
Messages
3
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,306
Hello,

I am using C on an ARM processor and am attempting to properly read parallel data off of a TI ADS7824 analog to digital converter. I would use serial but the application calls for parallel. Anyway, if you look at the datasheet,

https://www.ti.com/lit/ds/symlink/ads7824.pdf

you will see that, according to the state of the byte input, either the first four bits or the remaining eight are placed on the output pins. That data to my knowledge is signed, since the unit accepts +/- 10 volts on its analog inputs. How do you join, using C variables, the contents of a signed 12-bit quantity (in this case four and eight bits), broken into two parts? It is easy with unsigned data. I just do this:

Code:
short byteA = 0x00;
short byteB = 0x00;

byteB <<= 4
byteA |= ByteB
 

you test the sign bit of the 12 bit data and sign extend e.g. assuming twos complement data
(msb and lsb contain the most and least significant bytes respectively)
Code:
    if(msb & 8) msb |= 0xf0;    // if negative sign extend to left nibble
    int data = (msb << 8) | lsb;  // create a 16 bit int
if data is a 32 bit int you need to sign extend into the top 16 bits
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…