Hello,
I am trying to interface a MPU6050 with a STM32G432RB with the I2C protocol. I have a main.c file for the main operations, a mpu6050.h for the macros and a mpu6050.c where I am putting the functions for reading from the MPU6050(presented below). The raw data that I am getting from the mpu6050 is not as expected. The values are not in 'g(s)' and it appears that the gyroscope values are being showed up in the variable for accelerometer and vice-versa. And I have checked the register map and datasheet to make sure that I am reading the correct registers, and it is currect as far as I checked. Would appreciate insights from anyone who has tried this before. Please have a look at my code:
Moderator edit: code tags added.
I am trying to interface a MPU6050 with a STM32G432RB with the I2C protocol. I have a main.c file for the main operations, a mpu6050.h for the macros and a mpu6050.c where I am putting the functions for reading from the MPU6050(presented below). The raw data that I am getting from the mpu6050 is not as expected. The values are not in 'g(s)' and it appears that the gyroscope values are being showed up in the variable for accelerometer and vice-versa. And I have checked the register map and datasheet to make sure that I am reading the correct registers, and it is currect as far as I checked. Would appreciate insights from anyone who has tried this before. Please have a look at my code:
C:
/*
* mpu6050.c
*
* Created on: Aug 7, 2023
* Author: azmain
*/
#include "mpu6050.h"
#include <main.h>
#include <stdio.h>
extern I2C_HandleTypeDef hi2c1;
extern int16_t x_acc;
extern int16_t y_acc;
extern int16_t z_acc;
extern int16_t x_gyro;
extern int16_t y_gyro;
extern int16_t z_gyro;
void mpu6050_init(){
HAL_StatusTypeDef ret = HAL_I2C_IsDeviceReady(&hi2c1, (MPU6050_ADDR<<1), 1,100);
if (ret==HAL_OK){
printf("The device is ready\n");
}
else{
printf("The device is not ready. Check configurations");
}
uint8_t temp_data = FS_GYRO_500;
ret = HAL_I2C_Mem_Write(&hi2c1, (MPU6050_ADDR<<1), REG_CONFIG_GYRO,1, &temp_data, 1, 200);
if (ret==HAL_OK){
printf("Configuring Gyroscope\n");
}
else{
printf("Failed to configure Gyroscope");
}
temp_data = FS_ACC_4G;
ret = HAL_I2C_Mem_Write(&hi2c1, (MPU6050_ADDR<<1), REG_CONFIG_ACC,1, &temp_data, 1, 200);
if (ret==HAL_OK){
printf("Configuring Accelerometer \n");
}
else{
printf("Failed to Configure Accelerometer \n");
}
temp_data = 0;
ret = HAL_I2C_Mem_Write(&hi2c1, (MPU6050_ADDR<<1), REG_USR_CTRL,1, &temp_data, 1, 200);
if (ret==HAL_OK){
printf("Exiting from Sleep Mode\n");
}
else{
printf("Failed to Exit from Sleep Mode \n");
}
}
void mpu6050_read(){
uint8_t data[6];
//int16_t x_acc;
HAL_I2C_Mem_Read(&hi2c1, (MPU6050_ADDR<<1)+1, REG_DATA,1, data, 6, 200);
x_acc = (((int16_t)data[0])<<8)+data[1];
//printf("x axis acceleration: %d \n",x_acc);
}
void mpu6050_read_gyro(){
uint8_t data[2];
//int16_t x_acc;
uint8_t read_reg[] = {GYRO_XOUT_H_REG,
GYRO_XOUT_H_REG+1,
GYRO_XOUT_H_REG+2,
GYRO_XOUT_H_REG+3,
GYRO_XOUT_H_REG+4,
GYRO_XOUT_H_REG+5};
HAL_I2C_Mem_Read(&hi2c1, (MPU6050_ADDR<<1)+1, GYRO_XOUT_H_REG,1, data, 2, 200);
x_gyro = (((int16_t)data[0])<<8)+data[1];
//printf("x axis acceleration: %d \n",x_gyro);
}
Attachments
-
9.1 KB Views: 15
-
2.9 KB Views: 3