void I2C_EE_ByteWrite(unsigned short int WriteAddr, unsigned char pBuffer)
{
/* Send STRAT condition */
I2C1_CR1 |= 0x0100;
/* Test on EV5 and clear it */
while(!(I2C1_SR1 & 0x0001));
/* Send EEPROM address for write */
I2C1_DR = I2C_SLAVE_ADDRESS7_Write;
/* Test on EV6 and clear it */
while((!(I2C1_SR1 & 0x0002)) || (I2C1_SR2 & 0x0000));
/* Send the EEPROM's internal address to write to : MSB of the address first */
I2C1_DR = (unsigned char)((WriteAddr & 0xFF00) >> 8);
/* Test on EV8 and clear it */
while(!(I2C1_SR1 & 0x0080) || (I2C1_SR2 & 0x0000));
/* Send the EEPROM's internal address to write to : LSB of the address */
I2C1_DR = (unsigned char)(WriteAddr & 0x00FF);
/* Test on EV8 and clear it */
while(!(I2C1_SR1 & 0x0080));
/* Send the byte to be written */
I2C1_DR = pBuffer;
/* Test on EV8 and clear it */
while(!(I2C1_SR1 & 0x0080));
/* Send STOP condition */
I2C1_CR1 |= 0x0200;
}
unsigned char I2C_EE_ByteRead(unsigned short int ReadAddr)
{
/* While the bus is busy */
while(I2C1_SR2 & 0x0002);
/* Send START condition */
I2C1_CR1 |= 0x0100;
/* Test on EV5 and clear it */
while(!(I2C1_SR1 & 0x0001));
/* Send EEPROM address for write */
I2C1_DR = I2C_SLAVE_ADDRESS7_Write;
/* Test on EV6 and clear it */
while(!(I2C1_SR1 & 0x0002) || (I2C1_SR2 & 0x0000));
/* Clear EV6 by setting again the PE bit */
I2C1_CR1 |= 0x0001;
/* Send the EEPROM's internal address to read from: MSB of the address first */
I2C1_DR = (unsigned char)((ReadAddr & 0xFF00) >> 8);
/* Test on EV8 and clear it */
while(!(I2C1_SR1 & 0x0080));
/* Send the EEPROM's internal address to read from: LSB of the address */
I2C1_DR = (unsigned char)(ReadAddr & 0x00FF);
/* Test on EV8 and clear it */
while(!(I2C1_SR1 & 0x0080));
/* Send STRAT condition a second time */
I2C1_CR1 |= 0x0100;
/* Test on EV5 and clear it */
while(!(I2C1_SR1 & 0x0001));
/* Send EEPROM address for read */
I2C1_DR = I2C_SLAVE_ADDRESS7_Read;
/* Test on EV6 and clear it */
while(!(I2C1_SR1 & 0x0002) || (I2C1_SR2 & 0x0000));
/* While there is data to be read */
/* Disable Acknowledgement */
I2C1_CR1 &= 0xFBFF;
/* Send STOP Condition */
I2C1_CR1 |= 0x0200;
return I2C1_DR;
}
在调试时,单步运行这两个函数可以运行通过,但run起来就老是死在某哦个while处,请大家帮忙看看这两个函数有什么问题? |