步骤
一、配置I2C外设,对I2C2外设进行初始化(MyI2C_Init)
- 开启I2C外设和对应的GPIO口的时钟
- 把I2C对应的GPIO口初始化为复用开漏模式
- 使用结构体配置I2C
- I2C_Cmd,使能I2C
I2C_GenerateSTART //生产起始条件
I2C_GenerateSTOP //生产终止条件
I2C_AcknowledgeConfig //配置ACK应答使能
I2C_SendData //把数据写入DR寄存器
I2C_ReceiveData //读取DR数据
I2C_Send7bitAddress //发送7位地址
I2C_CheckEvent //检查标志位
二、控制外设电路,实现指定地址写的时序(WriteReg)
三、控制外设电路,实现指定地址读的实现(ReadReg)
MPU6050.c
#include "stm32f10x.h" // Device header
#include "MPU6050_Reg.h"
#define MPU6050_ADDRESS 0xD0
void MPU6050_waitEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
{
uint32_t Timeout;
Timeout = 10000;
while(I2C_CheckEvent(I2Cx , I2C_EVENT) != SUCCESS)
{
Timeout --; //超时退出机制
if(Timeout == 0)
{
break;
}
}
}
void MPU6050_WriteReg(uint8_t RegAddress, uint8_t Data)
{
I2C_GenerateSTART(I2C2 , ENABLE); //起始
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_MODE_SELECT); //检测EV5
I2C_Send7bitAddress(I2C2 , MPU6050_ADDRESS ,I2C_Direction_Transmitter); //发送7位地址
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED); //检测EV6
I2C_SendData(I2C2 , RegAddress); //发送数据
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_BYTE_TRANSMITTING); //检测EV8
I2C_SendData(I2C2 , Data);
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_BYTE_TRANSMITTED); //检测EV8_2
I2C_GenerateSTOP(I2C2 , ENABLE); //终止
}
uint8_t MPU6050_ReadReg(uint8_t RegAddress)
{
uint8_t Data;
I2C_GenerateSTART(I2C2 , ENABLE); //起始
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_MODE_SELECT); //检测EV5
I2C_Send7bitAddress(I2C2 , MPU6050_ADDRESS ,I2C_Direction_Transmitter); //发送7位地址
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED); //检测EV6
I2C_SendData(I2C2 , RegAddress); //发送数据
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_BYTE_TRANSMITTED); //检测EV8
I2C_GenerateSTART(I2C2 , ENABLE); //重复起始条件
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_MODE_SELECT); //检测EV5
I2C_Send7bitAddress(I2C2 , MPU6050_ADDRESS ,I2C_Direction_Receiver); //接收7位地址
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED); //检测EV6
I2C_AcknowledgeConfig(I2C2 , DISABLE); //设置ack=0
I2C_GenerateSTOP(I2C2 ,ENABLE); //stop = 1 申请产生终止条件
MPU6050_waitEvent(I2C2 , I2C_EVENT_MASTER_BYTE_RECEIVED); //检测EV7
Data = I2C_ReceiveData(I2C2); //数值赋给Data
I2C_AcknowledgeConfig(I2C2 , ENABLE); //设置ack=1
return Data;
}
void MPU6050_Init(void)
{
// MyI2C_Init();
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //复用AF:GPIO的控制权要交给硬件外设 开漏OD:I2C协议的要求
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
I2C_InitTypeDef I2C_Initstructure;
I2C_Initstructure.I2C_Mode = I2C_Mode_I2C;
I2C_Initstructure.I2C_ClockSpeed = 50000;
I2C_Initstructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_Initstructure.I2C_Ack = I2C_Ack_Enable;
I2C_Initstructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Initstructure.I2C_OwnAddress1 = 0x00;
I2C_Init(I2C2 , &I2C_Initstructure);
I2C_Cmd(I2C2 , ENABLE);
MPU6050_WriteReg(MPU6050_PWR_MGMT_1, 0x01);//解除睡眠、选择陀螺仪时钟
MPU6050_WriteReg(MPU6050_PWR_MGMT_2, 0x00);//6个轴均不待机
MPU6050_WriteReg(MPU6050_SMPLRT_DIV, 0x09);//10分频
MPU6050_WriteReg(MPU6050_CONFIG, 0x06);//配置寄存器
MPU6050_WriteReg(MPU6050_GYRO_CONFIG, 0x18);//陀螺仪配置寄存器
MPU6050_WriteReg(MPU6050_ACCEL_CONFIG, 0x18);//加速度配置寄存器
}
uint8_t MPU6050_GetID(void)
{
return MPU6050_ReadReg(MPU6050_WHO_AM_I);
}
void MPU6050_GetData(int16_t *AccX, int16_t *AccY, int16_t *AccZ,
int16_t *GyroX, int16_t *GyroY, int16_t *GyroZ)
//分别读取6个轴数据寄存器的高位和低位,拼接为16位数据再通过指针变量返回
{
uint8_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;
}