crenn

I2C EEPROM code

Sep 14th, 2012
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  
  2.  *  24LC128   version with Crenn's beta Wire library
  3.  *  http://ww1.microchip.com/downloads/en/devicedoc/21191m.pdf
  4.  *  http://arduino.cc/playground/Code/I2CEEPROM
  5.  *    id EEPROM_ID 1010
  6.  *   EEPROM is wired to SDA/SCL maple  pin 9 as SDA and pin 5 as SCL
  7.  *    maple default pins 20,21 but no header on 21
  8.  *  careful: this library wants 7 bit device id
  9.  *                     don't write too many times!
  10.  *      left side in ground rail
  11.  *   grnd 1    5   3.3v       circle 1  left  notch at top
  12.  *   grnd 2    6       grnd or float
  13.  *   grnd 3    7 SCL  maple 5  4.7K pullup
  14.  *   grnd 4    8 SDA  maple 9  4.7K pullup
  15.  */
  16.  
  17. #define _TESTHARD_
  18.  
  19. #ifdef _TESTHARD_
  20.  
  21. #include "HardWire.h"
  22. HardWire I2cPort(1, I2C_FAST_MODE);            // port 2   29,30 clk,sda  ,I2C_FAST_MODE for 400KHz
  23. // port 1   5,9 clk,sda
  24.  
  25. #else
  26.  
  27. #include "Wire.h"
  28. TwoWire I2cPort(5,9,SOFT_FAST);       // clk,sda     or  ,SOFT_FAST
  29. #endif
  30.  
  31. // I2C 24LC128 EEPROM Device Identifier 7bits
  32. #define EEPROM_ID (0xA0 >> 1)
  33. #define I2CBUFSIZ 30
  34.  
  35.  
  36. void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
  37.   byte rdata = data;
  38.   I2cPort.beginTransmission(deviceaddress);
  39.   I2cPort.send((int)(eeaddress >> 8)); // MSB
  40.   I2cPort.send((int)(eeaddress & 0xFF)); // LSB
  41.   I2cPort.send(rdata);
  42.   I2cPort.endTransmission();
  43. }
  44.  
  45. // WARNING: address is a page address, 6-bit end will wrap around
  46. // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  47. // chip supports 64 byte page write
  48.  
  49. void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte data[], byte length ) {
  50.   I2cPort.beginTransmission(deviceaddress);
  51.   I2cPort.send((int)(eeaddresspage >> 8)); // MSB
  52.   I2cPort.send((int)(eeaddresspage & 0xFF)); // LSB
  53.   byte c;
  54.   for ( c = 0; c < length; c++)
  55.     I2cPort.send(data[c]);
  56.   I2cPort.endTransmission();
  57. }
  58.  
  59. byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
  60.   byte rdata = 0xFF;
  61.   I2cPort.beginTransmission(deviceaddress);
  62.   I2cPort.send((int)(eeaddress >> 8)); // MSB
  63.   I2cPort.send((int)(eeaddress & 0xFF)); // LSB
  64.   I2cPort.endTransmission();
  65.   I2cPort.requestFrom(deviceaddress,1);
  66.   if (I2cPort.available()) rdata = I2cPort.receive();
  67.   return rdata;
  68. }
  69.  
  70. byte i2c_eeprom_read_next( int deviceaddress ) {
  71.   byte rdata = 0x00;
  72.   I2cPort.requestFrom(deviceaddress, 1);
  73.   if (I2cPort.available() > 0) {
  74.     rdata = I2cPort.receive();
  75.   }
  76.   return rdata;
  77. }
  78.  
  79. // maybe let's not read more than 30 or 32 bytes at a time!
  80. //  chip supports unlimited sequential read, address will wrap
  81.  
  82. void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
  83.   I2cPort.beginTransmission(deviceaddress);
  84.   I2cPort.send((int)(eeaddress >> 8)); // MSB
  85.   I2cPort.send((int)(eeaddress & 0xFF)); // LSB
  86.   I2cPort.endTransmission();
  87.   I2cPort.requestFrom(deviceaddress,length);
  88.   for (int c = 0; c < length; c++ )
  89.     if (I2cPort.available()) buffer[c] = I2cPort.receive();
  90. }
  91.  
  92. void setup()
  93. {
  94.   char somedata[] = "this is data from the eeprom"; // data to write
  95.   byte buff[I2CBUFSIZ];
  96.   unsigned int t;
  97.  
  98.   while(SerialUSB.available() == 0 ) {
  99.     SerialUSB.println("hit a key");
  100.     delay(1500);
  101.   }
  102.   SerialUSB.read();
  103.   I2cPort.begin();
  104.   i2c_eeprom_write_page(EEPROM_ID, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM
  105.  
  106.   delay(10); //delay for write to complete
  107.  
  108.   SerialUSB.println("Memory written");
  109.   for(int i = 0; i < sizeof(buff); i++) {
  110.     buff[i] = i;
  111.   }
  112.   t = micros();
  113.   i2c_eeprom_write_page(EEPROM_ID, 128, buff, sizeof(buff));
  114.   t = micros() - t;
  115.   SerialUSB.print("blk write ");
  116.   SerialUSB.println(t,DEC);
  117.   delay(10); //delay for write to complete
  118. }
  119.  
  120. void loop()
  121. {
  122.   int i,addr=0; //first address
  123.   byte buff[I2CBUFSIZ];
  124.   unsigned int t;
  125.  
  126.   byte b = i2c_eeprom_read_byte(EEPROM_ID, 0); // access the first address from the memory
  127.   SerialUSB.println(b,HEX);
  128.  
  129.   while (b!=0xff)
  130.   {
  131.     SerialUSB.print((char)b); //print content to serial port
  132.     addr++; //increase address
  133.     b = i2c_eeprom_read_byte(EEPROM_ID, addr); //access an address from the memory
  134.   }
  135.   SerialUSB.println(" ");
  136.   delay(2000);
  137.   i2c_eeprom_read_byte(EEPROM_ID, 0);  // reset to address 0
  138.   b=i2c_eeprom_read_next(EEPROM_ID);
  139.   SerialUSB.println(b,HEX);
  140.   b=i2c_eeprom_read_next(EEPROM_ID);
  141.   SerialUSB.println(b,HEX);
  142.   t=micros();
  143.   i2c_eeprom_read_buffer(EEPROM_ID, 0,buff,sizeof(buff));
  144.   t=micros()-t;
  145.   SerialUSB.print("blk read ");
  146.   SerialUSB.println(t,DEC);
  147.   t=micros();
  148.   for (i=0;i< sizeof(buff); i++){
  149.     buff[i]=i2c_eeprom_read_next(EEPROM_ID);
  150.   }
  151.   t=micros()-t;
  152.   SerialUSB.print("loop read ");
  153.   SerialUSB.println(t,DEC);
  154.   delay(2000);
  155. }
RAW Paste Data