各位大虾,我用STM32F103VBT和24C08用I2C通讯,利用库(V2.03)的函数进行,先写数据,再读,在读的过程中,EV6处SR1变为0x0044,而下面的判断需要是0x0040,程序在这死循环,跳不出来,请各位帮看看,已经搞两天,开始好使过一阵子,后面就一直这样。请帮忙,谢谢。
下面是读这块的程序:
void I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
I2C_EE_WaitEepromStandbyState();
/* Send START condition */
I2C_GenerateSTART(I2C2, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C2,
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C2, ENABLE);
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C2, ReadAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C2, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C2, EEPROM_ADDRESS, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
//////////问题在这跳不出去了,看寄存器的值SR1是0044,SR2是0003,而后面的比较值I2C_EVENT_MASTER_BYTE_RECEIVED是00030040。相差SR的bit2:BTF位
while(NumByteToRead) //
{
if(NumByteToRead == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C2, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(I2C2, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the EEPROM */
*pBuffer = I2C_ReceiveData(I2C2);
/* Point to the next location where the byte read will be saved */
pBuffer++;
/* Decrement the read bytes counter */
NumByteToRead--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C2, ENABLE);
}
先自己顶下,求各位大虾帮忙看看,是在搞不定了,谢谢!
1)请使用最新版本的固件库,目前的最新版本是V3.1.2。
2)请考虑使用置顶帖中的封装库:
STM32 I2C 封装库(查询方式+29楼中断方式+32楼DMA方式)。
本人还没调过I2C,,一直用SPI,但对I2C很有兴趣