cancel
Showing results for 
Search instead for 
Did you mean: 

Reading from Accelerometer via i2c on STM32F3 Discovery

Ala1980
Associate II

Can anyone tell me the difference between those functions:

HAL_I2C_Master_Receive

HAL_I2C_Mem_Read

The problem with HAL_I2C_Master_Receive is that it accepts too few parameters (hi2c, DevAddress, pData, Size, Timeout).

I don't know how to tell the memory address of the register in the slave to be read!!

So, in my case I am trying to read data from LSM303DLHC whose DevAddress is 0X33 and the acceleration data are to read from the following addresses: 0X28 -> 2Dh (i.e. 6 bytes).

Thanks in advance for any help or if there is a ready example project.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

You want HAL_I2C_Mem_Read, which with both send the memory address to the slave and read the data the slave sends. HAL_I2C_Master_Receive only receives data from a slave.

One of several examples available in the Cube repository:

https://github.com/STMicroelectronics/STM32CubeF3/blob/8fa3aadf0255818f0ca72ba6a5a6731ef8c585fb/Drivers/BSP/STM32373C_EVAL/stm32373c_eval.c

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

5 REPLIES 5
TDK
Guru

You want HAL_I2C_Mem_Read, which with both send the memory address to the slave and read the data the slave sends. HAL_I2C_Master_Receive only receives data from a slave.

One of several examples available in the Cube repository:

https://github.com/STMicroelectronics/STM32CubeF3/blob/8fa3aadf0255818f0ca72ba6a5a6731ef8c585fb/Drivers/BSP/STM32373C_EVAL/stm32373c_eval.c

If you feel a post has answered your question, please click "Accept as Solution".
Ala1980
Associate II

Thanks for the reply, but I dont know why I cant read the value of a register using the HAL_I2C_Mem_Read function.

So I am using the function HAL_I2C_Master_Transmit to write into a specific register and then I try to read the value using the HAL_I2C_Mem_Read function but unfortunately I get a different value.

Here is a link of my main file:

https://github.com/Ala1980/LSM303DLHC

Any help please?

TDK
Guru

You probably want to use HAL_I2C_Mem_Write to write to a memory address.

If you feel a post has answered your question, please click "Accept as Solution".
Ala1980
Associate II

Oh, I found my mistake.

I was writing to the correct register 0x20, but reading from another register 20.

So writing to 32 decimal and reading from 20 decimal.

Ala1980
Associate II

Oh, I found my mistake.

I was writing to the correct register 0x20, but reading from another register 20.

So writing to 32 decimal and reading from 20 decimal.