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.
大家好、我正在尝试 通过 I2C 与 Launchpad LAUNCHXL2-TMS57012通信。 代码很简单、LaunchPad 只需接收并绘制接收的内容;它与 PC 连接、通过 LabVIEW、我可以看到结果。 我已经尝试过与 Arduino 的通信、它工作正常。 使用 launchpad、我无需进行通信。 我的问题是、在 Arduino 中、我将使用2个上拉电阻进行 I2C 通信;Launchpad 似乎尚未上拉、是否正确或是否应使用上拉电阻?
我使用的代码为:
/* Include Files */ #include "sys_common.h" /* USER CODE BEGIN (1) */ #include "i2c.h" /* USER CODE END */ /** @fn void main(void) * @brief Application main function * @note This function is empty by default. * * This function is called after startup. * The user can use this function to implement the application. */ /* USER CODE BEGIN (2) */ #define DATA_COUNT 8 #define Slave_Address 0x50 //uint8_t TX_Data_Master[10] = { 0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19}; //uint8_t RX_Data_Master[10] = { 0 }; //uint8_t TX_Data_Slave[10] = { 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29}; uint8_t RX_Data_Slave[8] = { 0 }; /* USER CODE END */ void main(void) { /* USER CODE BEGIN (3) */ int repeat = 0; /////////////////////////////////////////////////////////////// // Slave Receiver Functionality // /////////////////////////////////////////////////////////////// /* I2C Init as per GUI * Mode = Slave - Receiver * baud rate = 100KHz * Count = 10 * Bit Count = 8bit */ i2cInit(); /* Configure own address so that master can use it as slave address */ i2cSetOwnAdd(i2cREG1, Slave_Address); /* Set direction to receiver */ /* Note: Optional - It is done in Init */ i2cSetDirection(i2cREG1, I2C_RECEIVER); for(repeat = 0; repeat < 2; repeat++) { /* Configure Data count */ /* Note: Optional - It is done in Init, unless user want to change */ i2cSetCount(i2cREG1, DATA_COUNT); /* Tranmit DATA_COUNT number of data in Polling mode */ i2cReceive(i2cREG1, DATA_COUNT, RX_Data_Slave); /* Wait until Bus Busy is cleared */ while(i2cIsBusBusy(i2cREG1) == true); /* Wait until Stop is detected */ while(i2cIsStopDetected(i2cREG1) == 0); /* Clear the Stop condition */ i2cClearSCD(i2cREG1); } fprintf("%c", RX_Data_Slave); while(1); }
同时、与 Arduino 一起使用的有效代码为:
#include <Wire.h> uint8_t data; int nByte = 0; byte lectura[4]; unsigned long valor = 0; void setup() { Wire.begin(0x50); // join i2c bus with address #4 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); // start serial for output } void loop() { delay(100); } // function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int howMany) { while (Wire.available() > 0){ // loop through all but the last data = Wire.read(); // receive byte as a character lectura[nByte]=data; nByte++; if(nByte % 4 == 0){ nByte = 0; valor = (unsigned long)lectura[3] + ((unsigned long)lectura[2]<<8) + ((unsigned long)lectura[1]<<16) + ((unsigned long)lectura[0]<<24); Serial.println(valor); } } }
不可以、该 LaunchPad 在 I2C 信号上不包含上拉电阻器。
这个器件(TMS570LS1224PGE)有一个 I2C 模块、但是 I2C 信号(引脚3和引脚4)与 MibSPI 引脚复用。 请正确配置引脚多路复用器、以将这两个引脚用作 I2C。
我已将这些引脚配置为 I2C、并将 Launchpad 中的 J11引脚8和9用作 SCL 和 SDA、其中具有2个3.3V 上拉电阻。它仍然不工作。 使用示波器、我只能看到 SCL 和 SDA 线路上的3.3V 电压。 当我运行调试时、它会正常工作、直到出现以下行: i2cReceive (i2cREG1、data_count、RX_Data_Slave);在此行中、它会在 i2c.c 中持续一段时间、即:
while (((i2c->STR &(UINT32) I2C_RX_INT)== 0U)
{
}/*等待*/
并留在那里。 问题可能出在哪?
它正在等待接收到的数据。 请查看从器件数据表、以确保您的代码遵循其通信协议。