打印
[NUC120]

nuc220 I2C每次读多个字节

[复制链接]
3253|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
coke|  楼主 | 2014-3-11 13:41 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
NUC, I2c, ST, se, AN
使用NUC220的lib库IIC读写24C02. 其中rx回调函数如下:
/*---------------------------------------------------------------------------------------------------------*/
/*  I2C Rx Callback Function                                                                               */
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterRx(uint32_t u32Status)
{
    if (u32Status == 0x08)                      /* START has been transmitted and prepare SLA+W */
    {
        I2C1->I2CDAT = g_u8DeviceAddr << 1;     /* Write SLA+W to Register I2CDAT */
        _I2C_SET_CONTROL_BITS(I2C1, I2C_I2CON_SI);
    }   
    else if (u32Status == 0x18)                 /* SLA+W has been transmitted and ACK has been received */
    {
  I2C1->I2CDAT = g_u8MemAddr;
        _I2C_SET_CONTROL_BITS(I2C1, I2C_I2CON_SI);
    }
    else if (u32Status == 0x20)                 /* SLA+W has been transmitted and NACK has been received */
    {
        _I2C_SET_CONTROL_BITS(I2C1, I2C_I2CON_STA_STO_SI); //发送stop停止
    }
    else if (u32Status == 0x28)                 /* DATA has been transmitted and ACK has been received */
    {
        _I2C_SET_CONTROL_BITS(I2C1, I2C_I2CON_STA_SI);   //再次发送start      
    }
    else if (u32Status == 0x10)                 /* Repeat START has been transmitted and prepare SLA+R */
    {
        I2C1->I2CDAT = ((g_u8DeviceAddr << 1) | 0x01);   /* Write SLA+R to Register I2CDAT */
        _I2C_SET_CONTROL_BITS(I2C1, I2C_I2CON_SI);
    }
    else if (u32Status == 0x40)                 /* SLA+R has been transmitted and ACK has been received */
    {
        _I2C_SET_CONTROL_BITS(I2C1, I2C_I2CON_SI);
    }      
    else if (u32Status == 0x58)                 /* DATA has been received and NACK has been returned */
    {
  g_au8TRxData[g_u8DataLen++] = I2C1->I2CDAT;
  if (g_u8DataLen >= RTxDataSize)
  {
         g_u8EndFlag = 1;
      _I2C_SET_CONTROL_BITS(I2C1, I2C_I2CON_STO_SI); //发送stop停止
  }
  else
   _I2C_SET_CONTROL_BITS(I2C1, I2C_I2CON_SI);
    }
    else
    {
        /* TO DO */
        printf("Status 0x%x is NOT processed\n", u32Status);
    }           
}
这个原来是读写24LC64,每次读一个字节,现在如何改成一次可以读多个字节的. 我修改后使用以上代码发现只有第一个字节读出OK,后面全部是0xff.  24c02规格中每读一个字节,应该回复ack给24c02,但demo中没有见到任何回复ACK的地方. 请问应该如何修改此回调函数  

相关帖子

沙发
stul海盗| | 2014-3-11 17:24 | 只看该作者
我在学校写的I2C和24LC02通信的代码,不过只发送接收了一个字节

I2C.rar

1.33 KB

使用特权

评论回复
板凳
呆板书生| | 2014-3-13 14:56 | 只看该作者
曾经做个一个项目,也是一直头疼这个问题,后来发现,一次读完所有数据再检查就OK

使用特权

评论回复
地板
lj22022| | 2014-3-14 14:21 | 只看该作者
demo只做了针对一个字节的读取,下面是我的连续读取的code,主要问题在于应答的设定,这个在技术参考手册里面有提到,可以自己去看看
/*---------------------------------------------------------------------------------------------------------*/
/*  I2C0 (Master) Rx Callback Function                                                                     */
/*---------------------------------------------------------------------------------------------------------*/
void I2C0_Callback_Rx(uint32_t status)
{
    if (status == 0x08)                     /* START has been transmitted and prepare SLA+W */
    {
        DrvI2C_WriteData(I2C_PORT0, Device_Addr<<1);
        DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
    }   
    else if (status == 0x18)                /* SLA+W has been transmitted and ACK has been received */
    {
        DrvI2C_WriteData(I2C_PORT0, Tx_Data[0]);
        DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
    }
    else if (status == 0x20)                /* SLA+W has been transmitted and NACK has been received */
    {
        DrvI2C_Ctrl(I2C_PORT0, 1, 1, 1, 0);
    }
    else if (status == 0x28)                /* DATA has been transmitted and ACK has been received */
    {
        DrvI2C_Ctrl(I2C_PORT0, 1, 0, 1, 0);      
    }
    else if (status == 0x10)                /* Repeat START has been transmitted and prepare SLA+R */
    {
        DrvI2C_WriteData(I2C_PORT0, Device_Addr<<1 | 0x01);
        DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
    }
    else if (status == 0x40)                /* SLA+R has been transmitted and ACK has been received */
    {
        if(RxLen == 1)
                DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
        else
                DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1);
    }   
    else if (status == 0x50)                /* DATA has been received and ACK has been returned */
    {
        Rx_Data[DataLen++] = DrvI2C_ReadData(I2C_PORT0);   
        if(DataLen < (RxLen-1))              
                DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 1);                     
        else
                DrvI2C_Ctrl(I2C_PORT0, 0, 0, 1, 0);
    }
    else if (status == 0x58)                /* DATA has been received and NACK has been returned */
    {
        Rx_Data[DataLen] = DrvI2C_ReadData(I2C_PORT0);
        DrvI2C_Ctrl(I2C_PORT0, 0, 1, 1, 0);
        EndFlag = 1;
    }
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

104

主题

316

帖子

2

粉丝