Advertisement
manitou

crenn maple i2c eeprom sketch

Sep 13th, 2012
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.80 KB | None | 0 0
  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);        // 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);       // 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 ;
  72.     I2cPort.requestFrom(deviceaddress,1);
  73.     if (I2cPort.available()) rdata = I2cPort.receive();
  74.     return rdata;
  75. }
  76.  
  77.   // maybe let's not read more than 30 or 32 bytes at a time!
  78.   //  chip supports unlimited sequential read, address will wrap
  79.  
  80. void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
  81.     I2cPort.beginTransmission(deviceaddress);
  82.     I2cPort.send((int)(eeaddress >> 8)); // MSB
  83.     I2cPort.send((int)(eeaddress & 0xFF)); // LSB
  84.     I2cPort.endTransmission();
  85.     I2cPort.requestFrom(deviceaddress,length);
  86.     int c = 0;
  87.     for ( c = 0; c < length; c++ )
  88.       if (I2cPort.available()) buffer[c] = I2cPort.receive();
  89.     I2cPort.endTransmission();
  90. }
  91.  
  92. void setup()
  93. {
  94.     char somedata[] = "this is data from the eeprom"; // data to write
  95.     byte buff[I2CBUFSIZ];
  96.     int i;
  97.     unsigned int t;
  98.  
  99.     while(!SerialUSB.available() ) {
  100.         SerialUSB.println("hit a key");
  101.                 delay(1500);
  102.     }
  103.     SerialUSB.read();
  104.     I2cPort.begin();
  105.     i2c_eeprom_write_page(EEPROM_ID, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM
  106.  
  107.     delay(10); //delay for write to complete
  108.  
  109.     SerialUSB.println("Memory written");
  110.     for(i=0;i< sizeof(buff); i++) buff[i]=i;
  111.     t=micros();
  112.     i2c_eeprom_write_page(EEPROM_ID, 128, buff, sizeof(buff));
  113.     t=micros()-t;
  114.     SerialUSB.print("blk write ");
  115.     SerialUSB.println(t,DEC);
  116.     delay(10); //delay for write to complete
  117. }
  118.  
  119. void loop()
  120. {
  121.     int i,addr=0; //first address
  122.     byte buff[I2CBUFSIZ];
  123.     unsigned int t;
  124.  
  125.     byte b = i2c_eeprom_read_byte(EEPROM_ID, 0); // access the first address from the memory
  126.     SerialUSB.println(b,HEX);
  127.  
  128.     while (b!=0xff)
  129.     {
  130.       SerialUSB.print((char)b); //print content to serial port
  131.       addr++; //increase address
  132.       b = i2c_eeprom_read_byte(EEPROM_ID, addr); //access an address from the memory
  133.     }
  134.     SerialUSB.println(" ");
  135.     delay(2000);
  136.     i2c_eeprom_read_byte(EEPROM_ID, 0);  // reset to address 0
  137.     b=i2c_eeprom_read_next(EEPROM_ID);
  138.     SerialUSB.println(b,HEX);
  139.     b=i2c_eeprom_read_next(EEPROM_ID);
  140.     SerialUSB.println(b,HEX);
  141.     t=micros();
  142.     i2c_eeprom_read_buffer(EEPROM_ID, 0,buff,sizeof(buff));
  143.     t=micros()-t;
  144.     SerialUSB.print("blk read ");
  145.     SerialUSB.println(t,DEC);
  146.     t=micros();
  147.     for (i=0;i< sizeof(buff); i++){
  148.         buff[i]=i2c_eeprom_read_next(EEPROM_ID);
  149.     }
  150.     t=micros()-t;
  151.     SerialUSB.print("loop read ");
  152.     SerialUSB.println(t,DEC);
  153.     delay(2000);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement