I2C_Result I2C_EE_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead) { /* Enable I2C1 acknowledgement if it is already disabled by other function */ I2C_AcknowledgeConfig(I2C1, ENABLE);
//测试总线是否空闲 I2C_Delay_Out(3); while(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BUS_BUSY)&&(!I2C_Time_out)); if (I2C_Time_out) { /*i2c bus is busy. some other i2c communication is ongoing*/ I2C_Time_out = FALSE; RCC_FSMC_Enables; LCD_DisplayStringLine(Line4,"BUS_BUSY_read"); return BUS_BUSY; }
/* Send START condition */ I2C_GenerateSTART(I2C1, ENABLE); I2C_Delay_Out(1); /* Test on EV5 and clear it */ while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) || I2C_Time_out)); if (I2C_Time_out) { RCC_FSMC_Enables; LCD_DisplayStringLine(Line4,"EV5 OUT"); return SEND_START_ERR; } /* Send EEPROM address for write */ I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Transmitter); I2C_Delay_Out(1); /* Test on EV6 and clear it */ while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)|| I2C_Time_out)); if (I2C_Time_out) { I2C_GenerateSTOP(I2C1, ENABLE); RCC_FSMC_Enables; LCD_DisplayStringLine(Line4,"ADDR_HEADER_MATCH_ERR"); return ADDR_HEADER_MATCH_ERR; }
/* Send the EEPROM's internal address to write to */ I2C_SendData(I2C1, ReadAddr); I2C_Delay_Out(1); /* Test on EV8 and clear it */ while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)|| I2C_Time_out)); if (I2C_Time_out) { I2C_GenerateSTOP(I2C1, ENABLE); RCC_FSMC_Enables; LCD_DisplayStringLine(Line4,"ADDR_MATCH_ERR"); return ADDR_MATCH_ERR; }
/* Send START condition */ I2C_GenerateSTART(I2C1, ENABLE); I2C_Delay_Out(1); /* Test on EV5 and clear it */ while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) || I2C_Time_out)); if (I2C_Time_out) { RCC_FSMC_Enables; LCD_DisplayStringLine(Line4,"EV5 OUT"); return SEND_START_ERR; } /* Send 24c16 slave address for read */ I2C_Send7bitAddress(I2C1, EEPROM_ADDRESS, I2C_Direction_Receiver); I2C_Delay_Out(10); /* Test on EV6 and clear it */ while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)|| I2C_Time_out)); if (I2C_Time_out) { I2C_GenerateSTOP(I2C1, ENABLE); RCC_FSMC_Enables; LCD_DisplayStringLine(Line4,"ADDR_HEADER_MATCH_ERR"); return ADDR_HEADER_MATCH_ERR; } /* While there is data to be read */ while(NumByteToRead) { if(NumByteToRead == 1) { I2C_AcknowledgeConfig(I2C1, DISABLE); I2C_GenerateSTOP(I2C1, ENABLE); }
I2C_Delay_Out(1); /* Test on EV7 and clear it */ while(!(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)|| I2C_Time_out));//延时后死在这里 if (I2C_Time_out) { I2C_Time_out = FALSE; I2C_GenerateSTOP(I2C1, ENABLE); RCC_FSMC_Enables; LCD_DisplayStringLine(Line4,"DATA_TIMEOUT"); return DATA_TIMEOUT; } /* Read a byte from the EEPROM */ *pBuffer = I2C_ReceiveData(I2C1);
I2C_Delay_Out(1); //延时1ms测试,这里不允许有其他的操作吗?如果在这里产生中断怎么办? while(!I2C_Time_out);
pBuffer++; NumByteToRead--; } return NO_ERR; } 这个I2C的连续读程序是根据香主的例程修改。(NOSTRETCH=0没有作用)
|