AT24CXXX EEPROM

AT24CXXX EEPROM

This page is to Setup and Config IR AT24CXXX EEPROM

Hardware and Software

Hardware

AT24CXXX EEPROM

Software

Arduino IDE

Sanki Notes

    • AT24CXXX EEPROM

    • ------------ I2C ----------

    • GND : 1,2,3,4

    • V++ : 8

    • SCL : A5

    • SDA : A4

Examples

This example is ..........

Examples

This example is ..........

          1. /*

          2. * Use the I2C bus with EEPROM 24LC64

          3. * Sketch: eeprom.pde

          4. *

          5. * Author: hkhijhe

          6. * Date: 01/10/2010

          7. *

          8. *

          9. */

          10. #include <Wire.h> //I2C library

          11. void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {

          12. int rdata = data;

          13. Wire.beginTransmission(deviceaddress);

          14. Wire.write((int)(eeaddress >> 8)); // MSB

          15. Wire.write((int)(eeaddress & 0xFF)); // LSB

          16. Wire.write(rdata);

          17. Wire.endTransmission();

          18. }

          19. // WARNING: address is a page address, 6-bit end will wrap around

          20. // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes

          21. void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {

          22. #include <Wire.h>

          23. Wire.beginTransmission(deviceaddress);

          24. Wire.write((int)(eeaddresspage >> 8)); // MSB

          25. #define EEPROM_ADDR 0x50 // I2C Buss address of 24LC256 256K EEPROM

          26. Wire.write((int)(eeaddresspage & 0xFF)); // LSB

          27. /*

          28. byte c;

          29. * Read and Write Buffer Page MAX is 28byte / Page

          30. for ( c = 0; c < length; c++)

          31. *

          32. Wire.write(data[c]);

          33. */

          34. Wire.endTransmission();

          35. }

          36. void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data )

          37. byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {

          38. {

          39. byte rdata = 0xFF;

          40. int rdata = data;

          41. Wire.beginTransmission(deviceaddress);

          42. Wire.beginTransmission(deviceaddress);

          43. Wire.write((int)(eeaddress >> 8)); // MSB

          44. Wire.write((int)(eeaddress >> 8)); // Address High Byte

          45. Wire.write((int)(eeaddress & 0xFF)); // LSB

          46. Wire.write((int)(eeaddress & 0xFF)); // Address Low Byte

          47. Wire.endTransmission();

          48. Wire.write(rdata);

          49. Wire.requestFrom(deviceaddress,1);

          50. Wire.endTransmission();

          51. if (Wire.available()) rdata = Wire.read();

          52. }

          53. return rdata;

          54. }

          55. // Address is a page address, 6-bit (63). More and end will wrap around

          56. // But data can be maximum of 28 bytes, because the Wire library has a buffer of 32 bytes

          57. // maybe let's not read more than 30 or 32 bytes at a time!

          58. void i2c_eeprom_write_page

          59. void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {

          60. ( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length )

          61. {

          62. Wire.beginTransmission(deviceaddress);

          63. Wire.beginTransmission(deviceaddress);

          64. Wire.write((int)(eeaddress >> 8)); // MSB

          65. Wire.write((int)(eeaddresspage >> 8)); // Address High Byte

          66. Wire.write((int)(eeaddress & 0xFF)); // LSB

          67. Wire.write((int)(eeaddresspage & 0xFF)); // Address Low Byte

          68. Wire.endTransmission();

          69. byte c;

          70. Wire.requestFrom(deviceaddress,length);

          71. for ( c = 0; c < length; c++)

          72. int c = 0;

          73. Wire.write(data[c]);

          74. for ( c = 0; c < length; c++ )

          75. Wire.endTransmission();

          76. if (Wire.available()) buffer[c] = Wire.read();

          77. delay(10); // need some delay

          78. }

          79. }

          80. byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress )

          81. {

          82. byte rdata = 0xFF;

          83. void setup()

          84. Wire.beginTransmission(deviceaddress);

          85. {

          86. Wire.write((int)(eeaddress >> 8)); // Address High Byte

          87. char somedata[] = "this is data from the eeprom"; // data to write

          88. Wire.write((int)(eeaddress & 0xFF)); // Address Low Byte

          89. Wire.begin(); // initialise the connection

          90. Wire.endTransmission();

          91. Serial.begin(9600);

          92. Wire.requestFrom(deviceaddress,1);

          93. i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM

          94. if (Wire.available()) rdata = Wire.read();

          95. return rdata;

          96. }

          97. delay(10); //add a small delay

          98. // should not read more than 28 bytes at a time!

          99. Serial.println("Memory written");

          100. void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length )

          101. }

          102. {

          103. void loop()

          104. Wire.beginTransmission(deviceaddress);

          105. {

          106. Wire.write((int)(eeaddress >> 8)); // Address High Byte

          107. int addr=0; //first address

          108. Wire.write((int)(eeaddress & 0xFF)); // Address Low Byte

          109. byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory

          110. Wire.endTransmission();

          111. Wire.requestFrom(deviceaddress,length);

          112. while (b!=0)

          113. //int c = 0;

          114. {

          115. for ( int c = 0; c < length; c++ )

          116. Serial.print(addr, HEX); //print content to serial port

          117. if (Wire.available()) buffer[c] = Wire.read();

          118. Serial.print(" : "); //print content to serial port

          119. }

          120. Serial.print(b); //print content to serial port

          121. Serial.print(" : "); //print content to serial port

          122. void setup()

          123. Serial.println((char)b); //print content to serial port

          124. {

          125. addr++; //increase address

          126. Wire.begin(); // join I2C bus (address optional for master)

          127. b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory

          128. Serial.begin(9600);

          129. }

          130. Serial.println(" ");

          131. // TESTS FOR EACH FUNCTION BEGIN HERE

          132. delay(2000);

          133. Serial.print("Writing Test:");

          134. for (int i=0; i<20; i++){ // loop for first 20 slots

          135. }

          136. i2c_eeprom_write_byte(EEPROM_ADDR,i,i+65); // write address + 65 A or 97 a

          137. Serial.print((char)(i+65));

          138. Serial.print(" ");

          139. delay(10); // NEED THIS DELAY!

          140. }

          141. Serial.println("");

          142. delay(500);

          143. Serial.print("Reading Test:");

          144. for (int i=0; i<20; i++){ // loop for first 20 slots

          145. Serial.write(i2c_eeprom_read_byte(EEPROM_ADDR, i));

          146. Serial.print(" ");

          147. }

          148. // setup for page tests . . .

          149. byte PageData[30]; // array that will hold test data for a page

          150. byte PageRead[30]; // array that will hold result of data for a page

          151. for (int i=0; i<30; i++){ // zero both arrays for next test

          152. PageData[i] = 0;

          153. PageRead[i] = 0;

          154. }

          155. Serial.println("\n");

          156. for (int i=0; i<30; i++) PageData[i] = i+33; // fill up array for next test char 33 = !

          157. Serial.print("Writing Page Test:");

          158. for (int i=0; i<28; i++){ // zero both arrays for next test

          159. Serial.print((char)PageData[i]);

          160. Serial.print(" ");

          161. }

          162. Serial.println("\n");

          163. i2c_eeprom_write_page(EEPROM_ADDR, 100, PageData, 28 ); // 28 bytes/page is max

          164. Serial.print("Reading Page Test:");

          165. i2c_eeprom_read_buffer( EEPROM_ADDR, 100, PageRead, 28);

          166. for (int i=0; i<28; i++){

          167. Serial.write(PageRead[i]); // display the array read

          168. Serial.print(" ");

          169. }

          170. }

          171. void loop()

          172. {

          173. }