简体   繁体   English

在 Beaglebone 上使用 I2C 需要一些帮助

[英]Need some help using I2C on Beaglebone

I'm a beginner at I2C communication and I am trying to hook up communication with an I2C device a (mpu6050) using librobotcontrol http://strawsondesign.com/docs/librobotcontrol/group___i2_c.html i2c library in C. The code Excluding stuff not I2C related I have:我是 I2C 通信的初学者,我正在尝试使用 librobotcontrol http://strawsondesign.com/docs/librobotcontrol/group___i2_c.html C中的 i2c 库与 I2C 设备 a (mpu6050) 进行通信。代码排除内容与 I2C 无关我有:

#define I2C_ADDRESS 0x68
#define I2C_BUS 1

void setupMPU();
float recordAccelRegisters();

int main(){

    float ax = 0;
    while(1){
        ax = recordAccelRegisters()
    }

    float recordAccelRegisters(){
        float Xg = 0;
        float Xa = 0, Ya, Za;
        unsigned char data;

        printf("Recording Accel Data\n");

        rc_i2c_init (I2C_BUS,I2C_ADDRESS);
        rc_i2c_read_bytes(I2C_BUS, 0x3B, 6, data);
        rc_i2c_close(I2C_BUS);

        //Xa = 8 << data;
        //Xa = (float)Xa;
        //Xg = Xa/16384.0;
        return Xg;
        }

    void setupMPU(){
        printf("Initalizing Mpu\n");

        // init I2C Bus
        rc_i2c_init (I2C_BUS,I2C_ADDRESS);

        //Wake up setup
        rc_i2c_write_byte(I2C_BUS, 0x6B, 0x00);
        rc_i2c_close(I2C_BUS);

        printf("Configuring Accelerometer\n");
        printf("Initalizing Mpu\n");

        //Accel Config
        rc_i2c_init (I2C_BUS,I2C_ADDRESS);
        rc_i2c_write_byte(I2C_BUS, 0x1C, 0x00);

        printf("Done with Accel Config\n");
        rc_i2c_close(I2C_BUS);
        }
   }

I am trying to figure out if I need to close and reopen the port every time like in an Arduino using wire library or if I can just initialize it once.我想弄清楚我是否需要像在 Arduino 中那样使用线库每次都关闭并重新打开端口,或者我是否可以只初始化一次。 I have tried many different types of code configurations.我尝试了许多不同类型的代码配置。 For instance only initializing the communication only once, using send words instead of send bytes, sending only 1 byte at a time ect... but I am still unable to get data from the device.例如只初始化通信一次,使用发送字而不是发送字节,一次只发送 1 个字节等......但我仍然无法从设备获取数据。 I am also struggling with weather or not to use read words or read bytes.我也在为天气而苦苦挣扎,或者不使用读取的单词或读取的字节。 It keeps returning a -1 bytes as seen by this error message when using它在使用时不断返回一个 -1 字节,正如这个错误消息所看到的

read bytes:读取字节:

ERROR: in rc_i2c_read_bytes, received -1 bytes from device, expected 1

and this error when using read words:使用读词时出现此错误:

ERROR: Segmentation Fault
Fault address: (nil)
Address not mapped.
Segmentation fault

Any help on this will be much appreciated.对此的任何帮助将不胜感激。

According to the documentation you linked, the function rc_i2c_read_bytes takes a pointer where it will store the bytes read:根据您链接的文档, function rc_i2c_read_bytes采用一个指针来存储读取的字节:

int rc_i2c_read_bytes (int bus, uint8_t regAddr, size_t count, uint8_t *data);
Reads multiple bytes from a device register

Thus, in your recordAccelRegisters function, you should define an array to store the bytes:因此,在您的recordAccelRegisters function 中,您应该定义一个数组来存储字节:

float recordAccelRegisters(){
        float Xg = 0;
        float Xa = 0, Ya, Za;
        unsigned char data[6];

        printf("Recording Accel Data\n");

        rc_i2c_init (I2C_BUS,I2C_ADDRESS);
        rc_i2c_read_bytes(I2C_BUS, 0x3B, 6, data);
        rc_i2c_close(I2C_BUS);

        //Xa = 8 << data;
        //Xa = (float)Xa;
        //Xg = Xa/16384.0;
        return Xg;
}

Same applies if you use rc_i2c_read_word , it also requires a pointer (to a uint16_t ):如果您使用rc_i2c_read_word ,同样适用,它还需要一个指针(指向uint16_t ):

float recordAccelRegisters(){
        float Xg = 0;
        float Xa = 0, Ya, Za;
        uint16_t data;

        printf("Recording Accel Data\n");

        rc_i2c_init (I2C_BUS,I2C_ADDRESS);
        rc_i2c_read_word(I2C_BUS, 0x3B, &data);
        rc_i2c_close(I2C_BUS);

        //Xa = 8 << data;
        //Xa = (float)Xa;
        //Xg = Xa/16384.0;
        return Xg;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM