Other Parts Discussed in Thread: TMS570LS0714
我正在尝试通过 I2C 通信与 PCB 与具有 Arduino 的 TMS570LS0714进行通信。 Arduino 是从属设备、而我的 PCB 是主设备。
我编写了一个函数以写入从器件(Arduino):
void write_slave (int address、int buffer){ /*配置要与*/通话的从机地址 i2cSetSlaveAdd (i2cREG1、地址); //配置数据计数* /*注:可选-在 Init 中完成,除非用户想要更改*/ //i2cSetCount (i2cREG1、data_count_I2C); /*将模式设置为主设备*/ i2cSetMode (i2cREG1、I2C_MASTER); /*设置编程计数后停止*/ i2cSetStop (i2cREG1); /*发送开始条件*/ i2cSetStart (i2cREG1); /*轮询模式下的 Transmit data_count 数据数量*/ i2cSend (i2cREG1,data_count_I2C,buffer>>24); i2cSend (i2cREG1,data_count_I2C,buffer>>16); i2cSend (i2cREG1,data_count_I2C,buffer>>8); i2cSend (i2cREG1、DATA_COUNT_I2C、缓冲器); /*等待总线忙被清除*/ while (i2cIsBusy (i2cREG1)=true); /*等待直到检测到停止*/ while (i2cIsStopDetected (i2cREG1)=0); /*清除停止条件*/ i2cClearSCD (i2cREG1); }
其中:
//引脚排列 I2C
#define SLAVE_WRITE_ADDR 0xA0
#define SLAVE_READ_ADDR 0xA1
#define DATA_COUNT_I2C 8.
Arduino 中的代码仅用于将数据发送到 Labvew 界面:
#include
uint8_t data;
int nByte = 0;
byte lectura [4];
unsigned long valor = 0;
void setup ()
{
Wire.begin(0x50); //使用地址#4
Wire.onReceive (receiveEvent)加入 i2c 总线;//注册事件
Serial.begin(9600); //开始序列输出
}
void loop()
{
delay(100);
}//
从主控方接收数据时执行的函数
//此函数被注册为事件,请参阅 setup()
void receiveEvent(int howMiny)
{
while (Wire.Available ()>0){//循环遍历除最后一个以外的所有内容
数据= Wire.read();//接收作为字符的字节
lectura[nByte]=数据;
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);
}
}
当我调用函数"write_slave"时、代码跳转到具有无限循环的 i2c.c 函数。 为什么会发生这种情况? 它找不到从器件(Arduino)?

