Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #define device 0x50 // Address with three address pins grounded.
- void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
- byte err;
- byte counter;
- Wire.beginTransmission(deviceaddress);
- Wire.write((byte)(eeaddress >> 8)); // MSB
- Wire.write((byte)(eeaddress & 0xFF)); // LSB
- Wire.write(&data, 1);
- err = Wire.endTransmission();
- if (err != 0) Serial.print("Cannot write to device");
- /*while(1) {
- Wire.beginTransmission(deviceaddress);
- if(Wire.endTransmission() == 0)break;
- }*/
- // 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) (deviceaddress >> 8)); // high order byte
- Wire.write ((byte) (deviceaddress & 0xFF)); // low-order byte
- err = Wire.endTransmission ();
- if (err == 0)
- break;
- }
- }
- // WARNING: address is a page address, 6-bit end will wrap around
- // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
- void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
- Wire.beginTransmission(deviceaddress);
- Wire.write((byte)(eeaddresspage >> 8)); // MSB
- Wire.write((byte)(eeaddresspage & 0xFF)); // LSB
- byte c;
- for ( c = 0; c < length; c++)
- Wire.write(data[c]);
- Wire.endTransmission();
- }
- byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
- byte rdata = 0xFF;
- Wire.beginTransmission(deviceaddress);
- Wire.write((int)(eeaddress >> 8)); // MSB
- Wire.write((int)(eeaddress & 0xFF)); // LSB
- Wire.endTransmission(); //RESTART OBLIGATORY
- Wire.requestFrom(deviceaddress,1);
- if (Wire.available()) rdata = Wire.read();
- Wire.endTransmission();
- return rdata;
- }
- // maybe let's not read more than 30 or 32 bytes at a time!
- void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
- Wire.beginTransmission(deviceaddress);
- Wire.write((int)(eeaddress >> 8)); // MSB
- Wire.write((int)(eeaddress & 0xFF)); // LSB
- Wire.endTransmission();
- Wire.requestFrom(deviceaddress,length);
- int c = 0;
- for ( c = 0; c < length; c++ )
- if (Wire.available()) buffer[c] = Wire.read();
- }
- unsigned char somedata[] = {
- 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
- };
- char somedata2[] = "this is data from the eeprom"; // data to write
- void setup()
- {
- Serial.begin(9600);
- Wire.begin(); // initialise the connection
- delay(1000);
- Serial.println("Wait for Wire");
- Serial.println("OK!");
- // Write
- Serial.print("Write: ");
- for(short i = 0; i < 256; i++)
- {
- if(i % 16 == 0) Serial.println(" ");
- Serial.print ("0x");
- Serial.print(somedata[i], HEX);
- Serial.print ( ", ");
- i2c_eeprom_write_byte(device, i, (byte)somedata[i]);
- }
- delay(100); //add a small delay
- Serial.println(" ");
- Serial.println("Memory written: ");
- Serial.println(sizeof(somedata));
- delay(1000); //add a small delay
- //i2c_eeprom_write_page(device, 0, (byte *)somedata2, sizeof(somedata2)); // write to EEPROM
- Serial.print("\nReady\n");
- // DUMP
- Serial.print("Read: ");
- for(short i = 0; i < 256; i++)
- {
- if(i % 16 == 0) Serial.println(" ");
- Serial.print ("0x");
- Serial.print (i2c_eeprom_read_byte(device, i),HEX);
- Serial.print ( ", ");
- }
- }
- void loop()
- {
- /*for (byte bank = 0; bank < 16; bank++)
- {
- byte data[16]; // a byte array to store 16 bytes
- Wire.requestFrom(0x53, 16); // request 16 bytes
- for (byte i = 0; i < 16; i++)
- {
- data[i] = Wire.read(); // store each byte into the array
- Serial.print ("0x");
- Serial.print (data[i],HEX);
- Serial.print ( ", ");
- }
- Serial.print ("\n");
- delay(500);
- }*/
- delay(1000000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement