求助啊!I2C和MPU6050一直出不来结果

2019-07-21 18:49发布

这下面是我的程序    求帮我看看啊



void i2cinit()
{
        SysCtlPeripheralEnable(SYSCTL_PERIPH_I2C0);
  GPIOPinConfigure(GPIO_PB2_I2C0SCL);
  GPIOPinConfigure(GPIO_PB3_I2C0SDA);
        GPIOPinTypeI2CSCL(GPIO_PORTB_BASE, GPIO_PIN_2);
  GPIOPinTypeI2C(GPIO_PORTB_BASE, GPIO_PIN_3);
        GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_2,GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_STD_WPU);
  GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_3,GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_OD);
        I2CMasterInitExpClk(I2C0_BASE, SysCtlClockGet(), false);
        I2CMasterEnable(I2C0_BASE);
}
void write(uint8_t DevAddr,uint8_t RegAddr,uint8_t writedata)
{
        I2CMasterSlaveAddrSet(I2C0_BASE, DevAddr, false);
        I2CMasterDataPut(I2C0_BASE,RegAddr);
        while(I2CMasterBusy(I2C0_BASE)){}
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
        while(I2CMasterBusy(I2C0_BASE)){}
  I2CMasterDataPut(I2C0_BASE, writedata);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
  while(I2CMasterBusy(I2C0_BASE)){}
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_FINISH);
        i2cinit();
}
uint8_t read(uint8_t DevAddr,uint8_t RegAddr)
{
int data;
        I2CMasterSlaveAddrSet(I2C0_BASE, DevAddr, false);
        I2CMasterDataPut(I2C0_BASE,RegAddr);
        while(I2CMasterBusy(I2C0_BASE)){}
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_SEND_START);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
        while(I2CMasterBusy(I2C0_BASE)){}
        I2CMasterSlaveAddrSet(I2C0_BASE, DevAddr, true);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_START);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_RECEIVE);
  while(I2CMasterBusy(I2C0_BASE)){}
        data= I2CMasterDataGet(I2C0_BASE);
        I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_FINISH);
        i2cinit();
        return data;
}
void mpuinit()
{
        SysCtlDelay(100* (SysCtlClockGet() / 3000));
        write(0x50,PWR_MGMT_1, 0x80);
        SysCtlDelay(100* (SysCtlClockGet() / 3000));
        write(0x50,PWR_MGMT_1, 0x01);
        write(0x50,SMPLRT_DIV, 0x07);
        write(0x50,CONFIG, 0x06);
        write(0x50,GYRO_CONFIG, 0x18);
        write(0x50,ACCEL_CONFIG, 0x00);
}
int
main(void)
{
        init();
        i2cinit();
        InitConsole();
        mpuinit();
        while(1){
        UARTprintf("%o ",read(0x50,0x68));
  delay(500);}}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。