Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 8 months 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: 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: Now follow a dataAnalyzer of a program made with KDS and it's working! The code is: error=GI2C2_ReadByteAddress8(0x1D,CTRL_REG1,&valeur);
YYEEEESSSSSSSSSSSSS That's working with: i2c2.write( (0x1D << 1), &cmd, 1, true);
Thanks a lot!
1 Answer
8 years, 8 months 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 );
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 );