因为我的博客已经对I2C协议的详细过程已经做了一个例子!所以这个MPU6050的程序我将使用库函数完成!
第一步:硬件连接:
第二步:初始化I2C端口的函数:
/***PB6/PB7 端口初始化****/
static void I2C_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB,ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/***模式设置 使能I2C***/
static void I2C_Mode_Config(void)
{
I2C_InitTypeDef I2C_InitStructure;
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C ;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 50000;
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd (I2C1,ENABLE);
I2C_AcknowledgeConfig(I2C1, ENABLE);
}
/***初始化***/
void I2C_MPU6050_Init(void)
{
I2C_GPIO_Config();
I2C_Mode_Config();
}
第三步:写一个写数据函数和一个读数据函数
/** 读数据函数****/
void I2C_ByteWrite(uint8_t REG_Address,uint8_t REG_data)
{
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,SlaveAddress,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,REG_Address);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,REG_data);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1,ENABLE);
}
/*** 读数据函数 **/
uint8_t I2C_ByteRead(uint8_t REG_Address)
{
uint8_t REG_data;
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,SlaveAddress,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_Cmd(I2C1,ENABLE);
I2C_SendData(I2C1,REG_Address);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,SlaveAddress,I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_AcknowledgeConfig(I2C1,DISABLE);
I2C_GenerateSTOP(I2C1,ENABLE);
while(!(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)));
REG_data=I2C_ReceiveData(I2C1);
return REG_data;
}
/** mpu6050初始化***/
void InitMPU6050(void)
{
I2C_ByteWrite(PWR_MGMT_1,0x00);
I2C_ByteWrite(SMPLRT_DIV,0x07);
I2C_ByteWrite(CONFIG,0x06);
I2C_ByteWrite(GYRO_CONFIG,0x18);
I2C_ByteWrite(ACCEL_CONFIG,0x01);
}
最后一步:
先写一个获取数据的函数:
unsigned int GetData(unsigned char REG_Address)
{
char H,L;
H=I2C_ByteRead(REG_Address);
L=I2C_ByteRead(REG_Address+1);
return (H<<8)+L;
}
/**打印输出**/
主函数:
int main(void)
{
//延时函数自己写
USART1_Config(); //串口数据发送自己写
I2C_MPU6050_Init();
InitMPU6050();
while(1)
{
printf("\r\n---------加速度X轴原始数据--------%d \r\n",GetData(ACCEL_XOUT_H));
printf("\r\n---------加速度Y轴原始数据---------%d \r\n",GetData(ACCEL_YOUT_H));
printf("\r\n---------加速度Z轴原始数据----------%d \r\n",GetData(ACCEL_ZOUT_H));
printf("\r\n---------陀螺仪X轴原始数据----------%d \r\n",GetData(GYRO_XOUT_H));
printf("\r\n---------陀螺仪Y轴原始数据----------%d \r\n",GetData(GYRO_YOUT_H));
printf("\r\n---------陀螺仪Z轴原始数据----------%d \r\n",GetData(GYRO_ZOUT_H));
delay_ms(2000);
printf("===================================================================== /r/n");
}
}
PC显示结果:
完成了!!!