我正在尝试将 I2C 与 beaglebone 一起使用,但我不能写入超过 1 个字节
I'm trying to use I2C with beaglebone but I cannot write more than 1 byte
我正在尝试使用 Beaglebone Black 控制 DAC5571。我的内核版本是:
Linux beaglebone 4.14.108-ti-r137 #1stretch SMP PREEMPT Tue Aug 25 01:48:39 UTC 2020 armv7l GNU/Linux
我可以部分控制DAC IC。可以看到here,需要发送3个字节;从地址,CTRL/MSB 和 LSB。 IC 识别从地址字节和 CTRL/MSB。我可以读取并确认输出引脚的输出。但是当我开始缓慢增加电压值时,Vout += 0.05,输出增加为 0.2、0.4、0.6 等...
我已经用我的示波器检查过,我可以确认第三个字节被传输为 0x00,不管它的实际值是多少。
这是我的源代码:
int DAC5571::writeI2CDeviceByte(int value)
{
cout << "Starting DAC5571 I2C sensor state write" << endl;
char namebuf[MAX_BUS];
snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", I2CBus);
int file;
if ((file = open(namebuf, O_RDWR)) < 0) {
cout << "Failed to open DAC5571 Sensor on " << namebuf << " I2C Bus" << endl;
return(1);
}
if (ioctl(file, I2C_SLAVE, I2CAddress) < 0) {
cout << "I2C_SLAVE address " << I2CAddress << " failed..." << endl;
return(2);
}
int buffer[2];
buffer[0] = value>>8;
buffer[1] = value & 0xFF;
cout << "buffer [0] is " << buffer[0] << endl;
cout << "buffer [1] is " << buffer[1] << endl;
if (write(file, buffer, 2) != 2) {
cout << "Failure to write values to I2C Device address." << endl;
return(3);
}
close(file);
cout << "Finished DAC5571 I2C sensor state write" << endl;
return 0;
}
控制台输出如下:
Starting DAC5571 I2C sensor state write
buffer [0] is 3
buffer [1] is 128
Finished DAC5571 I2C sensor state write
我在研究中看到有一个名为“i2c-core.h”的头文件,但我无法将其包含到具有块写入功能的项目中。不确定这对我的情况是否有帮助。
谁能帮我解决无法传输 LSB 部分数据的问题?
谢谢。
int buffer[2];
buffer[0] = value>>8;
buffer[1] = value & 0xFF;
if ( write(file, buffer, 2) != 2) { ... }
buffer
的元素是 int
类型,长度为 4 个字节。所以当你写长度为2
的buffer
时,你写了2个字节,也就是整数buffer[0]
的前两个字节。在你的例子中 buffer[0]
是 3
,所以因为这台机器是小端的,它由字节 03 00 00 00
组成,你写出 03 00
.
您可能需要 unsigned char buffer[2];
或者 uint8_t buffer[2];
,这样每个元素都是一个字节。
我正在尝试使用 Beaglebone Black 控制 DAC5571。我的内核版本是:
Linux beaglebone 4.14.108-ti-r137 #1stretch SMP PREEMPT Tue Aug 25 01:48:39 UTC 2020 armv7l GNU/Linux
我可以部分控制DAC IC。可以看到here,需要发送3个字节;从地址,CTRL/MSB 和 LSB。 IC 识别从地址字节和 CTRL/MSB。我可以读取并确认输出引脚的输出。但是当我开始缓慢增加电压值时,Vout += 0.05,输出增加为 0.2、0.4、0.6 等...
我已经用我的示波器检查过,我可以确认第三个字节被传输为 0x00,不管它的实际值是多少。
这是我的源代码:
int DAC5571::writeI2CDeviceByte(int value)
{
cout << "Starting DAC5571 I2C sensor state write" << endl;
char namebuf[MAX_BUS];
snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", I2CBus);
int file;
if ((file = open(namebuf, O_RDWR)) < 0) {
cout << "Failed to open DAC5571 Sensor on " << namebuf << " I2C Bus" << endl;
return(1);
}
if (ioctl(file, I2C_SLAVE, I2CAddress) < 0) {
cout << "I2C_SLAVE address " << I2CAddress << " failed..." << endl;
return(2);
}
int buffer[2];
buffer[0] = value>>8;
buffer[1] = value & 0xFF;
cout << "buffer [0] is " << buffer[0] << endl;
cout << "buffer [1] is " << buffer[1] << endl;
if (write(file, buffer, 2) != 2) {
cout << "Failure to write values to I2C Device address." << endl;
return(3);
}
close(file);
cout << "Finished DAC5571 I2C sensor state write" << endl;
return 0;
}
控制台输出如下:
Starting DAC5571 I2C sensor state write
buffer [0] is 3
buffer [1] is 128
Finished DAC5571 I2C sensor state write
我在研究中看到有一个名为“i2c-core.h”的头文件,但我无法将其包含到具有块写入功能的项目中。不确定这对我的情况是否有帮助。
谁能帮我解决无法传输 LSB 部分数据的问题?
谢谢。
int buffer[2];
buffer[0] = value>>8;
buffer[1] = value & 0xFF;
if ( write(file, buffer, 2) != 2) { ... }
buffer
的元素是 int
类型,长度为 4 个字节。所以当你写长度为2
的buffer
时,你写了2个字节,也就是整数buffer[0]
的前两个字节。在你的例子中 buffer[0]
是 3
,所以因为这台机器是小端的,它由字节 03 00 00 00
组成,你写出 03 00
.
您可能需要 unsigned char buffer[2];
或者 uint8_t buffer[2];
,这样每个元素都是一个字节。