|
虚拟现实远端消防机器人组进度贴:STM32F746用模拟I2C(二)--读取MPU6050
[复制链接]
本帖最后由 天天好心情亲 于 2015-12-14 09:27 编辑
上次用F7的模拟I2C读取了电子罗盘的数据 ,但是无法读取MPU6050的数据,经过稍稍的改动现在成功读取了MPU6050的数据源码和图片如下 - /*********I2C*********************/
- uint32_t I2CStep;
- void I2C1_Delay(void)
- {
- I2CStep=50;
- while(I2CStep--);
- }
- uint8_t I2C1_Start(void)
- {
- I2C_SDA_OUT();
-
- I2C1_SDA_H;
- I2C1_SCL_H;
- I2C1_Delay();
- I2C1_SDA_L;
- I2C1_Delay();
- I2C1_SCL_L;
- }
- void I2C1_Stop(void)
- {
- I2C_SDA_OUT();
- I2C1_SCL_L;
- I2C1_SDA_L;
- I2C1_SCL_H;
- I2C1_Delay();
- I2C1_SDA_H;
- I2C1_Delay();
- }
- void I2C1_Ack(void)
- {
- I2C1_SCL_L;
- I2C_SDA_OUT();
- I2C1_SDA_L;
- I2C1_Delay();
- I2C1_SCL_H;
- I2C1_Delay();
- I2C1_SCL_L;
- }
- void I2C1_NAck(void)
- {
- I2C1_SCL_L;
- I2C_SDA_OUT();
- I2C1_SDA_H;
- I2C1_Delay();
- I2C1_SCL_H;
- I2C1_Delay();
- I2C1_SCL_L;
- }
- uint8_t I2C1_WaitAck(void)
- {
- uint8_t tempTime=0;
- I2C_SDA_IN();
- I2C1_SDA_H;
- I2C1_Delay();
- I2C1_SCL_H;
- I2C1_Delay();
- while(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
- {
- tempTime++;
- if(tempTime>250)
- {
- I2C1_Stop();
- return 1;
- }
- }
- I2C1_SCL_L;
- return 0;
- }
- uint8_t I2C1_SendByte(uint8_t SendByte) //
- {
- uint8_t i=0;
- I2C_SDA_OUT();
- I2C1_SCL_L;//拉低时钟开始数据传输
- for(i=0;i<8;i++)
- {
- if((SendByte&0x80)>0) //0x80 1000 0000
- I2C1_SDA_H;
- else
- I2C1_SDA_L;
- SendByte<<=1;
- I2C1_SCL_H;
- I2C1_Delay(); //发送数据
- I2C1_SCL_L;
- I2C1_Delay();
- }
- }
- uint8_t I2C1_ReceiveByte(void) //
- {
- uint8_t i,Dat = 0;
- I2C1_SDA_H;
- I2C1_SCL_L;
- Dat=0;
- for(i=0;i<8;i++)
- {
- I2C1_SCL_H;//???????[???],????????
- I2C1_Delay();
- I2C1_Delay();
- I2C1_Delay();
- Dat<<=1;
- if(I2C1_SDA) //?????
- {
- Dat|=0x01;
- }
- I2C1_SCL_L;//?????????
- I2C1_Delay();
- I2C1_Delay();
- I2C1_Delay();//???????
- }
- return Dat;
- }
- //读1个字节,ack=1时,发送ACK,ack=0,发送nACK
- uint8_t I2C1_Read_Byte(unsigned char ack)
- {
- uint8_t i=0,receive=0;
- I2C_SDA_IN();
- for(i=0;i<8;i++)
- {
- I2C1_SCL_L;
- I2C1_Delay();
- I2C1_SCL_H;
- receive<<=1;
- if(HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9))
- receive++;
- I2C1_Delay();
- }
- if(ack==0)
- I2C1_NAck();
- else
- I2C1_Ack();
- return receive;
- }
- /*******MPU6050****************/
- void PMU6050_WriteReg(uint8_t reg_add,uint8_t reg_dat)
- {
- I2C1_Start();
- I2C1_SendByte(MPU6050_SLAVE_ADDRESS);
- I2C1_WaitAck();
- I2C1_SendByte(reg_add);
- I2C1_WaitAck();
- I2C1_SendByte(reg_dat);
- I2C1_WaitAck();
- I2C1_Stop();
- }
- void PMU6050_ReadData(uint8_t reg_add,unsigned char*Read,uint8_t num)
- {
- unsigned char i;
-
- I2C1_Start();
- I2C1_SendByte(MPU6050_SLAVE_ADDRESS);
- I2C1_WaitAck();
- I2C1_SendByte(reg_add);
- I2C1_WaitAck();
-
- I2C1_Start();
- I2C1_SendByte(MPU6050_SLAVE_ADDRESS+1);
- I2C1_WaitAck();
-
- for(i=0;i<(num-1);i++){
- *Read=I2C1_Read_Byte(1);
- Read++;
- }
- *Read=I2C1_Read_Byte(0);
- I2C1_Stop();
- }
- void MPU6050_Init(void)
- {
- I2C1_Delay();
- I2C1_Delay();
- I2C1_Delay();
- // MPU6050_PWR_MGMT_1_INIT();
- PMU6050_WriteReg(MPU6050_RA_PWR_MGMT_1, 0x00); //解除休眠状态
- PMU6050_WriteReg(MPU6050_RA_SMPLRT_DIV , 0x07); //陀螺仪采样率
- PMU6050_WriteReg(MPU6050_RA_CONFIG , 0x06);
- PMU6050_WriteReg(MPU6050_RA_ACCEL_CONFIG , 0x01); //配置加速度传感器工作在16G模式
- PMU6050_WriteReg(MPU6050_RA_GYRO_CONFIG, 0x18); //陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)
- }
- void MPU6050ReadID(void)
- {
- unsigned char Re = 0;
- PMU6050_ReadData(MPU6050_RA_WHO_AM_I,&Re,1); //读器件地址
- // printf("%d\r\n",Re);
- }
- void MPU6050ReadAcc(short *accData)
- {
- uint8_t buf[6];
- PMU6050_ReadData(MPU6050_ACC_OUT, buf, 6);
- accData[0] = (buf[0] << 8) | buf[1];
- accData[1] = (buf[2] << 8) | buf[3];
- accData[2] = (buf[4] << 8) | buf[5];
- printf("AccX = %d ",accData[0]);
- printf("AccY = %d ",accData[1]);
- printf("AccZ = %d ",accData[2]);
- }
- void MPU6050ReadGyro(short *gyroData)
- {
- uint8_t buf[6];
- PMU6050_ReadData(MPU6050_GYRO_OUT,buf,6);
- gyroData[0] = (buf[0] << 8) | buf[1];
- gyroData[1] = (buf[2] << 8) | buf[3];
- gyroData[2] = (buf[4] << 8) | buf[5];
- printf("GYROX = %d ",gyroData[0]);
- printf("GYROY = %d ",gyroData[1]);
- printf("GYROZ = %d\r\n",gyroData[2]);
- }
- void MPU6050ReadTemp(short *tempData)
- {
- uint8_t buf[2];
- PMU6050_ReadData(MPU6050_RA_TEMP_OUT_H,buf,2); //读取温度值
- *tempData = (buf[0] << 8) | buf[1];
- }
- void MPU6050_ReturnTemp(short*Temperature)
- {
- short temp3;
- uint8_t buf[2];
-
- PMU6050_ReadData(MPU6050_RA_TEMP_OUT_H,buf,2); //读取温度值
- temp3= (buf[0] << 8) | buf[1];
- *Temperature=(((double) (temp3 + 13200)) / 280)-13;
- }
复制代码
|
|