Thread 中讨论的其他器件:CC2650
工具/软件:TI-RTOS
您好!
目前、我正在尝试通过 I2C 与多路复用器传感器(MAX44009)进行通信。 我将传感器 SCL 连接到 GPIO4、将 SDA 连接到 GPIO5 (在 CC2650_LAUNCHXL.h 文件中进行了类似定义)。 此外、我还将 A0引脚从 MAX44009连接到 GND、以确定从器件地址。 现在、根据传感器的规格、从器件地址应为
0b10010100用于写入
0b10010101用于读取。
此外、它还为我提供了一个有关如何读取特定寄存器的示例:
我的代码现在如下所示:
uint16_t 温度; uint8_t txBuffer[1]; uint8_t rxBuffer[1]; I2C_Handle I2C; I2C_Params i2cParams; I2C_Transaction i2cTransaction; uint8_t 一; /*创建 I2C 以供使用*/ I2C_Params_init (&i2cParams); i2cParams.bitrate = I2C_400kHz; I2C = I2C_open (0、&i2cParams); if (i2c == NULL){ UartDebug_Print (中、"Error Initializing I2C\n"); } 否则{ UartDebug_Print (中、"I2C initialized!\n"); } TxBuffer[0]= 0x04; i2cTransaction.slaveAddress = 0b10010100; i2cTransaction.writeBuf = txBuffer; i2cTransaction.writeCount = 1; i2cTransaction.readBuf = rxBuffer; i2cTransaction.ReadCount = 0; if (I2C_transfer (i2c、&i2cTransaction)){ UartDebug_Print (介质、"I2C 写入成功\n"); } 否则{ UartDebug_Print (中、"I2C 总线故障写入"\n); } TxBuffer[0]= 0x04; rxBuffer[0]= 0x00; i2cTransaction.slaveAddress = 0b10010101; i2cTransaction.writeBuf = txBuffer; i2cTransaction.writeCount = 0; i2cTransaction.readBuf = rxBuffer; i2cTransaction.ReadCount = 1; /*采集20个样本并将其打印到控制台*/ 对于(i = 0;i < 1;i++){ if (I2C_transfer (i2c、&i2cTransaction)){ 温度= rxBuffer[0]; UartDebug_Print (medium、"Sample %u:%d (C)\n"、i、temperature); } 否则{ UartDebug_Print (中、"I2C 总线故障读取"\n); } } /*已取消初始化 I2C */ I2C_Close (i2c); UartDebug_Print (中、"I2C Closed!\n"\});
我的输出现在为:
UART_Log [level:medium] I2C 已初始化! UART_Log [级别:中等] I2C 总线故障写入 UART_Log [级别:中等] I2C 总线故障读取 UART_Log [级别:中等] I2C 闭合!
我不确定我在这里做什么错了。 我首先将寄存器地址写入传感器、然后尝试读回一个字节。 我处理 I2C_Transfer 函数是否错误? 当我想只读/写时、我可以将 ReadCount/writeCount 设置为零、还是应该使用专用的读/写命令?
感谢您的任何帮助!
谢谢
Chris
PS:我在 CC2650 SensorTag I2C 示例中采用了这种方法!



