Author Topic: stm32 hal library mpu6050  (Read 5154 times)

0 Members and 1 Guest are viewing this topic.

Offline huseyinkTopic starter

  • Contributor
  • Posts: 11
stm32 hal library mpu6050
« on: July 04, 2018, 06:52:44 am »
I try to read gyro data from MPU-6050. I configured the gyro config register.

Code: [Select]
  TxData[1] = 0xF0;
  HAL_I2C_Mem_Write(&hi2c1,slaveAddr,GYRO_CONFIG,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData[1],1,100);
  HAL_Delay(50);

 /* Gyro_Config settings...  XG_ST --> 1 , YG_ST --> 1 , ZG_ST --> 1 , FS-SEL 10 (1000 dps)  */

Code: [Select]
  HAL_I2C_Mem_Read(&hi2c1,slaveAddr|0x01,GYRO_X_OUT_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[0],1,10);
  HAL_Delay(100);
  HAL_I2C_Mem_Read(&hi2c1,slaveAddr|0x01,GYRO_X_OUT_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[1],1,10);

/* Reading gyro X data. */

Is there any missing code for config MPU-6050? I cant read any data.
 

Offline huseyinkTopic starter

  • Contributor
  • Posts: 11
Re: stm32 hal library mpu6050
« Reply #1 on: July 04, 2018, 08:27:17 am »
I try to read gyro data from MPU-6050. I configured the gyro config register.

Code: [Select]
  TxData[1] = 0xF0;
  HAL_I2C_Mem_Write(&hi2c1,slaveAddr,GYRO_CONFIG,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData[1],1,100);
  HAL_Delay(50);

 /* Gyro_Config settings...  XG_ST --> 1 , YG_ST --> 1 , ZG_ST --> 1 , FS-SEL 10 (1000 dps)  */

Code: [Select]
  HAL_I2C_Mem_Read(&hi2c1,slaveAddr|0x01,GYRO_X_OUT_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[0],1,10);
  HAL_Delay(100);
  HAL_I2C_Mem_Read(&hi2c1,slaveAddr|0x01,GYRO_X_OUT_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[1],1,10);

/* Reading gyro X data. */

Is there any missing code for config MPU-6050? I cant read any data.

I have done !!

Code: [Select]
void MPU_6050_Init()
{
TxData[0] = 0x81;
HAL_I2C_Mem_Write(&hi2c1,slaveAddr,PWR_MGMT,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData[0],1,10);
HAL_Delay(50);

TxData[0] = 0x07;
HAL_I2C_Mem_Write(&hi2c1,slaveAddr,SMPLRT_DIV,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData[0],1,10);
HAL_Delay(50);

TxData[0] = 0x01;
HAL_I2C_Mem_Write(&hi2c1,slaveAddr,PWR_MGMT,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData[0],1,10);
HAL_Delay(50);

TxData[0] = 0x00;
HAL_I2C_Mem_Write(&hi2c1,slaveAddr,CONFIG_REG,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData[0],1,10);
HAL_Delay(50);

TxData[0] = 0xF1;
HAL_I2C_Mem_Write(&hi2c1,slaveAddr,GYRO_CONFIG,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData[0],1,10);
HAL_Delay(50);
}



Code: [Select]
while (1)
  {

HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_X_OUT_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[0],1,10);
HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_X_OUT_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[1],1,10);

gyro_X = (RxData[1]<<8 | RxData[0])*0.060975F;

HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_Y_OUT_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[2],1,10);
HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_Y_OUT_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[3],1,10);

gyro_Y = (RxData[3]<<8 | RxData[2])*0.060975F;

HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_Z_OUT_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[4],1,10);
HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_Z_OUT_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData[5],1,10);

gyro_Z = (RxData[5]<<8 | RxData[4])*0.060975F;

HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,TEMP_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Temp[0],1,10);
HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,TEMP_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Temp[1],1,10);

temp = ((Temp[1]<<8 | Temp[0]) / 340.0F)+ 36.53;
 
  }

}

But I have a question. My temperature value bounce sometimes 36 'C.. How can I fix it? Which config settings?
 

Offline mkdesign

  • Newbie
  • Posts: 2
  • Country: us
Re: stm32 hal library mpu6050
« Reply #2 on: July 04, 2018, 08:04:25 pm »
Greetings!

A few things:

1) Your high & low bytes could possibly be from different samples - the user register is updated when the serial line is idle. This might be a problem for you at temperatures around 36.53 C and gyro readings around 0. You need to read both bytes in one go ("burst mode"). In fact, why not just read the whole block starting at 0x3B in one go so you are getting all of the values in one sample?

2) Check the values of the two bytes you're reading vs. the temp you're calculating.

3) Check the return value of the HAL_... functions.

REF:

Datasheet: https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf

Registers: https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf
« Last Edit: July 04, 2018, 08:06:48 pm by mkdesign »
 

Offline huseyinkTopic starter

  • Contributor
  • Posts: 11
Re: stm32 hal library mpu6050
« Reply #3 on: July 05, 2018, 10:26:50 am »
Greetings!

A few things:

1) Your high & low bytes could possibly be from different samples - the user register is updated when the serial line is idle. This might be a problem for you at temperatures around 36.53 C and gyro readings around 0. You need to read both bytes in one go ("burst mode"). In fact, why not just read the whole block starting at 0x3B in one go so you are getting all of the values in one sample?

2) Check the values of the two bytes you're reading vs. the temp you're calculating.

3) Check the return value of the HAL_... functions.

REF:

Datasheet: https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Datasheet1.pdf

Registers: https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf

I have done it. Probably as you said , it is about not reading all of the register at the same time.. Thank you for help.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->