#include "stm32f10x.h"
#include "stm32f10x_it.h"
#include <stdio.h>
#define MPU6050_SLAVE_ADDRESS 0xd0
#define I2C_Speed 400000
#define I2C2_SLAVE_ADDRESS7 0xA0
/*??????????*/
typedef struct{
short accData_x;
short accData_y;
short accData_z;
}AccData;
/*??????????*/
typedef struct{
short gyroData_x;
short gyroData_y;
short gyroData_z;
}GyroData;
/*????*/
void GPIO_Config(void);
void USART_Config(void);
void IIC_Config(void);
void I2C_EE_ByteWrite(uint8_t pBuffer,uint8_t WriteAddr);
void I2C_EE_BufferRead(uint8_t* pBuffer,uint8_t ReadAddr,uint16_t NumByteToRead);
void MPU6050_Init(void);
void MPU6050ReadAcc(AccData* accData);
void MPU6050ReadGyro(GyroData* gyroData);
void MPU6050_ReturnTemp(double* temperature);
//??????,??printf??
#if 1
#pragma import(__use_no_semihosting)
//??????????
struct __FILE
{
int handle;
};
FILE __stdout;
//??_sys_exit()??????????
void _sys_exit(int x)
{
x = x;
}
//???fputc??
int fputc(int ch, FILE *f)
{
USART_SendData(USART1,(uint8_t) ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET) {}
return ch;
}
#endif
void delay(int time)
{
int j,k;
for(j = 0;j < time;j++)
for(k = 0;k < 1500;k++);
}
int main(void)
{
AccData accData;
GyroData gyroData;
double temperature;//??????
USART_Config();
IIC_Config();
MPU6050_Init();
while (1)
{
MPU6050ReadAcc(&accData);
printf("AccData:%8d %8d %8d",accData.accData_x,accData.accData_y,accData.accData_z);
MPU6050ReadGyro(&gyroData);
printf("GyroData:%8d %8d %8d",gyroData.gyroData_x,gyroData.gyroData_y,gyroData.gyroData_z);
MPU6050_ReturnTemp(&temperature);
printf(" temperature: %.2f?\r\n",temperature);
//delay(1000);
}
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/*????,?????I/O?????????*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO, ENABLE);
/*??PA9 USART1_Tx???????*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
/*??PA10 USART1_Rx?????*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????-RX
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600; //??????9600 /* 8????,1????,?????*/
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
/*????????*/
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE); //??USART
}
void IIC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//??IO???
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);//??I2C??
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;//??????
GPIO_Init(GPIOB,&GPIO_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;//??I2C??
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;//SCL???????2:1
I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;
I2C_InitStructure.I2C_OwnAddress1=I2C2_SLAVE_ADDRESS7;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;//??ACK??
I2C_InitStructure.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
//7?????
I2C_Init(I2C2,&I2C_InitStructure);//I2C???
I2C_Cmd(I2C2,ENABLE);//??I2C
}
void I2C_EE_ByteWrite(uint8_t pBuffer,uint8_t WriteAddr)
{
I2C_GenerateSTART(I2C2,ENABLE);//??IIC????
while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C2,MPU6050_SLAVE_ADDRESS,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C2,WriteAddr);
while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C2,pBuffer);
while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C2,ENABLE);//??IIC????
}
void I2C_EE_BufferRead(uint8_t* pBuffer,uint8_t ReadAddr,uint16_t NumByteToRead)
{
while(I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY));//??IIC????,??????????
I2C_GenerateSTART(I2C2,ENABLE);//??IIC????
while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_MODE_SELECT));//??????IIC??????
I2C_Send7bitAddress(I2C2,MPU6050_SLAVE_ADDRESS,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //??????IIC??????
I2C_Cmd(I2C2,ENABLE);
I2C_SendData(I2C2,ReadAddr);
while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_TRANSMITTED));//??????IIC??????
I2C_GenerateSTART(I2C2,ENABLE);
while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_MODE_SELECT));//??????IIC??????
I2C_Send7bitAddress(I2C2,MPU6050_SLAVE_ADDRESS,I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); //??????IIC??????
while(NumByteToRead)
{
if(NumByteToRead==1)
{
I2C_AcknowledgeConfig(I2C2,DISABLE);//???ACK??
I2C_GenerateSTOP(I2C2,ENABLE);//IIC????
}
if(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_RECEIVED))
{
*pBuffer = I2C_ReceiveData(I2C2);
pBuffer++;
NumByteToRead--;
}
}
I2C_AcknowledgeConfig(I2C2,ENABLE); //??ACK??????????
}
void MPU6050_Init(void)
{
int i=0,j=0;
//?????????????,?????,??????????????
for(i=0;i<1000;i++)
for(j=0;j<1000;j++);
I2C_EE_ByteWrite(0x01,0x6B);//???????1:0x6B ?????? X????
I2C_EE_ByteWrite(0x18,0x1B);//????????:0x1B ?????????????��2000��/s
I2C_EE_ByteWrite(0x18,0x1C);//???????????:0x1C ???????????16G??
I2C_EE_ByteWrite(0x13,0x19);//???????????:0x19 ??????50Hz
I2C_EE_ByteWrite(0x06,0x1A);//?????:0x1A ????????5Hz ????????5Hz
I2C_EE_ByteWrite(0x00,0X6C);//??????????
}
void MPU6050ReadAcc(AccData* accData)
{
u8 buf[6];
I2C_EE_BufferRead(buf,0x3B,6);//??????????0x3B~0x40??
accData->accData_x = (buf[0] << 8) | buf[1];
accData->accData_y = (buf[2] << 8) | buf[3];
accData->accData_z = (buf[4] << 8) | buf[5];
}
void MPU6050ReadGyro(GyroData* gyroData)
{
u8 buf[6];
I2C_EE_BufferRead(buf,0x43,6);//???????????0x43~0x48??
gyroData->gyroData_x = (buf[0] << 8) | buf[1];
gyroData->gyroData_y = (buf[2] << 8) | buf[3];
gyroData->gyroData_z = (buf[4] << 8) | buf[5];
}
void MPU6050_ReturnTemp(double* temperature)
{
short temp;
u8 buf[2];
I2C_EE_BufferRead(buf,0x41,2);//???????0x41~0x42??
temp = (buf[0] << 8) | buf[1];
*temperature = 36.53 + temp/340.0;
}
评论0