This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C123用硬件I2C读取mpu9250的问题

看着库函数手册和以前在32上写I2C的步骤写了个初始化程序 设备ID读出来0x00

下面是程序

void I2C_Init(void)
{
 SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C3);
 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
 
 GPIOPinTypeI2C(GPIO_PORTD_BASE,GPIO_PIN_1);
 GPIOPinTypeI2C(GPIO_PORTD_BASE,GPIO_PIN_0);
 GPIOPinTypeI2CSCL(GPIO_PORTD_BASE, GPIO_PIN_0);
  GPIOPinConfigure(GPIO_PD0_I2C3SCL);
  GPIOPinConfigure(GPIO_PD1_I2C3SDA); 
 
 I2CMasterInitExpClk(I2C3_BASE, SysCtlClockGet(), true);
  I2CMasterEnable(I2C3_BASE);
}
void MPU_Write_Byte(uint8_t addr,uint8_t reg,uint8_t data)
{
 I2CMasterSlaveAddrSet(I2C3_BASE,addr,false);
 I2CMasterDataPut(I2C3_BASE,reg);
 I2CMasterControl(I2C3_BASE,I2C_MASTER_CMD_BURST_SEND_START);
 while(I2CMasterBusy(I2C3_BASE));
 I2CMasterDataPut(I2C3_BASE,data);
 I2CMasterControl(I2C3_BASE,I2C_MASTER_CMD_BURST_SEND_CONT);
 while(I2CMasterBusy(I2C3_BASE));
 I2CMasterControl(I2C3_BASE,I2C_MASTER_CMD_BURST_SEND_FINISH);
}
uint8_t MPU_Read_Byte(uint8_t addr,uint8_t reg)
{
 uint32_t raw_res;
 uint8_t res;
 I2CMasterSlaveAddrSet(I2C3_BASE,addr,false);
  I2CMasterDataPut(I2C3_BASE,reg);
 I2CMasterControl(I2C3_BASE,I2C_MASTER_CMD_BURST_SEND_START);
 while(I2CMasterBusy(I2C3_BASE));
 I2CMasterSlaveAddrSet(I2C3_BASE,addr,true);
  I2CMasterControl(I2C3_BASE,I2C_MASTER_CMD_SINGLE_RECEIVE);
  while(I2CMasterBusy(I2C3_BASE));
  raw_res=I2CMasterDataGet(I2C3_BASE);
  res=(uint8_t)(raw_res>>24);
  return res;
}
void MPU_Read_Len(uint8_t addr,uint8_t reg,uint8_t len,uint8_t *buf)
{
 uint8_t i;
 uint32_t raw_res;
 uint8_t res;
 I2CMasterSlaveAddrSet(I2C3_BASE,addr,false);
 I2CMasterDataPut(I2C3_BASE,reg);
 I2CMasterControl(I2C3_BASE,I2C_MASTER_CMD_BURST_SEND_START);
  while(I2CMasterBusy(I2C3_BASE));
  I2CMasterSlaveAddrSet(I2C3_BASE,addr,true);
 I2CMasterControl(I2C3_BASE,I2C_MASTER_CMD_BURST_RECEIVE_START);
 while(I2CMasterBusy(I2C3_BASE));
 for(i=0;i<len;i++)
 {
  I2CMasterControl(I2C3_BASE,I2C_MASTER_CMD_BURST_RECEIVE_CONT);
   while(I2CMasterBusy(I2C3_BASE));
    raw_res=I2CMasterDataGet(I2C3_BASE);
    res=(uint8_t)(raw_res>>24);  
   buf++;
 }
 I2CMasterControl(I2C3_BASE,I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
}
下面是初始化程序
uint8_t MPU9250_Init(void)
{
 uint8_t res=0;
 I2C_Init();
 MPU_Write_Byte(MPU9250_ADDR,MPU_PWR_MGMT1_REG,0X80);
 delay_Nms(200);
 MPU_Write_Byte(MPU9250_ADDR,MPU_PWR_MGMT1_REG,0X00);
  MPU_Set_Gyro_Fsr(3); //ÍÓÂÝÒÇ´«¸ÐÆ÷,¡À2000dps
 MPU_Set_LPF(42);
 MPU_Set_Accel_Fsr(0);               //¼ÓËٶȴ«¸ÐÆ÷,¡À2g
  MPU_Set_Rate(500);                //ÉèÖòÉÑùÂÊ500Hz
  MPU_Write_Byte(MPU9250_ADDR,MPU_INT_EN_REG,0X00);   //¹Ø±ÕËùÓÐÖжÏ
 MPU_Write_Byte(MPU9250_ADDR,MPU_USER_CTRL_REG,0X00);//I2CÖ÷ģʽ¹Ø±Õ
 MPU_Write_Byte(MPU9250_ADDR,MPU_FIFO_EN_REG,0XFF);
 MPU_Write_Byte(MPU9250_ADDR,MPU_INTBP_CFG_REG,0X82);//INTÒý½ÅµÍµçƽÓÐЧ£¬¿ªÆôbypassģʽ£¬¿ÉÒÔÖ±½Ó¶ÁÈ¡´ÅÁ¦¼Æ
 res=MPU_Read_Byte(MPU9250_ADDR,MPU_DEVICE_ID_REG|0x80);  //¶ÁÈ¡MPU6500µÄID
 if(res==MPU6500_ID) //Æ÷¼þIDÕýÈ·
 {
   MPU_Write_Byte(MPU9250_ADDR,MPU_PWR_MGMT1_REG,0X01);   //ÉèÖÃCLKSEL,PLL XÖáΪ²Î¿¼
   MPU_Write_Byte(MPU9250_ADDR,MPU_PWR_MGMT2_REG,0X00);   //¼ÓËÙ¶ÈÓëÍÓÂÝÒǶ¼¹¤×÷
   MPU_Set_Rate(500);              //ÉèÖòÉÑùÂÊΪ500Hz  
 }else return 1;
 res=MPU_Read_Byte(AK8963_ADDR,MAG_WIA);       //¶ÁÈ¡AK8963 ID  
 if(res==AK8963_ID)
 {
   MPU_Write_Byte(AK8963_ADDR,MAG_CNTL1,0X11);  //ÉèÖÃAK8963Ϊµ¥´Î²âÁ¿Ä£Ê½
 }else return 1;
 return 0;
}  设备ID读不出来