Skip to main content
Ala1980
Associate III
July 27, 2020
Solved

Reading from Accelerometer via i2c on STM32F3 Discovery

  • July 27, 2020
  • 5 replies
  • 2856 views

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.

This topic has been closed for replies.
Best answer by TDK

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

5 replies

TDK
TDKBest answer
July 27, 2020

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
Ala1980Author
Associate III
August 14, 2020

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
August 14, 2020

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
Ala1980Author
Associate III
August 15, 2020

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
Ala1980Author
Associate III
August 15, 2020

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.