Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Written by Nick Gammon
- // February 2011
- // Note: maximum address is 0x7FFF (32 Kbytes)
- #include <Wire.h>
- const byte rom = 0x50; // Address of 24LC256 eeprom chip
- unsigned char somedata[][16] = {
- {0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0x38, 0x36, 0x36, 0x2E, 0x32, 0x37, 0x38, 0x36, 0x36, 0x2E},
- {0x32, 0x37, 0x03, 0x93, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x7F, 0x4A, 0x02, 0x00, 0xFA, 0xC5},
- {0xFA, 0xFB, 0xF8, 0xF9, 0xFE, 0xFF, 0xF2, 0xF3, 0xF7, 0xDF, 0xFA, 0x57, 0x03, 0x93, 0x00, 0x00},
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xD0, 0x00, 0x03, 0x93},
- {0x02, 0x00, 0x28, 0x00, 0x0E, 0x99, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0xC4, 0xC4, 0x50, 0x00},
- {0x4C, 0x00, 0x00, 0x60, 0x58, 0x4D, 0x00, 0x00, 0x00, 0x2E, 0x09, 0x44, 0x00, 0x00, 0xC6, 0x47},
- {0x00, 0x00, 0x55, 0x22, 0x00, 0x60, 0xA1, 0x60, 0x00, 0xC4, 0xC4, 0x50, 0x00, 0x4C, 0x00, 0x00},
- {0x00, 0x58, 0x4D, 0x00, 0x00, 0x00, 0x2E, 0x09, 0x44, 0x00, 0x00, 0xE3, 0xA3, 0x00, 0x00, 0x55},
- {0x22, 0x00, 0x60, 0xA1, 0x26, 0x00, 0x02, 0x00, 0xF7, 0xDF, 0xFA, 0x60, 0xAA, 0xFF, 0xE1, 0x1E},
- {0x4C, 0x08, 0x08, 0x00, 0x04, 0x28, 0x02, 0xF1, 0x0A, 0x00, 0xA6, 0x7F, 0x0A, 0x08, 0x02, 0x02},
- {0x00, 0x00, 0x28, 0xFF, 0x71, 0xAC, 0x85, 0x89, 0x6F, 0x17, 0x08, 0x08, 0x08, 0x00, 0x00, 0x28},
- {0xFF, 0x2B, 0x00, 0x1C, 0xAA, 0x7D, 0x08, 0x08, 0x02, 0x02, 0x00, 0x00, 0x28, 0x17, 0x00, 0x93},
- {0x02, 0xB8, 0xB7, 0x22, 0x4C, 0x80, 0x80, 0x00, 0x04, 0x28, 0x7B, 0x00, 0x00, 0x00, 0x82, 0x92},
- {0x2E, 0x44, 0x01, 0x01, 0x00, 0x04, 0x28, 0x01, 0x74, 0x00, 0x82, 0x00, 0x99, 0x0B, 0x08, 0x10},
- {0x10, 0x00, 0x00, 0x28, 0x03, 0x5F, 0x61, 0x57, 0x9E, 0x6B, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00},
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
- };
- void setup (void)
- {
- Serial.begin (9600); // debugging
- Wire.begin ();
- // test: write the number 123 to address 0x1234
- /*writeEEPROM (rom, 0, 123);
- // read back to confirm
- byte a = readEEPROM (rom, 0);
- Serial.println (a, DEC); // display to confirm
- // test: write a string to address 0x1000
- byte hello [] = "Hello, world!";
- writeEEPROM (rom, 10, hello, sizeof hello);*/
- byte test;
- for(byte i = 0; i < 16; i++)
- {
- writeEEPROM (rom, i, somedata[i], sizeof somedata[i]);
- }
- for(short i = 0; i < 256; i++)
- {
- if(i % 16 == 0) Serial.println(" ");
- byte err = readEEPROM(rom, i);
- Serial.print ("0x");
- Serial.print (err, HEX);
- Serial.print ( ", ");
- }
- } // end of setup
- void loop() {} // no main loop
- // write len (max 32) bytes to device, returns non-zero on error
- // return code: 0xFF means buffer too long
- // other: other error (eg. device not present)
- // Note that if writing multiple bytes the address plus
- // length must not cross a 64-byte boundary or it will "wrap"
- byte writeEEPROM (byte device, unsigned int addr, byte * data, byte len )
- {
- byte err;
- byte counter;
- if (len > BUFFER_LENGTH) // 32 (in Wire.h)
- return 0xFF; // too long
- Wire.beginTransmission(device);
- Wire.write ((byte) (addr >> 8)); // high order byte
- Wire.write ((byte) (addr & 0xFF)); // low-order byte
- Wire.write (data, len);
- err = Wire.endTransmission ();
- if (err != 0)
- return err; // cannot write to device
- // wait for write to finish by sending address again
- // ... give up after 100 attempts (1/10 of a second)
- for (counter = 0; counter < 100; counter++)
- {
- delayMicroseconds (300); // give it a moment
- Wire.beginTransmission (device);
- Wire.write ((byte) (addr >> 8)); // high order byte
- Wire.write ((byte) (addr & 0xFF)); // low-order byte
- err = Wire.endTransmission ();
- if (err == 0)
- break;
- }
- return err;
- } // end of writeEEPROM
- // write one byte to device, returns non-zero on error
- byte writeEEPROM (byte device, unsigned int addr, byte data )
- {
- return writeEEPROM (device, addr, &data, 1);
- } // end of writeEEPROM
- // read len (max 32) bytes from device, returns non-zero on error
- // return code: 0xFF means buffer too long
- // 0xFE means device did not return all requested bytes
- // other: other error (eg. device not present)
- // Note that if reading multiple bytes the address plus
- // length must not cross a 64-byte boundary or it will "wrap"
- byte readEEPROM (byte device, unsigned int addr, byte * data, byte len )
- {
- byte err;
- byte counter;
- if (len > BUFFER_LENGTH) // 32 (in Wire.h)
- return 0xFF; // too long
- Wire.beginTransmission (device);
- Wire.write ((byte) (addr >> 8)); // high order byte
- Wire.write ((byte) (addr & 0xFF)); // low-order byte
- err = Wire.endTransmission ();
- if (err != 0)
- return err; // cannot read from device
- // initiate blocking read into internal buffer
- Wire.requestFrom (device, len);
- // pull data out of Wire buffer into our buffer
- for (counter = 0; counter < len; counter++)
- {
- data [counter] = Wire.read ();
- }
- return 0; // OK
- } // end of readEEPROM
- // read one byte from device, returns 0xFF on error
- byte readEEPROM (byte device, unsigned int addr )
- {
- byte temp;
- if (readEEPROM (device, addr, &temp, 1) == 0)
- return temp;
- return 0xFF;
- } // end of readEEPROM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement