Feature:
I2C总线, 256页,每页8Byte.
#define EEP_FIRST_PAGE 0X00
#define BUFFER_SIZE 256
#define I2C_PAGE_SIZE 8
void main()
{
eeprom_buffer_write(i2c_buffer_write, EEP_FIRST_PAGE, BUFFER_SIZE);
printf("AT24C02 reading \r\n");
eeprom_buffer_read(i2c_buffer_read, EEP_FIRST_PAGE, BUFFER_SIZE);
}
void eeprom_buffer_write(uint8_t *p_buffer, uint8_t write_address, uint16_t number_of_byte)
void eeprom_buffer_write(uint8_t *p_buffer, uint8_t write_address, uint16_t number_of_byte)
{
uint8_t number_of_Page=0; //总整页数
uint8_t number_of_single=0; //不足一页的字节数
uint8_t address=0; //写的起始地址
uint8_t count=0;
address = write_address % I2C_PAGE_SIZE;
count = I2C_PAGE_SIZE - address;
number_of_page = number_of_byte / I2C_PAGE_SIZE;
number_of_single = number % I2C_PAGE_SIZE;
if(address == 0) //传入的写地址刚好是某一页的起始地址
{
while(number_of_page--)
{
//写整页,一页8Byte
eeprom_page_write(p_buffer, write_address, I2C_PAGE_SIZE)
eeprom_wait_standby_state();
write_address += I2C_PAGE_SIZE;
p_buffer += I2C_PAGE_SIZE;
}
if(number_of_single != 0)
{
}
}else //传入的地址,在页的中间
{
if(number_of_count < count) //一页剩余的 > 要写的,直接写入
{
eeprom_page_write(p_buffer, write_address, number_of_byte);
eeprom_wait_standby_state();
}else
{
number_of_byte -= count;
number_of_page = number_of_byte / I2C_PAGE_SIZE;
number_of_single = number_of_byte % I2C_PAGE_SIZE;
//先写首页,首页没有对齐,有地址偏移
if(count != 0)
{
eeprom_page_write(p_buf , write, count);
eeprom_wait_standy_state();
write_address += count;
p_buffer += count;
}
//写整页
while(number_of_page--)
{
eeprom_page_write(p_buffer, write_address, I2C_PAGE_SIZE);
eeprom_wait_standby_state();
write_address += I2C_PAGE_SIZE;
p_buffer += I2C_PAGE_SIZE;
}
//有不整的尾页,就写
if(number_of_single != 0)
{
eeprom_page_write(p_buffer, write_address, number_of_single);
eeprom_wait_standby_state();
}
}
}
}