Hi !
I Can't communicate with FXLS8471Q via i2c. I want to read WHO_I_AM register but device don't respond.
After read who value is 255. Can You show me a example code how to read it ?
#define FXLS8471Q_SLAVE_ADDR 0x1E
#define FXLS8471Q_WHOAMI 0x0D
void FXLS_init(void) {
uint8_t who;
Soft_I2C1_send_address(FXLS8471Q_SLAVE_ADDR);
Soft_I2C1_send_data(FXLS8471Q_WHOAMI, END);
Soft_I2C1_send_address(FXLS8471Q_SLAVE_ADDR);
Soft_I2C1_send_data(0x00, END);
who = Soft_I2C1_read_data(END);
}
Solved! Go to Solution.
Hi Piotr,
Here is an example code to read the WHO_AM_I register (0x0D) using I2C when SA0 = SA1 = 0.
//FXLS8471Q 7-bit I2C address
#define FXLS8471Q_I2C_ADDRESS 0x1E // SA0 pin = SA1 = 0 -> 7-bit I2C address is 0x1E
who_am_i = I2C_ReadRegister(FXLS8471Q_I2C_ADDRESS, 0x0D);
unsigned char I2C_ReadRegister(unsigned char u8SlaveAddress, unsigned char u8RegisterAddress)
{
unsigned char result;
I2C_Start();
I2C0_D = u8SlaveAddress << 1; /* Send I2C device address with W/R bit = 0 */
I2C_Wait();
I2C0_D = u8RegisterAddress; /* Send register address */
I2C_Wait();
I2C_RepeatedStart();
I2C0_D = (u8SlaveAddress << 1) | 0x01; /* Send I2C device address this time with W/R bit = 1 */
I2C_Wait();
I2C_EnterRxMode();
I2C_DisableAck();
result = I2C0_D;
I2C_Wait();
I2C_Stop();
result = I2C0_D;
Pause(50);
return result;
}
A copy from the data sheet:
I hope it helps.
Regards,
Tomas
PS: If my answer helps to solve your question, please mark it as "Correct". Thank you.
Hi Piotr,
Here is an example code to read the WHO_AM_I register (0x0D) using I2C when SA0 = SA1 = 0.
//FXLS8471Q 7-bit I2C address
#define FXLS8471Q_I2C_ADDRESS 0x1E // SA0 pin = SA1 = 0 -> 7-bit I2C address is 0x1E
who_am_i = I2C_ReadRegister(FXLS8471Q_I2C_ADDRESS, 0x0D);
unsigned char I2C_ReadRegister(unsigned char u8SlaveAddress, unsigned char u8RegisterAddress)
{
unsigned char result;
I2C_Start();
I2C0_D = u8SlaveAddress << 1; /* Send I2C device address with W/R bit = 0 */
I2C_Wait();
I2C0_D = u8RegisterAddress; /* Send register address */
I2C_Wait();
I2C_RepeatedStart();
I2C0_D = (u8SlaveAddress << 1) | 0x01; /* Send I2C device address this time with W/R bit = 1 */
I2C_Wait();
I2C_EnterRxMode();
I2C_DisableAck();
result = I2C0_D;
I2C_Wait();
I2C_Stop();
result = I2C0_D;
Pause(50);
return result;
}
A copy from the data sheet:
I hope it helps.
Regards,
Tomas
PS: If my answer helps to solve your question, please mark it as "Correct". Thank you.