最近在做一个测试应用,用到CY8CMBR3108,用STC MCU,IC写)X37地址有ACK,写寄存器地址总是没有ACK,SDA总是高电平,有没有大佬遇到过啊,如何解决这个问题,下面是部分程序
//3108 read data
unsigned char I2C_3108_ReadData(unsigned char slaveAddress , unsigned char regAddr , unsigned char *readBuffer , unsigned char numberOfBytes )
{
unsigned char i=0,j=0,k=0;
unsigned int timeout=0;
unsigned char errorStatus = FALSE;
IIC_Start();
errorStatus = IIC_SendByte((slaveAddress<<1)|I2C_3108_WRITE);
errorStatus = IIC_SendByte((slaveAddress<<1)|I2C_3108_WRITE);
errorStatus &= iic_wait_ack();
while (( !errorStatus) && (timeout < I2C_3108_READY_STATUS_TIMEOUT))
{
IIC_Start();
errorStatus &= IIC_SendByte((slaveAddress<<1)|I2C_3108_WRITE);
errorStatus &= iic_wait_ack();
timeout ++;
}
if (timeout>(I2C_3108_READY_STATUS_TIMEOUT-1))
{
errorStatus = FALSE;
return errorStatus;
}
errorStatus &= IIC_SendByte(regAddr);
errorStatus &= iic_wait_ack();
if(!errorStatus)
{
return errorStatus;
}
IIC_Stop();
delayMs(50);
IIC_Start();
errorStatus &= IIC_SendByte((slaveAddress<<1)|I2C_3108_READ);
errorStatus &= iic_wait_ack();
while (( !errorStatus) && (timeout < I2C_3108_READY_STATUS_TIMEOUT))
{
IIC_Start();
errorStatus &= IIC_SendByte((slaveAddress<<1)|I2C_3108_READ);
errorStatus &= iic_wait_ack();
timeout ++;
}
if (timeout>(I2C_3108_READY_STATUS_TIMEOUT-1))
{
errorStatus = FALSE;
return errorStatus;
}
// delayMs(50);
// IIC_delay();
// errorStatus &= IIC_SendByte((slaveAddress<<1)|I2C_3108_READ);
// errorStatus &= iic_wait_ack();
// IIC_delay();
if (errorStatus)
{
for (j = 0; j < numberOfBytes-1; j++)
{
readBuffer[j] = IIC_RecvByte();
IIC_ACK() ;
// if (!errorStatus)
// {
// return errorStatus;
// }
}
readBuffer[numberOfBytes-1] = IIC_RecvByte();
IIC_NOACK() ;
}
IIC_Stop();
return errorStatus;
}
|