cancel
Showing results for 
Search instead for 
Did you mean: 

Getting problem with interfacing STM32 L072Z and MPU6050. Always getting a constant value on I2C Read

Vijay1
Associate II

Hello Everyone,

I am trying to interface MPU6050 with STM32L072Z and while communicating to I2C line I am always getting constant value, bellow is the code

void Serialprint(char *msg)

{

uint16_t msg_len = strlen(msg);

HAL_UART_Transmit(&huart2, (uint8_t*)msg , msg_len, 10U);

}

long accelX, accelY, accelZ;

 HAL_I2C_Mem_Write(&hi2c2,0x68<<1,0x6B,sizeof(uint16_t),buffer,2,10U);

 HAL_Delay(100);

 HAL_I2C_Mem_Write(&hi2c2,0x68<<1,0x1B,sizeof(uint16_t),buffer,2,10U);

 HAL_Delay(100);

 HAL_I2C_Mem_Write(&hi2c2,0x68<<1,0x1C,sizeof(uint16_t),buffer,2,10U);

 HAL_Delay(100);

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

while((HAL_I2C_Mem_Read(&hi2c2, 0x68<<1,0x3B,sizeof(uint16_t),buff,6,10U)) != HAL_OK)

{

Serialprint("ERROR");

}

 accelX = buff[0]<<8|buff[1]; //Store first two bytes into accelX

 accelY = buff[2]<<8|buff[3]; //Store middle two bytes into accelY

 accelZ = buff[4]<<8|buff[5];

//  accelX = accelX /16384;

//  accelY = accelY /16384;

//  accelZ = accelZ /16384;

 sprintf(ax, "%d",accelX);

 sprintf(ay, "%d",accelY);

 sprintf(az, "%d",accelZ);

 Serialprint("AX : ");

 Serialprint(ax);

 Serialprint("\n\r");

 Serialprint("AY : ");

 Serialprint(ay);

 Serialprint("\n\r");

 Serialprint("AZ : ");

 Serialprint(az);

 Serialprint("\n\r");

}

0 REPLIES 0