stm32 hal library mpu6050 - Page 1
Forum section: electronics / general

author: huseyink
author_rank: Contributor
timestamp: July 04, 2018, 06:52:44 am
content: I try to read gyro data from MPU-6050. I configured the gyro config register.Code:TxData = 0xF0;HAL_I2C_Mem_Write(&hi2c1,slaveAddr,GYRO_CONFIG,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData,1,100);HAL_Delay(50);/* Gyro_Config settings... XG_ST --> 1 , YG_ST --> 1 , ZG_ST --> 1 , FS-SEL 10 (1000 dps) */Code:HAL_I2C_Mem_Read(&hi2c1,slaveAddr|0x01,GYRO_X_OUT_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData,1,10);HAL_Delay(100);HAL_I2C_Mem_Read(&hi2c1,slaveAddr|0x01,GYRO_X_OUT_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData,1,10);/* Reading gyro X data. */Is there any missing code for config MPU-6050? I cant read any data.

author: huseyink
author_rank: Contributor
timestamp: July 04, 2018, 08:27:17 am
content: Quote from: huseyink on July 04, 2018, 06:52:44 amI have done !!Code:void MPU_6050_Init(){TxData = 0x81;HAL_I2C_Mem_Write(&hi2c1,slaveAddr,PWR_MGMT,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData,1,10);HAL_Delay(50);TxData = 0x07;HAL_I2C_Mem_Write(&hi2c1,slaveAddr,SMPLRT_DIV,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData,1,10);HAL_Delay(50);TxData = 0x01;HAL_I2C_Mem_Write(&hi2c1,slaveAddr,PWR_MGMT,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData,1,10);HAL_Delay(50);TxData = 0x00;HAL_I2C_Mem_Write(&hi2c1,slaveAddr,CONFIG_REG,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData,1,10);HAL_Delay(50);TxData = 0xF1;HAL_I2C_Mem_Write(&hi2c1,slaveAddr,GYRO_CONFIG,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&TxData,1,10);HAL_Delay(50);}Code:while (1){HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_X_OUT_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData,1,10);HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_X_OUT_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData,1,10);gyro_X = (RxData<<8 | RxData)*0.060975F;HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_Y_OUT_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData,1,10);HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_Y_OUT_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData,1,10);gyro_Y = (RxData<<8 | RxData)*0.060975F;HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_Z_OUT_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData,1,10);HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,GYRO_Z_OUT_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&RxData,1,10);gyro_Z = (RxData<<8 | RxData)*0.060975F;HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,TEMP_L,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Temp,1,10);HAL_I2C_Mem_Read(&hi2c1,slaveAddr | 0x01,TEMP_H,I2C_MEMADD_SIZE_8BIT,(uint8_t*)&Temp,1,10);temp = ((Temp<<8 | Temp) / 340.0F)+ 36.53;}}But I have a question. My temperature value bounce sometimes 36 'C.. How can I fix it? Which config settings?

author: mkdesign
author_rank: Newbie
timestamp: July 04, 2018, 08:04:25 pm
content: 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.pdfRegisters:https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf

author: huseyink
author_rank: Contributor
timestamp: July 05, 2018, 10:26:50 am
content: Quote from: mkdesign on July 04, 2018, 08:04:25 pmI have done it. Probably as you said , it is about not reading all of the register at the same time.. Thank you for help.