STM32H750XB_RT-THREAD/44-MPU6050(包含程序+上位机)/程序/4.STM32-MPU6050_DMP测试例程/User/mpu6050/mpu6050.c
2025-07-21 14:34:29 +08:00

126 lines
2.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "./mpu6050/mpu6050.h"
#include "./usart/bsp_debug_usart.h"
#include "./i2c/i2c.h"
#define MPU_ERROR I2C_ERROR
#define MPU_INFO I2C_INFO
/**
* @brief 写数据到MPU6050寄存器
* @param reg_add:寄存器地址
* @param reg_data:要写入的数据
* @retval
*/
void MPU6050_WriteReg(uint8_t reg_add,uint8_t reg_dat)
{
Sensors_I2C_WriteRegister(MPU6050_ADDRESS,reg_add,1,&reg_dat);
}
/**
* @brief 从MPU6050寄存器读取数据
* @param reg_add:寄存器地址
* @param Read存储数据的缓冲区
* @param num要读取的数据量
* @retval
*/
void MPU6050_ReadData(uint8_t reg_add,unsigned char* Read,uint8_t num)
{
Sensors_I2C_ReadRegister(MPU6050_ADDRESS,reg_add,num,Read);
}
/**
* @brief 初始化MPU6050芯片
* @param
* @retval
*/
void MPU6050_Init(void)
{
//在初始化之前要延时一段时间,若没有延时,则断电后再上电数据可能会出错
Delay(100);
MPU6050_WriteReg(MPU6050_RA_PWR_MGMT_1, 0x00); //解除休眠状态
MPU6050_WriteReg(MPU6050_RA_SMPLRT_DIV , 0x07); //陀螺仪采样率
MPU6050_WriteReg(MPU6050_RA_CONFIG , 0x06);
MPU6050_WriteReg(MPU6050_RA_ACCEL_CONFIG , 0x01); //配置加速度传感器工作在16G模式
MPU6050_WriteReg(MPU6050_RA_GYRO_CONFIG, 0x18); //陀螺仪自检及测量范围典型值0x18(不自检2000deg/s)
Delay(200);
}
/**
* @brief 读取MPU6050的ID
* @param
* @retval 正常返回1异常返回0
*/
uint8_t MPU6050ReadID(void)
{
unsigned char Re = 0;
MPU6050_ReadData(MPU6050_RA_WHO_AM_I,&Re,1); //读器件地址
if(Re != 0x68)
{
MPU_ERROR("MPU6050 dectected error!\r\n检测不到MPU6050模块请检查模块与开发板的接线");
return 0;
}
else
{
MPU_INFO("MPU6050 ID = %d\r\n",Re);
return 1;
}
}
/**
* @brief 读取MPU6050的加速度数据
* @param
* @retval
*/
void MPU6050ReadAcc(short *accData)
{
uint8_t buf[6];
MPU6050_ReadData(MPU6050_ACC_OUT, buf, 6);
accData[0] = (buf[0] << 8) | buf[1];
accData[1] = (buf[2] << 8) | buf[3];
accData[2] = (buf[4] << 8) | buf[5];
}
/**
* @brief 读取MPU6050的角加速度数据
* @param
* @retval
*/
void MPU6050ReadGyro(short *gyroData)
{
uint8_t buf[6];
MPU6050_ReadData(MPU6050_GYRO_OUT,buf,6);
gyroData[0] = (buf[0] << 8) | buf[1];
gyroData[1] = (buf[2] << 8) | buf[3];
gyroData[2] = (buf[4] << 8) | buf[5];
}
/**
* @brief 读取MPU6050的原始温度数据
* @param
* @retval
*/
void MPU6050ReadTemp(short *tempData)
{
uint8_t buf[2];
MPU6050_ReadData(MPU6050_RA_TEMP_OUT_H,buf,2); //读取温度值
*tempData = (buf[0] << 8) | buf[1];
}
/**
* @brief 读取MPU6050的温度数据转化成摄氏度
* @param
* @retval
*/
void MPU6050_ReturnTemp(float *Temperature)
{
short temp3;
uint8_t buf[2];
MPU6050_ReadData(MPU6050_RA_TEMP_OUT_H,buf,2); //读取温度值
temp3= (buf[0] << 8) | buf[1];
*Temperature=((double) temp3/340.0)+36.53;
}