大家好、我正在尝试 通过 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);
}
}
}