Mario Vargas
/
24LC256-EEPROM-I2C
EEPROM
Revision 1:0aa405151a74, committed 2016-12-20
- Comitter:
- andcor02
- Date:
- Tue Dec 20 14:25:12 2016 +0000
- Parent:
- 0:34c4fa6b49ac
- Child:
- 2:9bbd68ef87c5
- Commit message:
- Library to R/W for 24LC256 EEPROM;
Changed in this revision
--- a/main.cpp Wed Jul 20 10:43:06 2016 +0000 +++ b/main.cpp Tue Dec 20 14:25:12 2016 +0000 @@ -1,73 +1,15 @@ -// NOTES: -// 1. mbed.h MUST BE the revision from 30 Mar 2016, otherwise it will behave strange and unexpected! Older or newer versions won't work. -// For example, with the latest version of mbed.h from 25 May 2016, the "wait" function does not work at all, it's the same as if it's not there. -// 2. writeEEPROM has 64 bytes limitation. If you want to write more at once, read http://www.hobbytronics.co.uk/eeprom-page-write how this guy made it work. -// Im not interested in storing data above 64 bytes (1 page). The 24LC256 chip has 4096 pages. -// 3. reading the entire eeprom does its job until the 512 byte, then it will cycle from zero. I don't know why... #include "mbed.h" +I2C i2c(PTE25, PTE24); +Serial pc(USBTX, USBRX); +int address = 0xA2; +int pointerAdddress = 0; +char s[64]; + // function declaration void writeEEPROM(int address, unsigned int eeaddress, char *data, int size); void readEEPROM(int address, unsigned int eeaddress, char *data, int size); -// classes and functions instantiation -I2C i2c(I2C_SDA, I2C_SCL); -Serial pc(SERIAL_TX, SERIAL_RX); - -// settings -int address = 0xA8; // in my case A2 = high, A1 = low, A0 = low, so the address is 1010 1000 = A8 (format 1 0 1 0 A3 A2 A1 R/W) - -int main() -{ - pc.printf("24LC256 I2C EEPROM started...\n"); - - int pointerAdddress = 0; - - // write some data on eeprom - char writeData[] = {"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec f"}; // the text length must be below 64 bytes - char writeDataLen = 0; - do {writeDataLen++;} while (writeData[writeDataLen]); // calculate the text length - /* - writeEEPROM(address, pointerAdddress, writeData, writeDataLen); - - pc.printf("Data written: %s\n", writeData); - */ - - // read the data back - char data_read[writeDataLen]; - readEEPROM(address, pointerAdddress, data_read, writeDataLen); - - pc.printf("Data read: %s\n", data_read); - - // read the entire eeprom - /* - pc.printf("Printing the entire EEPROM array (64 columns, 4096 rows):\n"); - int i; - char data_chunk[64]; - int pointer; - for (i = 0; i < 4096; i++) - { - pointer = 64 * i; - readEEPROM(address, pointer, data_chunk, 64); - pc.printf("row: %4d, data: %s\n", i, data_chunk); - } - */ - - // delete the entire eeprom - /* - pc.printf("Deleting the entire EEPROM array (64 columns, 4096 rows):\n"); - int i; - int pointer; - char deleteData[] = {"0000000000000000000000000000000000000000000000000000000000000000"}; // 64 bytes data length of zeros - for (i = 0; i < 4096; i++) - { - pointer = 64 * i; - writeEEPROM(address, pointer, deleteData, 64); - } - pc.printf("Erasing complete\n"); - */ -} - // this function has 63 bytes write limit void writeEEPROM(int address, unsigned int eeaddress, char *data, int size) { @@ -97,4 +39,34 @@ // Read eeprom i2c.read(address, data, size); wait_ms(6); -} \ No newline at end of file +} + +int main() +{ + char data_read[64]; + readEEPROM(address, pointerAdddress, data_read, 64); + pc.printf("Previous data stored: %s\n", data_read); + while(1){ + + pc.gets(s,64); + char writeDataLen = 0; + do {writeDataLen++;} while (s[writeDataLen]); // calculate the text length + + writeEEPROM(address, pointerAdddress, s, writeDataLen); + pc.printf("Data written: %s\n", s); + + // read the data back + char data_read[writeDataLen]; + readEEPROM(address, pointerAdddress, data_read, writeDataLen); + pc.printf("Data read: %s\n", data_read); + } + +} + + + + + + + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Dec 20 14:25:12 2016 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#c3b9436e12610acaab723f730ab15b48a539a5ac
--- a/mbed.bld Wed Jul 20 10:43:06 2016 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/99a22ba036c9 \ No newline at end of file