2016-10-12 11:53 PM
Hi All,
When I use STM8AF6223 read data from eeprom 24C128,I can't poll RXNE flag. This is my code for Read Eeprom:uint8_t Read_EEPROM(uint8_t EepAddr,uint8_t RegAddr)
{
uint8_t data=0;
/* check busy flag ----------------------------------------------------------*/
while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY)==SET);
/* send START and check EV5 -------------------------------------------------*/
I2C_GenerateSTART(ENABLE);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
/* send Eeprom Address +Write and check EV6 ---------------------------------*/
I2C_Send7bitAddress(EepAddr,I2C_DIRECTION_TX);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/*send Register Address for Read data ---------------------------------------*/
I2C_SendData(0x00); // First Word Address
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
I2C_SendData(RegAddr); // Second Word Address
while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING));
/* Restart I2C --------------------------------------------------------------*/
I2C_GenerateSTART(ENABLE);
while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
/* send Eeprom Address + Read -----------------------------------------------*/
I2C_Send7bitAddress(EepAddr,I2C_DIRECTION_RX);
/* wait until ADDR flag is set ----------------------------------------------*/
while(I2C_GetFlagStatus(I2C_FLAG_ADDRESSSENTMATCHED==RESET));
/* clear ACK ----------------------------------------------------------------*/
I2C_AcknowledgeConfig(I2C_ACK_NONE);
/* clear ADDR flag ----------------------------------------------------------*/
//I2C -> SR1;
I2C -> SR3;
/* send STOP condition ------------------------------------------------------*/
I2C_GenerateSTOP(ENABLE);
/* Poll RXNE ----------------------------------------------------------------*/
while(I2C_GetFlagStatus(I2C_FLAG_RXNOTEMPTY)==RESET);
//Delay(10); // Delay 30us
/* read data ----------------------------------------------------------------*/
data = I2C_ReceiveData();
/* Poll STOP flag -----------------------------------------------------------*/
while(I2C -> CR2 & I2C_CR2_STOP);
/* Re-Enable ACK to be ready for another reception --------------------------*/
I2C_AcknowledgeConfig(I2C_ACK_CURR);
return (data);
}