0
votes

I succesfully connected the MMA-7455L sensor and I am getting data from it. Although I have one question if someone can help me.

Can someone help me understand this piece of code? That I am using to get the data from.

i2cbuf[1] = 0x00;
HAL_I2C_Master_Receive(&hi2c1, 0x1D<<1, &i2cbuf[1], 6, 10);

ax = -(i2cbuf[1]<<8 | i2cbuf[2]);
ay = -(i2cbuf[3]<<8 | i2cbuf[4]);
az = -(i2cbuf[5]<<8 | i2cbuf[6]);

I am getting data and the outpit is in 8 bit. I understand that I am combining two 8 bit responses to make it a 16 bit response. But what I do not understand is the minus part.

Thank you in advance

1
with what type are ax,ay,az variables declared?nio
I declared them as a floatMarco V
@MarcoV are you sure it works as expected? It seems that sensor value is signed 10-bit (and here you always have a negative value, regardless the sign read from sensor). Also note that &i2cbuf[1] in C is implementation defined (check your compiler doc)Adriano Repetti
I think ax, ay, az should be int16_t and not float.Clonk
@MarcoV check the 10th bit, if it's set then value is negative (reset that bit and flip the sign but Google for details about this specific sensor...I'm going blindly here). About data type: you can certainly store it as float but unless you need some further processing then you're just wasting memory and CPU cycles because it's a signed 16 integer int16_t (and not even "full"). Unless you're abstracting these things (in case in future you'll use a different sensor) it's probably useless.Adriano Repetti

1 Answers

0
votes

My guess is that the values returned are 16 bit signed integers (int16_t) so you will have readings of -32767 through 0 to +32767. Whoever designed the board with the accelerometer example code you're using, wanted the values to read correctly in the boards normal orientation, so they have negated the results

eg: -(i2cbuf[1]<<8 | i2cbuf[2]); If both i2c values are 0xFF you get 0xFFFF, which is -1 (if type is int16)

Negate that and you get +1 which should indicate a positive acceleration to the application