如何使用I2C

时间:2018-02-28 14:48:41

标签: c accelerometer i2c

我一直试图用加速度计读取加速度值,但我继续读取接近0的值。我无法读取它通常应该阅读的1G加速度。

寄存器:

#define ACCEL_CTRL_REG1     0x20
#define ACCEL_CTRL_REF      0x26
#define ACCEL_OUT_X_L       0x28
#define ACCEL_FIFO_CTRL     0x2E
#define ACCEL_INT1_CFG      0x30
#define ACCEL_INT1_SRC      0x31
#define ACCEL_INT1_THS      0x32
#define ACCEL_INT1_DURATION 0x33
#define ACCEL_INT2_CFG      0x34
#define ACCEL_INT2_SRC      0x35
#define ACCEL_INT2_THS      0x36
#define ACCEL_INT2_DURATION 0x37
#define ACCEL_CLICK_CFG     0x38
#define ACCEL_CLICK_SRC     0x39
#define ACCEL_CLICK_THS     0x3A

配置:

//Start accel
write_buffer[0] = ACCEL_CTRL_REG1 | 0x80; // Register | Autoincrement ON
write_buffer[1] = (accel_range << 4) | 0x0F; // Value: 50Hz | Low power | enable all axis
write_buffer[2] = 0x09; // Value: INT1 Filter
write_buffer[3] = 0x40; // Value: Enable IA interrupt on INT1
write_buffer[4] = 0x00; // Value:
write_buffer[5] = 0x48; // Value: Enable FIFO | latch Int1
write_buffer[6] = 0x00; // Value: INT1
ret = Ql_IIC_Write(1, ACCEL_ADDRESS, write_buffer,7);

//Setup movent interrupt
write_buffer[0] = ACCEL_INT1_THS | 0x80; // Register | Autoincrement ON
write_buffer[1] = thr; // Threshold
write_buffer[2] = dur; // Duration
ret = Ql_IIC_Write(1, ACCEL_ADDRESS, write_buffer,3);

//Dummy read to reset HP filter
registerAdrr = ACCEL_CTRL_REF;
Ql_IIC_Write_Read(1, ACCEL_ADDRESS, &registerAdrr, 1,read_buffer, 1);

//Enable interrupt
write_buffer[0] = ACCEL_INT1_CFG; // Register
write_buffer[1] = 0x2A; // Enable High on Z Y X
ret = Ql_IIC_Write(1, ACCEL_ADDRESS, write_buffer,2);

write_buffer[0] = ACCEL_FIFO_CTRL; // Register
write_buffer[1] = 0xC0; // Stream-to-FIFO mode
ret = Ql_IIC_Write(1, ACCEL_ADDRESS, write_buffer,2);

此外,我必须在捕获中断后重置中断和FIFO

u8 registerAdrr[1]={ACCEL_INT1_SRC};
ret = Ql_IIC_Write_Read(1, ACCEL_ADDRESS, registerAdrr, 1,read_buffer, 1);

read_buffer[0] = ACCEL_FIFO_CTRL; // Register
read_buffer[1] = 0x00; // Bypass FIFO mode
ret = Ql_IIC_Write(1, ACCEL_ADDRESS, read_buffer,2);
read_buffer[0] = ACCEL_FIFO_CTRL; // Register
read_buffer[1] = 0xC0; // Stream-to-FIFO mode
ret = Ql_IIC_Write(1, ACCEL_ADDRESS, read_buffer,2);

这是我用来读取值的代码:

#define ACCEL_ADDRESS 0x30
s32 ret = 0;
s8 read_buffer[200];
u8 registerAdrr[1]={ACCEL_OUT_X_L | 0x80}; // Autoincrement ON
u8 i;

Ql_memset(read_buffer, 0, sizeof(read_buffer));

for (i=0; i<32; i++)
{
    ret = Ql_IIC_Write_Read(1, ACCEL_ADDRESS, registerAdrr, 1, &read_buffer[i*6], 6);
    if(ret < 0)
    {
        // Error
        APP_DEBUG("\r\n<--Failed !! IIC controller Ql_IIC_Write_Read channel 1 fail ret=%d-->\r\n",ret);
        return -1;
    }

    APP_DEBUG("x:%d, y:%d, z:%d\r\n", (read_buffer[0+i*6]<<8) + read_buffer[1+i*6], (read_buffer[2+i*6]<<8) + read_buffer[3+i*6], (read_buffer[4+i*6]<<8) + read_buffer[5+i*6]);
}

这是我得到的价值观。我只是把它放在我的桌子上,所以它应该显示重力矢量,但它只是在站立时得到随机数字:

[2018-02-28_12:47:05:137]Movement detected
[2018-02-28_12:47:05:137]x:0, y:-1, z:-1
[2018-02-28_12:47:05:137]x:0, y:-1, z:-1
[2018-02-28_12:47:05:137]x:0, y:0, z:0
[2018-02-28_12:47:05:137]x:0, y:-1, z:0
[2018-02-28_12:47:05:137]x:0, y:0, z:-1
[2018-02-28_12:47:05:137]x:-1, y:-1, z:0
[2018-02-28_12:47:05:137]x:-1, y:0, z:-1
[2018-02-28_12:47:05:137]x:-1, y:0, z:0
[2018-02-28_12:47:05:137]x:-1, y:0, z:-1
[2018-02-28_12:47:05:137]x:-1, y:-1, z:-1
[2018-02-28_12:47:05:137]x:-1, y:0, z:0
[2018-02-28_12:47:05:137]x:0, y:0, z:-1
[2018-02-28_12:47:05:137]x:-1, y:-1, z:0
[2018-02-28_12:47:05:137]x:0, y:0, z:-1
[2018-02-28_12:47:05:137]x:-1, y:0, z:0
[2018-02-28_12:47:05:164]x:0, y:0, z:0
[2018-02-28_12:47:05:164]x:0, y:0, z:-1
[2018-02-28_12:47:05:164]x:0, y:1, z:-1
[2018-02-28_12:47:05:164]x:-1, y:-1, z:0
[2018-02-28_12:47:05:164]x:0, y:0, z:-1
[2018-02-28_12:47:05:164]x:-1, y:0, z:0
[2018-02-28_12:47:05:164]x:0, y:0, z:-1
[2018-02-28_12:47:05:164]x:0, y:0, z:-1
[2018-02-28_12:47:05:164]x:-1, y:0, z:-1
[2018-02-28_12:47:05:164]x:0, y:0, z:0
[2018-02-28_12:47:05:164]x:-1, y:0, z:-1
[2018-02-28_12:47:05:164]x:-1, y:0, z:1
[2018-02-28_12:47:05:164]x:0, y:-1, z:0
[2018-02-28_12:47:05:164]x:-112, y:96, z:-117
[2018-02-28_12:47:05:164]x:15, y:43, z:121
[2018-02-28_12:47:05:191]x:-100, y:-73, z:105
[2018-02-28_12:47:05:191]x:-64, y:-53, z:92

LIS2DH的数据表: http://www.st.com/content/ccc/resource/technical/document/datasheet/c1/e1/62/31/d2/b1/4d/bb/DM00042751.pdf/files/DM00042751.pdf/jcr:content/translations/en.DM00042751.pdf

非常感谢你的时间。

0 个答案:

没有答案