Skip to content

Instantly share code, notes, and snippets.

@dancollins
Created September 29, 2012 06:06
Show Gist options
  • Save dancollins/3803328 to your computer and use it in GitHub Desktop.
Save dancollins/3803328 to your computer and use it in GitHub Desktop.
EEPROM write which takes page boundaries into account
void ee_write(char * buf, unsigned int addr, unsigned int len) {
int i = 0;
ee_ackPoll(); // Wait for the EEPROM to be ready
sprintf(bf1, "Starting EEPROM write of %d bytes.\r\n", len);
debugr(bf1);
while(len > 0) { // Write all of the bytes to the EEPROM
IdleI2C1();
StartI2C1();
while (SSP1CON2bits.SEN);
WriteI2C1(EE_ADDR | EE_W);
IdleI2C1();
WriteI2C1((unsigned char)addr>>8); // Address to start writing
IdleI2C1();
WriteI2C1((unsigned char)addr&0xFF);
while (addr % 64 != 0) { // While we aren't on a page boundary
IdleI2C1();
sprintf(bf1, "Addr: %u Byte: %c\r\n", addr, buf[i]);
debugr(bf1);
WriteI2C1(buf[i++]);
addr++; // Written a byte, so increment count
len--; // Written a byte, so decrement bytes left
}
ee_ackPoll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment