Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] two complement problem

Status
Not open for further replies.

D_A_V_E

Member level 5
Joined
Nov 13, 2007
Messages
91
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,288
Activity points
1,736
Dear all,

I have a small problem here. I have a accelerometer, which read through I2C. the sensor data is 16bit two complement format and is store in 2 seperate registers(L and H). I am reading the data correctly and I would like to store the sensor data in an integer. So I am using the following routing.

acc_x_raw = (data.accelsH[1]) <<8) | data.accelsL[0];

But the result is not what I had expected. I think I am skipping a vital part here.....

Could anyone please tell me what I am doing wrong!!!

Thanks in advance

D
 

But the result is not what I had expected.

what is the result that you are getting and what is the result that you are expecting?

Could anyone please tell me what I am doing wrong!!!

articulate your problem concisely so others can better help you.
 

first thanks for your reply,

I am expecting values from about -32768 to +32768. then I would like to scal these values down to mG. But I am getting always negative values e.g. -65536.
 

Code:
acc_x_raw = (data.accelsH[1]) << | data.accelsL[0];

now, change it to the following and tell us what you get:

Code:
data.accelsH[1]=0x0f; data.accelsL[0]=0x55;
acc_x_raw = (data.accelsH[1]) << | data.accelsL[0];
 

doing:

acc_x_raw = ((data.accelsH[1]) << 8 ) | data.accelsL[0];

the value i am getting from this is 3925
 

great.

what that tells you is that the conversion from two bytes to a word is done correctly.

your problems are the result of data.accelsH[1] and data.accelsL[0] having the wrong values in them BEFORE the conversion to acc_x_raw took place.
 

Dear millwood,

thank you for your help.

Ok I am going to do this:

read from the sensor the High and low byte

acc_x_raw = ((highbyte) << 8 ) | lowbyte;

and then convert acc_x_raw to mG

yours,

D
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top