为 Devantech 数字罗盘设置 i2c 从机地址

发布于 2024-12-15 19:06:03 字数 1850 浏览 2 评论 0原文

我正在尝试与此处找到的 Devantech 数字罗盘进行交互 -

http://www. acroname.com/robotics/parts/R117-COMPASS.html

我正在使用 i2c-usb 转换器将其插入我的笔记本电脑 -

http://www.robot- electronics.co.uk/htm/usb_i2c_tech.htm

首先,我对电气工程了解不多。我对最基本的知识有很好的了解,但在那之后我就迷失了。

我正在尝试遵循本教程 -

https: //xgoat.com/wp/2007/11/11/using-i2c-from-userspace-in-linux/

但是,当我尝试设置设备地址时,我一开始就陷入困境。

if( ioctl( fd, I2C_SLAVE, ADDRESS ) < 0 )
    {
            fprintf( stderr, "Failed to set slave address: %m\n" );
            return 2;
    }

返回“无法设置从机地址:无效参数”

我最初认为地址应该是 0xC0,因为罗盘手册中的一句话是“首先发送起始位,模块地址(0xC0)...”,但事实并非如此工作。

现在我有一个从 1 到 100 的循环,并尝试每一个地址,但它们都失败了。循环是 -

for(int i=0x0;i<0x100;i++) {
    if( ioctl( fd, I2C_SLAVE, i ) < 0 )
        fprintf( stderr, "Failed to set slave address for address %i: %m\n", i );
}

我不知道还能尝试什么。现在,我只想设置地址,以便我可以开始尝试读写。由于转换器实际上是连接到电脑的,我应该使用该地址吗?如果是这样,我在哪里可以找到该链接及其信息?如果有人知道我可以尝试什么或出了什么问题,那就太好了。

编辑:

好的,我现在有这样的代码 -

#define ADDRESS 0x55
int fd = open("/dev/i2c-0", O_RDWR);

if (fd < 0) {
    printf("\n<0, %m", errno);
    return -1;
}

if( ioctl( fd, I2C_SLAVE, ADDRESS ) < 0 ) {
    fprintf( stderr, "Failed to set slave address: %m\n" );
    return 2;
}

if( i2c_smbus_write_byte( fd, 0xAA ) < 0 )
    fprintf( stderr, "Failed to write 0xAA to I2C device: %m\n" );

它将设置地址,但不会写入任何内容。每当我尝试写信时,我都会得到——

Failed to write 0xAA to I2C device: No such device or address 

I am trying to interface with a Devantech digital compass found here -

http://www.acroname.com/robotics/parts/R117-COMPASS.html

I am using a i2c-usb converter to plug it into my laptop -

http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm

First of all, I do not know much about electrical engineering. I have a good idea of the bare basics, but after that I get lost.

I am trying to follow this tutorial -

https://xgoat.com/wp/2007/11/11/using-i2c-from-userspace-in-linux/

However I get stuck at the very beginning when I try to set the device address.

if( ioctl( fd, I2C_SLAVE, ADDRESS ) < 0 )
    {
            fprintf( stderr, "Failed to set slave address: %m\n" );
            return 2;
    }

returns "Failed to set slave address: Invalid argument"

I originally thought the address should be 0xC0 because a sentence in the manual for the compass reads "First send a start bit, the module address (0xC0)..." but that did not work.

Now I have a loop that just goes from 1 to 100 and tries each one for the address, but they all fail. The loop is -

for(int i=0x0;i<0x100;i++) {
    if( ioctl( fd, I2C_SLAVE, i ) < 0 )
        fprintf( stderr, "Failed to set slave address for address %i: %m\n", i );
}

I'm not sure what else to try. Right now, I just want to set the address so I can start attempting to read and write. Since the converter is what is actually connected to the pc, should I be using the address for that? And if so, where can I find it on that link with the information for it? If someone has an idea of what I could try or what is wrong that would be great.

EDIT:

Okay I have the code like this now -

#define ADDRESS 0x55
int fd = open("/dev/i2c-0", O_RDWR);

if (fd < 0) {
    printf("\n<0, %m", errno);
    return -1;
}

if( ioctl( fd, I2C_SLAVE, ADDRESS ) < 0 ) {
    fprintf( stderr, "Failed to set slave address: %m\n" );
    return 2;
}

if( i2c_smbus_write_byte( fd, 0xAA ) < 0 )
    fprintf( stderr, "Failed to write 0xAA to I2C device: %m\n" );

It will set the address, but it won't write anything. Whenever I try to write to it, I get -

Failed to write 0xAA to I2C device: No such device or address 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2024-12-22 19:06:03

您为什么尝试将 0xAA 发送到设备?
据我了解,这不是它的寄存器(对于CMPS03,唯一的命令是寄存器15,它的用法并不常见:更改I2C代码>地址,恢复出厂设置,...)。
而且 0x55 似乎绝对不是设备的地址... 0xC0 应该是写入地址。

可能是 /dev/i2c-0 尚未正确创建/不正确?
您是如何创建 /dev/ic2-0 的?

Why are your trying to send 0xAA to the device ?
To my understanding this is not a register for it (for the CMPS03, the only command is register 15 and its usage is not common: change the I2C address, factory reset, ...).
And 0x55 seems definitively not the address of the device ... 0xC0 should be the write one.

Could be that /dev/i2c-0 has not been created properly / is not correct?
How did you get /dev/ic2-0 created ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文