/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //ó|′e£¬£¨Ñ¡ÔñÖ÷ģ꽣©
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//μè′y′ó»úó|′e¡££¨ó|′e3é1|ÔòÖ÷»ú·¢ËíÄ£ê½Î»ÖÃ1£©
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C1, WriteAddr); //·¢Ëí′ó»úÖD¼Ä′æÆ÷μØÖ·£¨ÎaáËÏòÆäÖDD′μØÖ·£©
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //μè′y′ó»úó|′e£¨Ö÷»ú·¢Ëí×Ö½úλÖÃ1£©
/* Send the byte to be written */
I2C_SendData(I2C1, pBuffer); //·¢Ëíêy¾Y×Ö½ú£¬D′Ôú¶Ôó|′ó»úμĶÔó|μØÖ·¿Õ¼äÖD
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //μè′y′ó»úó|′e£¨Ö÷»ú·¢Ëí×Ö½úλÖÃ1£©
void I2C_PageWrite(u8* pBuffer, u8 WriteAddr, u8 NumByteToWrite) //D′¶à×Ö½ú
{
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); //μè′y×üÏß¿ÕÏD
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //′ó»úó|′eÎaÖ÷»úÄ£ê½
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, SlaveAddress, I2C_Direction_Transmitter);//éèÖÃ′ó»úμØÖ·¼°Ö÷»ú·¢ËíÄ£ê½
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //ó|′eÎaÖ÷»ú·¢ËíÄ£ê½
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C1, WriteAddr); //éèÖÃ′ó»úÄú′æμØÖ·
/* Test on EV8 and clear it */
while(! I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //ó|′eÎaÖ÷»ú×Ö½ú·¢Ëí
/* While there is data to be written */
while(NumByteToWrite--)
{
/* Send the current byte */
I2C_SendData(I2C1, *pBuffer); //·¢Ëíêy¾Y
//while(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXE)==RESET);
/* Point to the next byte to be written */
pBuffer++;
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
void I2C_BufferRead(u8* pBuffer, u8 ReadAddr, u8 NumByteToRead) //¶áêy¾Y
{
/* While the bus is busy */
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)); //μè′y×üÏß¿ÕÏD
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); //ó|′eÖ÷»ú·¢ËíÄ£ê½Ñ¡Ôñ
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C1, ENABLE);
/* Send the EEPROM's internal address to write to */
I2C_SendData(I2C1, ReadAddr); //·¢Ëí′ó»úÄú′æμØÖ·
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //¼ì2éê¼t·¢Ëí×Ö½ú
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C1, ENABLE); //¿aê¼
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); //ó|′eê¼t5
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); //′ó»úó|′e£¬Ö÷»ú½óêÕÄ£ê½
/* While there is data to be read */
while(NumByteToRead)
{
if(NumByteToRead == 1) //èôÖ»′«ò»¸ö×Ö½ú¡£ÔòÖ÷»úÏìó|·Çó|′eDÅoÅ£¬è»oóí£Ö1
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) //μ±¼ì2aμ½Ö÷»ú½óêÕíê3飬Ôò½«½óêÕμ½μÄêy¾Y±£′æÔúpBufferÖD
{
/* Read a byte from the EEPROM */
*pBuffer = I2C_ReceiveData(I2C1);
/* 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(I2C1, ENABLE); //Ôù′Î′ò¿aó|′eê1Äü£¬ÎaÏÂ′ζáD′êy¾Y×ö×¼±¸
}