…要点。下面的代码将正确写入(通过范围验证),并读取0x01的第一个字节,但第二个字节和所有后续的字节都是0xFF。我已经尝试了在第一个字节读取之后手动启动ACK,但是下一个PLIB函数“I2CcRebug已经完成(EEPROMIFI2CYBUS)”;“表示它尚未完成。************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************int DataSz;UTIN 32实际时钟;BoOL成功=TRUE;UTIT8 I2CBYTE1=0;UIT8 8 I2CBYTE2=0;UIT8 8 I2CBYTE3=0;UIT8 8 I2CBYTE4=0;UTIN I2CBYTE5=0;Unt8好读=0;//拔出WPAN模块退出重置TISCITBITS。TRISC14=0;PrtcBysRC14=1;/initIIDED调试消息(当支持)dBIN();/设置I2C BaDeffEffice时钟=I2CSETFEED(EEPROMETII2CYBUS,GETPUTURARALCOLCORDER()),I2CyClClOxFRIQ;如果(ABS(SturalClCK-I2CyClcLog-Frq)& gt;I2CyClcLog-Frq/10){DbPrimTf(“错误:I2C1时钟频率(%u)错误超过10% %.n”),(未签名)//(同时,ATTN=1)//启用I2C总线I2cEnabl(EEPROMYI2CL BUS,TRUE);/toto需要延迟直到ATIN线路变低,才能使I2C//********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************/**************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************如果把数据写入芯片,就可以了!StartTransfer(false){{(1);} / /传输所有数据索引=0;而(成功和&;(index & lt;DATSZ)){/ /发送一个字节IF(发送字节(I2CDATA [索引)){/ /前进到下一个字节索引++;/ /验证字节是否被确认(如果)!I2cByTeWaSudio(EEPROMIFI2CYBUS){dPrPtTf(“错误:发送字节未被确认n”);成功= false;}{成功=false;}}停止传输();I2CyFrast7By位址(SLaveAdter,I2CYRCPY地址,I2CYRID);如果……StartTransfer(false){{(1);}如果(发送字节(SLaveAddio.Byter)){//验证字节是否被确认(如果)!I2cByTeAtWestRead(EEPROMIFI2CYBUS){dBPrTNF(“错误:发送字节未被确认n”);成功=false;}{成功= false;}如果(成功){if(I2CcRelvEnabl(EEPROFIXI2CYBUS,TRUE)= = I2CL接收溢出){dBPrTNF(“错误:I2C接收溢出n”);成功= false;} {而(!)I2CybDeDATAISDISABLE(EEPROMYI2CYBUS);I2cBythyI2CGETBILL(EEPROMYI2CYBUS);I2CRealEngI2CYBUS,TRUE;GooRead=I2CXECGEGHASTER(EEPROMIFI2CYBUS);}(I2CurrVelabEnabl(EEPROFIXI2CYBUS,TRUE)==I2CyEnraveOx溢出){DbPrimTf(“错误:I2C接收溢出N”)“”;成功=假;} {同时()!I2CycDeDATA可用(EEPROMYI2CYBUS);I2CBYTE1= I2CGETBYEL(EEPROMIFI2CYBUS);}(I2CcRelvEnabl(EEPROMIFI2CYBUS,TRUE)= = I2CyEnraveOx溢出){DbPrimTf(“错误:I2C接收溢出n”);成功= false;} {{同时}!I2CcRevEdDATAIS可用(EEPROMIFI2CYBUS);I2CBYTE2= I2CGETBEYEL(EEPROMIFI2CYBUS);}}StestTrimes();
以上来自于百度翻译
以下为原文
... point taken. The below code will write correctly (verified by scope) and will read the first byte of 0x01 but second and all subsequent bytes are 0xff. I have tried manually initiating the ACK after first byte read but the next plib function "I2CAcknowledgeHasCompleted ( EEPROM_I2C_BUS );" indicates that it has not completed.
// ****************************************************************************
// ****************************************************************************
// Application Main Entry Point
// ****************************************************************************
// ****************************************************************************
int main(void)
{
UINT8 i2cData[10];
I2C_7_BIT_ADDRESS SlaveAddress;
int Index;
int DataSz;
UINT32 actualClock;
BOOL Acknowledged;
BOOL Success = TRUE;
UINT8 i2cbyte = 0;
UINT8 i2cbyte1 = 0;
UINT8 i2cbyte2 = 0;
UINT8 i2cbyte3 = 0;
UINT8 i2cbyte4 = 0;
UINT8 i2cbyte5 = 0;
UINT8 GoodRead = 0;
// pull WPAN module out of reset
TRISCbits.TRISC14 = 0;
PORTCbits.RC14 = 1;
// Initialize debug messages (when supported)
DBINIT();
// Set the I2C baudrate
actualClock = I2CSetFrequency(EEPROM_I2C_BUS, GetPeripheralClock(), I2C_CLOCK_FREQ);
if ( abs(actualClock-I2C_CLOCK_FREQ) > I2C_CLOCK_FREQ/10 )
{
DBPRINTF("Error: I2C1 clock frequency (%u) error exceeds 10%%.n", (unsigned)actualClock);
}
// while(ATTN == 1)
// Enable the I2C bus
I2CEnable(EEPROM_I2C_BUS, TRUE);// TODO NEED DELAY UNTIL ATTN LINE GOES LOW BEFORE ENABLING I2C
//*************************************************************************
//*********** HANDSHAKE MCP READ OF RCP STATUS AFTER POWER UP *************
//*************************************************************************
I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, I2C_RCP_ADDRESS , I2C_WRITE);
i2cData[0] = SlaveAddress.byte;
i2cData[1] = 0x81; // Read Header Request
i2cData[2] = 0x08; // 8 data bytes in Header
DataSz = 3;
// Start the transfer to write data to the chip
if( !StartTransfer(FALSE) )
{
while(1);
}
// Transmit all data
Index = 0;
while( Success && (Index < DataSz) )
{
// Transmit a byte
if (TransmitOneByte(i2cData[Index]))
{
// Advance to the next byte
Index++;
// Verify that the byte was acknowledged
if(!I2CByteWasAcknowledged(EEPROM_I2C_BUS))
{
DBPRINTF("Error: Sent byte was not acknowledgedn");
Success = FALSE;
}
}
else
{
Success = FALSE;
}
}
StopTransfer();
I2C_FORMAT_7_BIT_ADDRESS(SlaveAddress, I2C_RCP_ADDRESS , I2C_READ);
//*************************************************************************************
if( !StartTransfer(FALSE) )
{
while(1);
}
if (TransmitOneByte(SlaveAddress.byte))
{
// Verify that the byte was acknowledged
if(!I2CByteWasAcknowledged(EEPROM_I2C_BUS))
{
DBPRINTF("Error: Sent byte was not acknowledgedn");
Success = FALSE;
}
}
else
{
Success = FALSE;
}
if(Success)
{
if(I2CReceiverEnable(EEPROM_I2C_BUS, TRUE) == I2C_RECEIVE_OVERFLOW)
{
DBPRINTF("Error: I2C Receive Overflown");
Success = FALSE;
}
else
{
while(!I2CReceivedDataIsAvailable(EEPROM_I2C_BUS));
i2cbyte = I2CGetByte(EEPROM_I2C_BUS);
I2CAcknowledgeByte ( EEPROM_I2C_BUS, TRUE );
GoodRead = I2CAcknowledgeHasCompleted ( EEPROM_I2C_BUS );
}
if(I2CReceiverEnable(EEPROM_I2C_BUS, TRUE) == I2C_RECEIVE_OVERFLOW)
{
DBPRINTF("Error: I2C Receive Overflown");
Success = FALSE;
}
else
{
while(!I2CReceivedDataIsAvailable(EEPROM_I2C_BUS));
i2cbyte1 = I2CGetByte(EEPROM_I2C_BUS);
}
if(I2CReceiverEnable(EEPROM_I2C_BUS, TRUE) == I2C_RECEIVE_OVERFLOW)
{
DBPRINTF("Error: I2C Receive Overflown");
Success = FALSE;
}
else
{
while(!I2CReceivedDataIsAvailable(EEPROM_I2C_BUS));
i2cbyte2 = I2CGetByte(EEPROM_I2C_BUS);
}
}
StopTransfer();