I am using MPU6500 accelerometer in I2C mode. Datasheet explains the reading sequence on page 33. enter image description here

It may seem easy for someone who is an expert but it is my first time using the I2C interfaced device. I have written a function but It doesn't read register values like WHO_AM_I etc.

    uint8_t Accelerometer_Read(uint8_t reg)
    uint8_t read;

    HAL_I2C_Master_Transmit(accelerometerhandle, 0x68|0x00, &reg, 1, 100);
    HAL_I2C_Master_Receive(accelerometerhandle, 0x68|0x01, &read, 1, 100);

    return read;

This is the function that I wrote, I have connected AD0 to GND so that I2C address is 0x68. How can I implement this sequence?

  • 1
    \$\begingroup\$ I think the problem here is that your master is likely issuing a stop condition after the first transmit, but the device is expecting a "repeated start" condition. Do you have a HAL_I2C function that supports repeated start conditions? \$\endgroup\$ – Justin Aug 21 '20 at 14:30
  • \$\begingroup\$ You mean there should be another S at the end of writing the register. Is it possible by sending dummy value like 0x00? \$\endgroup\$ – emre iris Aug 21 '20 at 14:37
  • 1
    \$\begingroup\$ Use HAL_I2C_Mem_Read() instead. That will perform the correct sequence of Start-devAddr-regAddr-reStart-readData-Stop all for you. \$\endgroup\$ – brhans Aug 21 '20 at 14:39
  • 1
    \$\begingroup\$ The alternative would b to use interrupts. Start with a HAL_I2C_Master_Seq_Transmit_IT(), then in the TxCplt callback do a HAL_I2C_Master_Seq_Receive_IT(). \$\endgroup\$ – brhans Aug 21 '20 at 14:44

Simply call the HAL_I2C_Mem_Read, that will do the standard "read from register address" sequence.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.