CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

ADXL345: Convert 16-bit Twos Complement to 10-bit Signed Int

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jcal54321



Joined: 06 Mar 2009
Posts: 10
Location: Salinas, CA

View user's profile Send private message

ADXL345: Convert 16-bit Twos Complement to 10-bit Signed Int
PostPosted: Sat Jun 16, 2012 5:40 am     Reply with quote

Hi All:
I was hoping I could get some help converting data bytes from the ADXL345 accelerometer that I am reading via I2C. The acceleration data is formatted into two bytes, say axDataHi and axDataLo, which are concatenated to form a 16-bit twos complement value. The actual acceleration data is a 10-bit value that is right justified with sign extension. If I am clear on how this works, the sign extension means that the leading bits of the axDataHi are padded with 6 bits, either 1 or 0, depending on the sign of the data.

Here is what I have tried, but I am not sure if this gives me the right result. accelIntArray[] has the six bytes for all three axes after a multi-byte read is accomplished. accelIntArray[0,2,4] is the low byte, and accelIntArray[1,3,5] is the high byte.

Code:

aX = ((int16) (accelIntArray[0] | accelIntArray[1]<<8));
aY = ((int16) (accelIntArray[2] | accelIntArray[3]<<8));
aZ = ((int16) (accelIntArray[4] | accelIntArray[5]<<8));


I have read through some of the forums and Wikipedia dealing with twos complement. I am still not really clear on what I need to do to convert the two bytes into a signed 10-bit integer. Any help would be greatly appreciated! Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Sun Jun 17, 2012 1:05 am     Reply with quote

Code:

//First thing, aX, to aZ, need to be declared as _signed_ int16
signed int16 aX,aY,aZ;

aX=make16(accelIntArray[1],accelIntArray[0]);
aY=make16(accelIntArray[3],accelIntArray[2]);
aZ=make16(accelIntArray[5],accelIntArray[4]);

//CCS, has the 'make16' function, that allows you to put two bytes together
//using single byte moves, instead of having to convert to int16 first
//and rotate.

printf("%4.3fg in X\n\r",aX/256.0);
//Just display the X axis in g as a demo - assuming unit is in +/-2g mode



Best Wishes
jcal54321



Joined: 06 Mar 2009
Posts: 10
Location: Salinas, CA

View user's profile Send private message

PostPosted: Mon Jun 18, 2012 6:12 pm     Reply with quote

Thank you for the good advice.

V/R James C.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group