I2C 接 MPU6050
MPU6050
量化范围-32768~32767
量程 | ||
3轴加速度计 | 测量加速度 | ±2/4/8/16(G) |
3轴陀螺仪传感器 | 测量角速度 | ±250/500/1000/2000(°/SEC) |
从机地址
AD0=0 :110 1000
AD0=1 :110 1001
↓↓MPU6050初始化&获取加速度角速度数据
#include "main.h"
#include "MPU6050.h"
#include "i2c.h"
#include "usart.h"
/*读指定寄存器*/
uint8_t MPU6050_ReadReg(uint8_t RegAddress )
{
uint8_t data;
HAL_I2C_Master_Transmit(&hi2c1,MPU6050_Address, &RegAddress, 1, 1000);
HAL_I2C_Master_Receive(&hi2c1,MPU6050_Address,&data,1,1000);
return data;
}
/*写指定寄存器*/
void MPU6050_WriteReg(uint8_t RegAddress,uint8_t Data)
{
uint8_t transmitData[2] = {RegAddress,Data};
HAL_I2C_Master_Transmit(&hi2c1,MPU6050_Address, transmitData, 2, 1000);
}
void MPU6050_Init(void )
{
uint8_t getReadReg = 0x00;
getReadReg = MPU6050_ReadReg(MPU6050_WHO_AM_I);
HAL_UART_Transmit(&huart2,&getReadReg,1,1000);
MPU6050_WriteReg(MPU6050_PWR_MGMT_1,0x01);
MPU6050_WriteReg(MPU6050_PWR_MGMT_2,0x00);
MPU6050_WriteReg(MPU6050_SMPLRT_DIV,0x09);
MPU6050_WriteReg(MPU6050_CONFIG,0x06);
MPU6050_WriteReg(MPU6050_GYRO_CONFIG,0x18);
MPU6050_WriteReg(MPU6050_ACCEL_CONFIG,0x18);
}
void MPU6050_GetData(int16_t *AccX, int16_t *AccY, int16_t *AccZ,
int16_t *GyroX,int16_t *GyroY,int16_t *GyroZ)
{
uint16_t DataH,DataL;
/*读取加速度*/
DataH = MPU6050_ReadReg(MPU6050_ACCEL_XOUT_H);
DataL = MPU6050_ReadReg(MPU6050_ACCEL_XOUT_L);
*AccX = (DataH<<8) | DataL;
DataH = MPU6050_ReadReg(MPU6050_ACCEL_YOUT_H);
DataL = MPU6050_ReadReg(MPU6050_ACCEL_YOUT_L);
*AccY = (DataH<<8) | DataL;
DataH = MPU6050_ReadReg(MPU6050_ACCEL_ZOUT_H);
DataL = MPU6050_ReadReg(MPU6050_ACCEL_ZOUT_L);
*AccZ = (DataH<<8) | DataL;
/*读取角速度*/
DataH = MPU6050_ReadReg(MPU6050_GYRO_XOUT_H);
DataL = MPU6050_ReadReg(MPU6050_GYRO_XOUT_L);
*GyroX = (DataH<<8) | DataL;
DataH = MPU6050_ReadReg(MPU6050_GYRO_YOUT_H);
DataL = MPU6050_ReadReg(MPU6050_GYRO_YOUT_L);
*GyroY = (DataH<<8) | DataL;
DataH = MPU6050_ReadReg(MPU6050_GYRO_ZOUT_H);
DataL = MPU6050_ReadReg(MPU6050_GYRO_ZOUT_L);
*GyroZ = (DataH<<8) | DataL;
}
读取数据结果: