/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* In the case of a single data transfer disable ACK before reading the data */
if(NumByteToRead==1)
{
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* 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));
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
/* While there is data to be read */
while(NumByteToRead)
{
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if(NumByteToRead == 2)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
后来改成这样了,但是还是只能读一次。LM75用GPIO模拟没有问题。
/*******************************************************************************
* Function Name : I2C_EE_BufferRead
* Description : Reads a block of data from the EEPROM.
* Input : - pBuffer : pointer to the buffer that receives the data read
* from the EEPROM.
* - ReadAddr : EEPROM's internal address to read from.
* - NumByteToRead : number of bytes to read from the EEPROM.
* Output : None
* Return : None
*******************************************************************************/
u32 I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)
{
/* Send START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{TestI2C++;
if(TestI2C==0xFF)
{
return 1;
}
}
TestI2C=0;
/* In the case of a single data transfer disable ACK before reading the data */
if(NumByteToRead==1)
{
I2C_AcknowledgeConfig(I2C1, DISABLE);
}
/* Send EEPROM address for write */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{TestI2C++;if(TestI2C==0xFF)
{return 2;}}
TestI2C=0;
/* 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))
{TestI2C++;if(TestI2C==0xFF){return 3;}}
TestI2C=0;
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{TestI2C++;if(TestI2C==0xFF){return 4;}}
TestI2C=0;
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{TestI2C++;if(TestI2C==0xFF){return 5;}}
TestI2C=0;
/* While there is data to be read */
while(NumByteToRead)
{
/* Test on EV7 and clear it */
// Reset I2C1 IP
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
// Release reset signal of I2C1 IP
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
// Set PB6,7 as OD AF - I2C1_SCL, I2C1_SDA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; // standard: 100k, fast: 400k
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Set PB5 as PU in - TemperatureSensor_INT
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//-----------------------------------------------------------------
// Read Tos register of LM75
//-----------------------------------------------------------------
u16 I2C_LM75_Tos_Read(void)
{
u16 u;
u = 0xffff; // init an invalid value
// Receive data: high byte (Tos_Reg) and ACK
I2C_AcknowledgeConfig(I2C1, ENABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Rx_Not_Empty) )
;
u = I2C_ReceiveData(I2C1) << 8;
// Receive data: low byte (Tos_Reg) and NAK
I2C_AcknowledgeConfig(I2C1, DISABLE);
while( !(I2C_ReadRegister(I2C1, I2C_Register_SR1) & Rx_Not_Empty) )
;
u |= I2C_ReceiveData(I2C1);
I2C_GenerateSTOP(I2C1, ENABLE);
// return correct result
return u;
}
//-----------------------------------------------------------------
// Read Temperature register of LM75
//-----------------------------------------------------------------
u16 I2C_LM75_Temp_Read(void)
{
u16 u;
u = 0xffff; // init an invalid value