I2C

20 stm32 软件i2c连续写寄存器抓包时出现问题,有几个的从机地址发生错误

发布于 2020-09-15 11:35:10

利用rtt_studio开发,读取寄存器的时候正常,
代码如下,

/* 写传感器寄存器 */
static rt_err_t write_reg(struct rt_i2c_bus_device *bus, rt_uint8_t reg, rt_uint32_t data)
{
    rt_uint8_t buf[4];
    struct rt_i2c_msg afe_msgs;

    buf[0] = reg;
    buf[1] = (rt_uint8_t)data>>16;
    buf[2] = (rt_uint8_t)data>>8;
    buf[3] = (rt_uint8_t)data;


    afe_msgs.addr = AFE_ADDR;
    afe_msgs.flags = RT_I2C_WR;
    afe_msgs.buf = buf;
    afe_msgs.len = 4;

    /* 调用I2C设备接口传输数据 */
    if (rt_i2c_transfer(bus, &afe_msgs, 1) == 1)
    {
        rt_thread_mdelay(30);
        return RT_EOK;
    }
    else
    {
        return -RT_ERROR;
    }
}

//如下为写的代码
        write_reg(i2c_bus,0x01,0x0000002B); /*LED2STC*/
        write_reg(i2c_bus,0x02,0x00000033); /*LED2ENDC*/
        write_reg(i2c_bus,0x03,0x00000043); /*LED1LEDSTC*/
        write_reg(i2c_bus,0x04,0x0000004F); /*LED1LEDENDC*/
        write_reg(i2c_bus,0x05,0x00000039); /*ALED2STC*/
        write_reg(i2c_bus,0x06,0x00000041); /*ALED2ENDC*/
        write_reg(i2c_bus,0x07,0x00000047); /*LED1STC*/
        write_reg(i2c_bus,0x08,0x0000004F); /*LED1ENDC*/
        write_reg(i2c_bus,0x09,0x00000027); /*LED2LEDSTC*/
        write_reg(i2c_bus,0x0A,0x00000033); /*LED2LEDENDC*/
        write_reg(i2c_bus,0x0B,0x00000055); /*ALED1STC*/
        write_reg(i2c_bus,0x0C,0x0000005D); /*ALED1ENDC*/
        write_reg(i2c_bus,0x0D,0x00000035); /*LED2CONVST*/
        write_reg(i2c_bus,0x0E,0x00000045); /*LED2CONVEND*/
        write_reg(i2c_bus,0x0F,0x00000047); /*ALED2CONVST*/
        write_reg(i2c_bus,0x10,0x00000057); /*ALED2CONVEND*/
        write_reg(i2c_bus,0x11,0x00000059); /*LED1CONVST*/
        write_reg(i2c_bus,0x12,0x00000069); /*LED1CONVEND*/
        write_reg(i2c_bus,0x13,0x0000006B); /*ALED1CONVST*/
        write_reg(i2c_bus,0x14,0x0000007B); /*ALED1CONVEND*/
        write_reg(i2c_bus,0x1D,0x000000FF); /*PRPCOUNT*/    /* 128k/FF = 500Hz,采样频率为500 */

        write_reg(i2c_bus,0x1E,0x00000102); /*CONTROL1*/    /*  bits 3-0 means numav EVERY 2 */
        write_reg(i2c_bus,0x1F,0x00000000); /*TIAGAIN_2_3*/
        write_reg(i2c_bus,0x20,0x00000003); /*TIAGAIN*/
        write_reg(i2c_bus,0x21,0x00000003); /*TIA_AMB_GAIN*/
        write_reg(i2c_bus,0x22,0x000030C3); /*LEDCNTRL1*/
        write_reg(i2c_bus,0x23,0x00124218); /*CONTROL2 Enable OSC*/
        write_reg(i2c_bus,0x24,0x00000000); /*LEDCNTRL2*/
        write_reg(i2c_bus,0x28,0x00000000); /*TOGGLE*/
        write_reg(i2c_bus,0x29,0x00000000); /*CLKDIV1*/
        write_reg(i2c_bus,0x2A,0x00000000); /*LED2VAL*/
        write_reg(i2c_bus,0x2B,0x00000000); /*ALED2VAL*/
        write_reg(i2c_bus,0x2C,0x00000000); /*LED1VAL*/
        write_reg(i2c_bus,0x2D,0x00000000); /*ALED1VAL*/
        write_reg(i2c_bus,0x2E,0x00000000); /*LED2-ALED2VAL*/
        write_reg(i2c_bus,0x2F,0x00000000); /*LED1-ALED1VAL*/
        write_reg(i2c_bus,0x31,0x00000020); /*CONTROL3*/
        write_reg(i2c_bus,0x34,0x00000000); /*PROG_INT2_STC*/
        write_reg(i2c_bus,0x35,0x00000000); /*PROG_INT2_ENDC*/
        write_reg(i2c_bus,0x36,0x00000035); /*LED3LEDSTC*/
        write_reg(i2c_bus,0x37,0x00000041); /*LED3LEDENDC*/
        write_reg(i2c_bus,0x39,0x00000000); /*CLKDIV2*/
        write_reg(i2c_bus,0x3A,0x00100000); /*OFFDAC*/
        write_reg(i2c_bus,0x3B,0x00000000); /*THRDETLOW*/
        write_reg(i2c_bus,0x3C,0x00000000); /*THRDETHIGH*/
        write_reg(i2c_bus,0x3D,0x00000000); /*THRDET*/
        write_reg(i2c_bus,0x3E,0x00000000); /*I_OFFDAC*/
        write_reg(i2c_bus,0x3F,0x00000000); /*AVG_LED2_ALED2VAL*/
        write_reg(i2c_bus,0x40,0x00000000); /*AVG_LED1_ALED1VAL*/
        write_reg(i2c_bus,0x42,0x00000000); /*FIFO*/

抓包的数据如下:

微信截图_20200915113306.png

scl sda的pin使用了pa3 pa4 只在board.h中define了一下,其他无设置。

查看更多

关注者
0
被浏览
43
1 个回答
aozima
aozima 3 days ago
拒绝白嫖,拒绝键盘侠!

既然有ACK了,说明从机认了。

是否你LA的采样率太低了,或是协议解析出错了。

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友