MPU6050简介:
MPU6050是InvenSense公司推出的全球首款整合性6轴运动处理组件,内带3轴陀螺仪和3轴加速度传感器,并且含有一个第二IIC接口,可用于连接外部磁力传感器。
那如何通过MPU6050传感器来获取数据呢?
具体步骤如下:
1.新建项目:
2.选择STM32F103C8T6芯片
3.对工程文件命名
4.在Connectivity中选择I2C1,在mode中选择I2C
?5.根据原理图选择引脚
其中MPU6050中的SCL和SDA应对应于C8T6主控模块上的PB8和PB9,故应在右侧引脚设置中单击PB9,PB8引脚,选择I2C1_SDA项。
?
?
?
6.点击生成代码,在MPU6050下新建一个User文件夹,在User文件夹下新建一个BSP文件夹,在BSP文件夹下添加Inc和Src文件夹,并在Inc和Src文件夹下添加bsp_mpu6050.c和bsp_mpu6050.h文件。(注意新建的文件夹需要设置路径)
新建的User文件夹,设置路径:
(只有设置完路径后才能添加的上.c和.h文件)
再添加.c和,h文件
?
7.把以下代码加入bsp_mpu6050.c
#include "main.h"
extern I2C_HandleTypeDef hi2c1;
IMU_SensorData_Raw_Structer IMU_SensorData_Raw;
uint8_t BSP_MPU6050_ReadReg(uint8_t address)
{
uint8_t Value = 0;
HAL_I2C_Mem_Read(&hi2c1,IMU_ADDRESS_READ,address,I2C_MEMADD_SIZE_8BIT,&Value,1,0x10);
return Value;
}
void BSP_MPU6050_ReadMultiReg(uint8_t address,uint8_t length,uint8_t *data)
{
HAL_I2C_Mem_Read(&hi2c1,IMU_ADDRESS_READ,address,I2C_MEMADD_SIZE_8BIT,data,length,0x10);
}
void BSP_MPU6050_WriteReg(uint8_t address,uint8_t data)
{
HAL_I2C_Mem_Write(&hi2c1,IMU_ADDRESS_WRITE,address,I2C_MEMADD_SIZE_8BIT,&data,1,0x10);
HAL_Delay(100);
}
void BSP_MPU6050_WriteMultiReg(uint8_t address,uint8_t length,uint8_t *data)
{
HAL_I2C_Mem_Write(&hi2c1,IMU_ADDRESS_WRITE,address,I2C_MEMADD_SIZE_8BIT,data,length,0x10);
HAL_Delay(100);
}
uint8_t BSP_MPU6050_Read_WHOAMI(void)
{
return BSP_MPU6050_ReadReg(MPUREG_WHOAMI);
}
uint8_t BSP_MPU6050_Init(void)
{
uint8_t tries;
for(tries = 0; tries<5; tries++)
{
BSP_MPU6050_WriteReg(MPUREG_PWR_MGMT_1,BIT_PWR_MGMT_1_DEVICE_RESET);//复位MPU6050
BSP_MPU6050_WriteReg(MPUREG_PWR_MGMT_1,BIT_PWR_MGMT_1_CLK_ZGYRO);//设置时钟
if(BSP_MPU6050_ReadReg(MPUREG_PWR_MGMT_1) == BIT_PWR_MGMT_1_CLK_ZGYRO)//判断是否写入,写入则跳出循环
break;
}
if(tries == 5)
return 0;
BSP_MPU6050_WriteReg(MPUREG_PWR_MGMT_2,0x00);//禁止休眠
BSP_MPU6050_WriteReg(MPUREG_USER_CTRL,0x00);//I2C主模式关闭
BSP_MPU6050_WriteReg(MPUREG_CONFIG,BITS_DLPF_CFG_20HZ);//设置低通滤波器
BSP_MPU6050_WriteReg(MPUREG_SMPLRT_DIV,MPUREG_SMPLRT_200HZ);//设置采样频率
BSP_MPU6050_WriteReg(MPUREG_GYRO_CONFIG,BITS_GYRO_FS_2000DPS);//设置陀螺仪量程
BSP_MPU6050_WriteReg(MPUREG_ACCEL_CONFIG,BITS_ACCEL_FS_8G);//设置加速度计量程
BSP_MPU6050_WriteReg(MPUREG_INT_ENABLE,0x00);//禁止中断
if(BSP_MPU6050_Read_WHOAMI() == IMU_WHOAMI)
return 1;
else
return 0;
}
void BSP_MPU6050_UpdateSensors(void)
{
uint8_t buffer[14];
BSP_MPU6050_ReadMultiReg(MPUREG_ACCEL_XOUT_H,14,buffer);
IMU_SensorData_Raw.ACC_X = (buffer[0]<<8)|buffer[1];
IMU_SensorData_Raw.ACC_Y = (buffer[2]<<8)|buffer[3];
IMU_SensorData_Raw.ACC_Z = (buffer[4]<<8)|buffer[5];
IMU_SensorData_Raw.Temp = (buffer[6]<<8)|buffer[7];
IMU_SensorData_Raw.GYR_X = (buffer[8]<<8)|buffer[9];
IMU_SensorData_Raw.GYR_Y = (buffer[10]<<8)|buffer[11];
IMU_SensorData_Raw.GYR_Z = (buffer[12]<<8)|buffer[13];
}
8.将以下代码加入bsp_mpu6050.h中
#ifndef BSP_INC_BSP_MPU6050_H_
#define BSP_INC_BSP_MPU6050_H_
#define IMU_WHOAMI 0x68
#define IMU_ADDRESS_READ 0xD1
#define IMU_ADDRESS_WRITE 0xD0
#define MPUREG_SMPLRT_DIV 0x19 // sample rate. Fsample= 1Khz/(<this value>+1) = 200Hz
#define MPUREG_SMPLRT_1000HZ 0x00
#define MPUREG_SMPLRT_500HZ 0x01
#define MPUREG_SMPLRT_250HZ 0x03
#define MPUREG_SMPLRT_200HZ 0x04
#define MPUREG_SMPLRT_100HZ 0x09
#define MPUREG_SMPLRT_50HZ 0x13
#define MPUREG_CONFIG 0x1A //低通滤波频率
#define MPUREG_GYRO_CONFIG 0x1B
// bit definitions for MPUREG_GYRO_CONFIG
#define BITS_GYRO_FS_250DPS 0x00
#define BITS_GYRO_FS_500DPS 0x08
#define BITS_GYRO_FS_1000DPS 0x10
#define BITS_GYRO_FS_2000DPS 0x18
#define BITS_GYRO_FS_MASK 0x18 // only bits 3 and 4 are used for gyro full scale so use this to mask off other bits
#define BITS_GYRO_ZGYRO_SELFTEST 0x20
#define BITS_GYRO_YGYRO_SELFTEST 0x40
#define BITS_GYRO_XGYRO_SELFTEST 0x80
#define MPUREG_ACCEL_CONFIG 0x1C
#define BITS_ACCEL_FS_2G 0x00
#define BITS_ACCEL_FS_4G 0x08
#define BITS_ACCEL_FS_8G 0x10
#define BITS_ACCEL_FS_16G 0x18
#define MPUREG_INT_ENABLE 0x38
#define MPUREG_ACCEL_XOUT_H 0x3B
#define MPUREG_ACCEL_XOUT_L 0x3C
#define MPUREG_ACCEL_YOUT_H 0x3D
#define MPUREG_ACCEL_YOUT_L 0x3E
#define MPUREG_ACCEL_ZOUT_H 0x3F
#define MPUREG_ACCEL_ZOUT_L 0x40
#define MPUREG_TEMP_OUT_H 0x41
#define MPUREG_TEMP_OUT_L 0x42
#define MPUREG_GYRO_XOUT_H 0x43
#define MPUREG_GYRO_XOUT_L 0x44
#define MPUREG_GYRO_YOUT_H 0x45
#define MPUREG_GYRO_YOUT_L 0x46
#define MPUREG_GYRO_ZOUT_H 0x47
#define MPUREG_GYRO_ZOUT_L 0x48
#define MPUREG_USER_CTRL 0x6A
#define MPUREG_PWR_MGMT_1 0x6B
#define BIT_PWR_MGMT_1_CLK_INTERNAL 0x00 // clock set to internal 8Mhz oscillator
#define BIT_PWR_MGMT_1_CLK_XGYRO 0x01 // PLL with X axis gyroscope reference
#define BIT_PWR_MGMT_1_CLK_YGYRO 0x02 // PLL with Y axis gyroscope reference
#define BIT_PWR_MGMT_1_CLK_ZGYRO 0x03 // PLL with Z axis gyroscope reference
#define BIT_PWR_MGMT_1_CLK_EXT32KHZ 0x04 // PLL with external 32.768kHz reference
#define BIT_PWR_MGMT_1_CLK_EXT19MHZ 0x05 // PLL with external 19.2MHz reference
#define BIT_PWR_MGMT_1_CLK_STOP 0x07 // Stops the clock and keeps the timing generator in reset
#define BIT_PWR_MGMT_1_TEMP_DIS 0x08 // disable temperature sensor
#define BIT_PWR_MGMT_1_CYCLE 0x20 // put sensor into cycle mode. cycles between sleep mode and waking up to take a single sample of data from active sensors at a rate determined by LP_WAKE_CTRL
#define BIT_PWR_MGMT_1_SLEEP 0x40 // put sensor into low power sleep mode
#define BIT_PWR_MGMT_1_DEVICE_RESET 0x80 // reset entire device
#define MPUREG_PWR_MGMT_2 0x6C // allows the user to configure the frequency of wake-ups in Accelerometer Only Low Power Mode
#define MPUREG_WHOAMI 0x75
#define BITS_DLPF_CFG_20HZ 0x04
uint8_t BSP_MPU6050_ReadReg(uint8_t address);
void BSP_MPU6050_ReadMultiReg(uint8_t address,uint8_t length,uint8_t *data);
void BSP_MPU6050_WriteReg(uint8_t address,uint8_t data);
void BSP_MPU6050_WriteMultiReg(uint8_t address,uint8_t length,uint8_t *data);
uint8_t BSP_MPU6050_Read_WHOAMI(void);
uint8_t BSP_MPU6050_Init(void);
void BSP_MPU6050_UpdateSensors(void);
typedef struct
{
int16_t ACC_X;
int16_t ACC_Y;
int16_t ACC_Z;
int16_t GYR_X;
int16_t GYR_Y;
int16_t GYR_Z;
int16_t Temp;
}IMU_SensorData_Raw_Structer;
#endif /* BSP_INC_BSP_MPU6050_H_ */
9.把以下代码加入main.h中
#include "bsp_mpu6050.h"
10.点击编译并烧录,烧录完成后,把bsp_mpu6050.h文件中的机构体IMU_SensorData_Raw复制到右上角现场表达式中。
?
11.点击运行,可在现场表达式中观察到MPU测量出的数据。
?
|