#include "my_i2c.h"
#include "mpu6050.h"
#include "usart.h"
#include <stdio.h>
uint8_t mpu6050_init()
{
uint8_t id,data;
IIC_gpio_init();
mpu6050_read_len(MPU6050_ADDR,MPU_DEVICE_ID_REG,1,&id);
if(id == 0x68){
mpu6050_reset(); /* mpu6050软件复位 0x6B */
mpu6050_set_rate(100); /* 设置陀螺仪采样频率为100Hz 0x19 */
mpu6050_set_accelerated_speed(0); /* 设置加速度量程范围:±2g 0x1C */
mpu6050_set_gyroscope(3); /* 设置陀螺仪量程范围:±2000dps 0x1B */
/* 关闭所有中断 */
data = 0;
mpu6050_write_len(MPU6050_ADDR,MPU_INT_EN_REG,1,&data);
/* 关闭IIC主模式 */
data = 0;
mpu6050_write_len(MPU6050_ADDR,MPU_USER_CTRL_REG,1,&data);
/* 关闭FIFO */
data = 0;
mpu6050_write_len(MPU6050_ADDR,MPU_FIFO_EN_REG,1,&data);
/* 设置CLKSEL,PLL X轴为参考 */
data = 0x01;
mpu6050_write_len(MPU6050_ADDR,MPU_PWR_MGMT1_REG,1,&data);
/* 加速度与陀螺仪都工作 */
data = 0;
mpu6050_write_len(MPU6050_ADDR,MPU_PWR_MGMT2_REG,1,&data);
return 0;
}
return 1;
}
/* mpu6050软件复位 */
void mpu6050_reset()
{
uint8_t data;
data = 0x80;
mpu6050_write_len(MPU6050_ADDR,MPU_PWR_MGMT1_REG,1,&data); // 复位mpu6050
HAL_Delay(100);
data = 0x00;
// 唤醒mpu6050
}
/* 设置数字低通滤波器频率(陀螺仪输出频率) */
void mpu6050_set_lpf(uint16_t lpf)
{
// 该函数设置陀螺仪输出频率为1000Hz
uint8_t data;
mpu6050_write_len(MPU6050_ADDR,MPU_CFG_REG,1,&data);
}
/* 设置陀螺仪采样频率:4~1000Hz */
void mpu6050_set_rate(uint16_t rata)
{
// 陀螺仪采样率rate = 陀螺仪输出频率(1000Hz或8000Hz,mpu6050_set_lpf函数设定为1000Hz) / (SMPLPT_DIV + 1)(采样周期)
uint8_t data;
if(rata >= 1000) rata = 1000;
if(rata <= 4) rata = 4;
data = 1000/rata - 1;
mpu6050_write_len(MPU6050_ADDR,MPU_SAMPLE_RATE_REG,1,&data);
mpu6050_set_lpf(rata / 2); // 自动设置低通滤波频率为采样率的一半
}
/*
设置加速度量程范围:
data:
0 --> ±2g
1 --> ±4g
2 --> ±8g
3 --> ±16g
*/
void mpu6050_set_accelerated_speed(uint8_t data)
{
data <<= 3;
mpu6050_write_len(MPU6050_ADDR,MPU_ACCEL_CFG_REG,1,&data);
}
/*
设置陀螺仪量程范围:
fsr:
0 --> ±250dps
1 --> ±500dps
2 --> ±1000dps
3 --> ±2000dps
*/
void mpu6050_set_gyroscope(uint8_t data)
{
data <<= 3;
mpu6050_write_len(MPU6050_ADDR,MPU_GYRO_CFG_REG,1,&data);
}
/* 获取mpu6050的原始数据 */
void mpu6050_read_accelerated_speed(int16_t *accelerated_speed_x,int16_t *accelerated_speed_y,int16_t *accelerated_speed_z)
{
uint8_t rev_buf[6];
mpu6050_read_len(MPU6050_ADDR,MPU_ACCEL_XOUTH_REG,6,rev_buf); //读取x,y,z三轴的加速度值
*accelerated_speed_x = (int16_t)(rev_buf[0] << 8 | rev_buf[1]);
*accelerated_speed_y = (int16_t)(rev_buf[2] << 8 | rev_buf[3]);
*accelerated_speed_z = (int16_t)(rev_buf[4] << 8 | rev_buf[5]);
}
void mpu6050_read_gyroscope(int16_t *gyroscope_x,int16_t *gyroscope_y,int16_t *gyroscope_z)
{
uint8_t rev_buf[6];
mpu6050_read_len(MPU6050_ADDR,MPU_GYRO_XOUTH_REG,6,rev_buf); //读取x,y,z三轴的陀螺仪值
*gyroscope_x = (int16_t)(rev_buf[0] << 8 | rev_buf[1]);
*gyroscope_y = (int16_t)(rev_buf[2] << 8 | rev_buf[3]);
*gyroscope_z = (int16_t)(rev_buf[4] << 8 | rev_buf[5]);
}
float mpu6050_read_temperature()
{
uint8_t rev_buf[2];
int16_t *temperature;
mpu6050_read_len(MPU6050_ADDR,MPU_TEMP_OUTH_REG,2,rev_buf);
*temperature = (int16_t)(rev_buf[0] << 8 | rev_buf[1]);
return 36.53 + *temperature / 340;
}
/* 下面两个函数用来代替dmp库中内容 */
uint8_t mpu6050_write_len(uint8_t addr,uint8_t reg,uint8_t len,uint8_t *buf)
{
uint8_t i;
IIC_start();
IIC_send_byte((addr | 0));
if (IIC_wait_ack() == 1)
{
IIC_stop();
return 1;
}
IIC_send_byte(reg);
if (IIC_wait_ack() == 1)
{
IIC_stop();
return 1;
}
for (i=0; i<len; i++)
{
IIC_send_byte(buf[i]);
if (IIC_wait_ack() == 1)
{
IIC_stop();
return 1;
}
}
IIC_stop();
return 0;
}
uint8_t mpu6050_read_len(uint8_t addr,uint8_t reg,uint8_t len,uint8_t *buf)
{
IIC_start();
IIC_send_byte(addr | 0);
if (IIC_wait_ack() == 1)
{
IIC_stop();
return 1;
}
IIC_send_byte(reg);
if (IIC_wait_ack() == 1)
{
IIC_stop();
return 1;
}
IIC_start();
IIC_send_byte(addr | 1);
if (IIC_wait_ack() == 1)
{
IIC_stop();
return 1;
}
while (len)
{
*buf = IIC_read_byte((len > 1) ? 1 : 0);
len--;
buf++;
}
IIC_stop();
return 0;
}