cancel
Showing results for 
Search instead for 
Did you mean: 

ADXL345 library development for STM32F407VG-DISC

testbenchmark
Associate II

Hi everyone, I am kinda new at Embedded programming and I wanna ask a question.

I am working on a library development of ADXL345 sensor and when I read the datasheet of this sensor here is the information that I got:

"An alternate I2C address of 0x53 (followed by the R/W bit) can be chosen by grounding the SDO/ALT ADDRESS pin (Pin 12). This translates to 0xA6 for a write and 0xA7 for a read."

 

This means I2C address of ADXL345 is = 0x53 HEX and "0101 0011" Binary.

after 1 bit left shift and write address is = 0xA6 HEX and "1010 0110" Binary.

after add 1 bit to the shift address is = 0xA7 HEX and "010 0110" Binary.

 

When I run this code block in STM32 : 

testbenchmark_0-1715498328928.png

I got the A6 write address instead of 0x53 I2C address. Do you know why ?

 

I got an opinion but I am not sure. When I read the brief of HAL_I2C_IsDeviceReady() function it says:

"@brief Checks if target device is ready for communication."

so its mean " I am trying to write something and checking for knowing can I write or not ?" so it returns the write address because it can only write something in write address right or not ?

 

Thanks.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Saket_Om
ST Employee

Hello @testbenchmark and welcome to the community !

 

The HAL_I2C_IsDeviceReady() function is indeed used to check if a device is ready for communication on the I2C bus. This function typically requires the I2C address in the format where the least significant bit (LSB) indicates whether it's a read (1) or write (0) operation. Since the function is checking for device readiness to communicate, it expects the write address.

Here's a breakdown of why you're seeing the address 0xA6:

  • The 7-bit I2C address for the ADXL345 is 0x53 when the SDO/ALT ADDRESS pin is grounded.
  • In binary, this is 0101 0011.
  • When you shift this address to the left by one bit to make room for the read/write bit, you get 1010 0110 in binary, which is 0xA6 in hexadecimal.
  • The HAL_I2C_IsDeviceReady() function expects the address in this 8-bit format, where the last bit is the read/write bit. Since you're checking if the device is ready to communicate (which implies writing to it), you use the write address 0xA6.

The code you've written scans through all possible addresses and uses the HAL_I2C_IsDeviceReady() function to check if a device is ready at that address. When it finds a device ready at address 0xA6, it means that the ADXL345 is responding to its write address.

Note that when using the HAL_I2C_IsDeviceReady() function, you must shift the 7-bit address of the device, as specified in the driver, one bit to the left before passing it as a parameter to the function.

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

View solution in original post

2 REPLIES 2
Saket_Om
ST Employee

Hello @testbenchmark and welcome to the community !

 

The HAL_I2C_IsDeviceReady() function is indeed used to check if a device is ready for communication on the I2C bus. This function typically requires the I2C address in the format where the least significant bit (LSB) indicates whether it's a read (1) or write (0) operation. Since the function is checking for device readiness to communicate, it expects the write address.

Here's a breakdown of why you're seeing the address 0xA6:

  • The 7-bit I2C address for the ADXL345 is 0x53 when the SDO/ALT ADDRESS pin is grounded.
  • In binary, this is 0101 0011.
  • When you shift this address to the left by one bit to make room for the read/write bit, you get 1010 0110 in binary, which is 0xA6 in hexadecimal.
  • The HAL_I2C_IsDeviceReady() function expects the address in this 8-bit format, where the last bit is the read/write bit. Since you're checking if the device is ready to communicate (which implies writing to it), you use the write address 0xA6.

The code you've written scans through all possible addresses and uses the HAL_I2C_IsDeviceReady() function to check if a device is ready at that address. When it finds a device ready at address 0xA6, it means that the ADXL345 is responding to its write address.

Note that when using the HAL_I2C_IsDeviceReady() function, you must shift the 7-bit address of the device, as specified in the driver, one bit to the left before passing it as a parameter to the function.

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

Thank you so much for your help OSAKE.1 .