Advertisement
manitou

maple software-i2c eeprom.pde

Sep 8th, 2012
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.44 KB | None | 0 0
  1. /*  
  2.   *  24LC128
  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.   *  does maple wire lib enable pullups?    left 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. #include "wirish.h"
  18. #include <Wire.h> //I2C library
  19. // I2C 24LC128 EEPROM Device Identifier 7bits
  20. #define EEPROM_ID (0xA0 >> 1)
  21.  
  22.  
  23. void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
  24.     byte rdata = data;
  25.     Wire.beginTransmission(deviceaddress);
  26.     Wire.send((int)(eeaddress >> 8)); // MSB
  27.     Wire.send((int)(eeaddress & 0xFF)); // LSB
  28.     Wire.send(rdata);
  29.     Wire.endTransmission();
  30.   }
  31.  
  32.   // WARNING: address is a page address, 6-bit end will wrap around
  33.   // also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
  34.   // chip supports 64 byte page write
  35.  
  36.   void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
  37.     Wire.beginTransmission(deviceaddress);
  38.     Wire.send((int)(eeaddresspage >> 8)); // MSB
  39.     Wire.send((int)(eeaddresspage & 0xFF)); // LSB
  40.     byte c;
  41.     for ( c = 0; c < length; c++)
  42.       Wire.send(data[c]);
  43.     Wire.endTransmission();
  44.   }
  45.  
  46.   byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
  47.     byte rdata = 0xFF;
  48.     Wire.beginTransmission(deviceaddress);
  49.     Wire.send((int)(eeaddress >> 8)); // MSB
  50.     Wire.send((int)(eeaddress & 0xFF)); // LSB
  51.     Wire.endTransmission();
  52.     Wire.requestFrom(deviceaddress,1);
  53.     if (Wire.available()) rdata = Wire.receive();
  54.     return rdata;
  55. }
  56.  
  57.   byte i2c_eeprom_read_next( int deviceaddress ) {
  58.     byte rdata ;
  59.     Wire.requestFrom(deviceaddress,1);
  60.     if (Wire.available()) rdata = Wire.receive();
  61.     return rdata;
  62. }
  63.  
  64.   // maybe let's not read more than 30 or 32 bytes at a time!
  65.   //  chip supports unlimited sequential read, address will wrap
  66.  
  67. void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
  68.     Wire.beginTransmission(deviceaddress);
  69.     Wire.send((int)(eeaddress >> 8)); // MSB
  70.     Wire.send((int)(eeaddress & 0xFF)); // LSB
  71.     Wire.endTransmission();
  72.     Wire.requestFrom(deviceaddress,length);
  73.     int c = 0;
  74.     for ( c = 0; c < length; c++ )
  75.       if (Wire.available()) buffer[c] = Wire.receive();
  76.     Wire.endTransmission();
  77. }
  78.  
  79. void setup()
  80. {
  81.     char somedata[] = "this is data from the eeprom"; // data to write
  82.     byte buff[30];
  83.     int i;
  84.     unsigned int t;
  85.  
  86.     Wire.begin(9,5); // sda,scl initialise the connection
  87.     while(!SerialUSB.available() ) {
  88.         SerialUSB.println("hit a key");
  89.                 delay(1500);
  90.     }
  91.     SerialUSB.read();
  92.     i2c_eeprom_write_page(EEPROM_ID, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM
  93.  
  94.     delay(10); //add a small delay
  95.  
  96.     SerialUSB.println("Memory written");
  97.     for(i=0;i< sizeof(buff); i++) buff[i]=i;
  98.     t=micros();
  99.     i2c_eeprom_write_page(EEPROM_ID, 128, buff, sizeof(buff));
  100.     t=micros()-t;
  101.     SerialUSB.print("blk write ");
  102.     SerialUSB.println(t,DEC);
  103. }
  104.  
  105. void loop()
  106. {
  107.     int i,addr=0; //first address
  108.     byte buff[30];
  109.     unsigned int t;
  110.  
  111.     byte b = i2c_eeprom_read_byte(EEPROM_ID, 0); // access the first address from the memory
  112.     SerialUSB.println(b,HEX);
  113.  
  114.     while (b!=0xff)
  115.     {
  116.       SerialUSB.print((char)b); //print content to serial port
  117.       addr++; //increase address
  118.       b = i2c_eeprom_read_byte(EEPROM_ID, addr); //access an address from the memory
  119.     }
  120.     SerialUSB.println(" ");
  121.     delay(2000);
  122.     i2c_eeprom_read_byte(EEPROM_ID, 0);  // reset to address 0
  123.     b=i2c_eeprom_read_next(EEPROM_ID);
  124.     SerialUSB.println(b,HEX);
  125.     b=i2c_eeprom_read_next(EEPROM_ID);
  126.     SerialUSB.println(b,HEX);
  127.     t=micros();
  128.     i2c_eeprom_read_buffer(EEPROM_ID, 0,buff,sizeof(buff));
  129.     t=micros()-t;
  130.     SerialUSB.print("blk read ");
  131.     SerialUSB.println(t,DEC);
  132.     t=micros();
  133.     for (i=0;i< sizeof(buff); i++){
  134.         buff[i]=i2c_eeprom_read_next(EEPROM_ID);
  135.     }
  136.     t=micros()-t;
  137.     SerialUSB.print("loop read ");
  138.     SerialUSB.println(t,DEC);
  139.     delay(2000);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement