只能写入6个字节,7与8字节写不进
uint32_t EEPROM_PageWrite(uint8_t u8Addr, uint8_t *pu8Buf)
{
int32_t i32Err;
int32_t i,k;
k=0;
do
{
k++;
i32Err = 0;
/* Send start */
_I2C_START(I2C0);
_I2C_WAIT_READY(I2C0);
/* Send control byte */
_I2C_WRITE(I2C0, EEPROM_WRITE_ADDR);
_I2C_WAIT_READY(I2C0);
if(I2C0->I2CSTATUS == 0x18)
{
{
/* ACK */
/* Send low address */
_I2C_WRITE(I2C0, u8Addr); //address
_I2C_WAIT_READY(I2C0);
if(I2C0->I2CSTATUS == 0x28)
{
/* ACK */
for(i=0;i<8;i++)
{
/* Send data */
if(I2C0->I2CSTATUS == 0x28)
{
_I2C_WRITE(I2C0, pu8Buf[i]); // data
_I2C_WAIT_READY(I2C0);
if(I2C0->I2CSTATUS == 0x30)
{
/* NACK */
/* Send data error */
i32Err = 3;
}
}
else i32Err = 1;
}
if(I2C0->I2CSTATUS == 0x28)
{
/* ACK */
/* Send stop */
_I2C_STOP(I2C0);
}
else
{
/* NACK */
/* Send data error */
i32Err = 4;
}
}
else
{
/* NACK */
/* Send low address error */
i32Err = 2;
}
}
}
else
{
/* NACK */
/* Send control error */
i32Err = 1;
}
if(i32Err)
{
/* Send stop */
_I2C_STOP(I2C0);
}
Delay(200);
}while(i32Err&&(k<4));
return(i32Err);
}
|