8 years, 1 month ago.

Read MMA8451

Hello, I was working on my FRDM KL25 with KDS. I want to try MBED online compiler. For that I have made a small program wich read a MMA8451 accelerometer (adress 0x1D) connected on PTE0 and PTE1. For your information, it's working fine with my KDS program. The program is very simple:

include "mbed.h" define REG_WHO_AM_I 0x0D

I2C i2c2(PTE0,PTE1); Serial pc(USBTX,USBRX);

int main() { char v; char cmd; cmd = REG_WHO_AM_I; pc.baud(115200); i2c2.write(0x1D, &cmd, 1 ); i2c2.read(0x1D, &v, 1 ); pc.printf("received=%d\n",v); }

I should received 0x1A and I received 0x4D Here follow the DataAnalyzer picture: /media/uploads/michastro/p4.jpg Have you an idea?

I have tested now as suggested Wim Huiskamp: i2c2.write( (0x1D << 1), &cmd, 1 ); i2c2.read( (0x1D << 1), &v, 1 ); It's a little bit better but not OK; the dataAnalyzer ggives that: /media/uploads/michastro/p5.jpg Now follow a dataAnalyzer of a program made with KDS and it's working! The code is: error=GI2C2_ReadByteAddress8(0x1D,CTRL_REG1,&valeur); /media/uploads/michastro/p6.jpg

YYEEEESSSSSSSSSSSSS That's working with: i2c2.write( (0x1D << 1), &cmd, 1, true); /media/uploads/michastro/p7.jpg

Thanks a lot!

1 Answer

8 years, 1 month ago.

mbed libs use the 8bit address format for I2C slave addresses. You provide 0x1D which is in the 7bit format. The result is that the lib overwrites the addressbit 0 and then sends the result, which is decoded by your logic analyser as 0x0E (instead of 0x1D or actually 0x1C). You also get a NotAck since the slave does not respond to the incorrect address.

Try shifting the address to the left by one bit:

i2c2.write( (0x1D << 1), &cmd, 1 );
i2c2.read( (0x1D << 1), &v, 1 );

Accepted Answer

See above the tests with this new code, it's not working properly.

posted by michel Meunier 19 Mar 2016

The slaveaddress is now correct and you get an ACK from the slave so thats working. The difference with the trace from the KDS example is that it uses a repeated start for the Read operation. Maybe the MMA8451 needs that. Try

i2c2.write( (0x1D << 1), &cmd, 1, true);
i2c2.read( (0x1D << 1), &v, 1 );
posted by Wim Huiskamp 19 Mar 2016