cancel
Showing results for 
Search instead for 
Did you mean: 

How to use i2c functions with stm32f401RE?

dlee.0
Associate II

Hi, I'm using stm32f401RE and making a code with cubeMX.

So I set i2c3 and I was going to get some numbers from LSM303DLHC whic is accelo/gyro sensor.

Here's the codes what I set.

#define Slave_addr (0x1E<<1)

#define X_high 0x03

#define X_low 0x04

uint8_t x;

uint16_t x_full;

and below are the codes in the while loop.

while(1)

{

HAL_I2C_Mem_Read(&hi2c3,Slave_addr,X_high,I2C_MEMADD_SIZE_8BIT,&x,1,500);

   x_full=x<<8;

   HAL_I2C_Mem_Read(&hi2c3,Slave_addr,X_low,I2C_MEMADD_SIZE_8BIT,&x,1,500);

   x_full= x_full | x;

 message[0]=x_full/10000+ 0x30;

   message[1]=x_full%10000/1000 +0x30;

   message[2]=x_full%1000/100 + 0x30;

  message[3]=x_full%100/10 + 0x30;

  message[4]=x_full%10 + 0x30;

   HAL_UART_Transmit(&huart2, message, 7, 100);

  HAL_Delay(2000);

 }

I wanted to check x value through UART.

However I only get "00155" from sensor and doesn't change even I move my sensor.  So I concluded that I set it wrong or misunderstaded functions.

Can you guys check this??

1 REPLY 1
dlee.0
Associate II

​I just tested temparature with this sensor and I succeed in get temparature values. Its value goes up when I put my fingers on and it goes down when I put away my fingers. But still, x value isn't change at all