////////////////////////// // Write data to EEPROM // ////////////////////////// //I2C_SLAVE_ADDR = 0x50 address of the EEPROM //I am writing 'h','e','l','l','o' to the EEPROM and then read it back later //I2C_NUMBYTES = 5 //[MemoryHighAddr:MemoryLowAddr] -Address within the eeprom where you want to read/write //Data[] has the word 'hello' and RxdData[] will store data read from the eeprom I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address I2caRegs.I2CCNT = I2C_NUMBYTES + 2; //Set count to 5 characters plus 2 address bytes I2caRegs.I2CDXR = MemoryHighAddr; //Send eeprom high address I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0 I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out I2caRegs.I2CDXR = MemoryLowAddr; //Send eeprom low address for(i = 0; i < I2C_NUMBYTES; i++){ while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out I2caRegs.I2CDXR = Data[i]; //Send out the message } ////////////////////////// // Read data from EEPROM// ////////////////////////// I2caRegs.I2CSAR = I2C_SLAVE_ADDR; //Set slave address I2caRegs.I2CCNT = 2; //Set count to 2 address bytes I2caRegs.I2CDXR = MemoryHighAddr; //Send eeprom high address I2caRegs.I2CMDR.bit.TRX = 1; //Set to Transmit mode I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode I2caRegs.I2CMDR.bit.STP = 0; //Dont release the bus after Tx I2caRegs.I2CMDR.bit.STT = 1; //Send the start bit, transmission will follow while(I2caRegs.I2CSTR.bit.XRDY == 0){}; //Do nothing till data is shifted out I2caRegs.I2CDXR = MemoryLowAddr; //Send eeprom low address I2caRegs.I2CCNT = I2C_NUMBYTES; //read 5 bytes from eeprom I2caRegs.I2CMDR.bit.TRX = 0; //Set to Recieve mode I2caRegs.I2CMDR.bit.MST = 1; //Set to Master mode I2caRegs.I2CMDR.bit.FREE = 1; //Run in FREE mode I2caRegs.I2CMDR.bit.STP = 1; //Stop when internal counter becomes 0 I2caRegs.I2CMDR.bit.STT = 1; //Repeated start, Reception will follow for(i = 0; i < I2C_NUMBYTES; i++){ while(I2caRegs.I2CSTR.bit.RRDY == 0){}; //I2CDRR not ready to read? RxdData[i] = I2caRegs.I2CDRR; }