您好!
I2C 在初始上电时不会读取寄存器、但如果我进行了一次调试、它会在接下来的所有运行中按预期工作。
我使用的是 CCSv8.1、Windows 10、EK - TM4C123GXL
I2C0通道用于 I2C 通信。
I2C0 SCL - PB2
I2C0 SDA - PB3
我在此附上了我的代码片段:
/*检查 I2C SDA 是否未保持低电平如果是,则切换 I2C SCL 直到它被释放 *按下"Reset"按钮时可安全进行 i2c 通信*/ GPIOPinTypeGPIOInput (GPIO_PORTB_BASE、GPIO_PIN_3); GPIOPinTypeGPIOOutput (GPIO_PORTB_BASE、GPIO_PIN_2); while (GPIOPinRead (GPIO_PORTB_BASE、GPIO_PIN_3)!= GPIO_PIN_3) { GPIOPinWrite (GPIO_PORTB_BASE、GPIO_PIN_2、0); GPIOPinWrite (GPIO_PORTB_BASE、GPIO_PIN_2、GPIO_PIN_2); }
初始化 I2C:
/* I2C0 Init */ /*启用外设*/ SysCtlPeripheralEnable (SYSCTL_Periph_I2C0); //为 I2C0SCL 的 PB2配置 GPIO 引脚多路复用器 GPIOPinConfigure (GPIO_PB2_I2C0SCL); GPIOPinTypeI2CSCL (GPIO_PORTB_BASE、GPIO_PIN_2); //为 I2C0SDA 的 PB3配置 GPIO 引脚多路复用器 GPIOPinConfigure (GPIO_PB3_I2C0SDA); GPIOPinTypeI2C (GPIO_PORTB_BASE、GPIO_PIN_3);
我将 TI-RTOS 2.16.00.08用于配置如下的项目、作为任务从 IMU 传感器获取数据:
void get_IMU()
{
I2C_Handle i2c;
I2C_Params i2cParams;
uint8_t 电源;
PWR = 0x00;
//创建 I2C 以供使用
I2C_Params_init (&i2cParams);
i2cParams.bitrate = I2C_400kHz;
I2C = I2C_open (IMU_sensor、&i2cParams);
if (i2c == NULL)
{
System_abort ("初始化 I2C\n 时出错");
}
其他
{
system_printf ("I2C 已初始化!\n");
}
//初始化 MPU6050
writeRegister (i2c、0x6B、0x80、true);
读寄存器(i2c、0x6B、&pwr、1、true);
操作
{
读寄存器(i2c、0x6B、&pwr、1、true);
}
while (pwr & 0x40!= 0x40);
//使用具有 X 轴陀螺仪基准的 PLL
writeRegister (i2c、0x6B、0x01、true);
//启用 I2C 主机模式
writeRegister (i2c、0x6A、0x20、true);
//设置采样率分频器
writeRegister (i2c、0x19、0x13、true);
writeRegister (i2c、0x67、0x11、true);
while (1)
{
Semaphore_pend (IMUSem、BIOS_wait_forever);
readRegister (i2c、0x3B、(uint8_t *)&signal.imu.mpu6050、14、true);
}
//取消初始化 I2C
I2C_Close (i2c);
System_printf ("I2C 已关闭!\n"\});
system_flush();
}
此致、
Yashwanth Kumar Gandeti。



