Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 24LC128 version with Crenn's beta Wire library
- * http://ww1.microchip.com/downloads/en/devicedoc/21191m.pdf
- * http://arduino.cc/playground/Code/I2CEEPROM
- * id EEPROM_ID 1010
- * EEPROM is wired to SDA/SCL maple pin 9 as SDA and pin 5 as SCL
- * maple default pins 20,21 but no header on 21
- * careful: this library wants 7 bit device id
- * don't write too many times!
- * left side in ground rail
- * grnd 1 5 3.3v circle 1 left notch at top
- * grnd 2 6 grnd or float
- * grnd 3 7 SCL maple 5 4.7K pullup
- * grnd 4 8 SDA maple 9 4.7K pullup
- */
- //#define _TESTHARD_
- #ifdef _TESTHARD_
- #include "HardWire.h"
- HardWire I2cPort(1); // port 2 29,30 clk,sda ,I2C_FAST_MODE for 400KHz
- // port 1 5,9 clk,sda
- #else
- #include "Wire.h"
- TwoWire I2cPort(5,9); // clk,sda or ,SOFT_FAST
- #endif
- // I2C 24LC128 EEPROM Device Identifier 7bits
- #define EEPROM_ID (0xA0 >> 1)
- #define I2CBUFSIZ 30
- void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
- byte rdata = data;
- I2cPort.beginTransmission(deviceaddress);
- I2cPort.send((int)(eeaddress >> 8)); // MSB
- I2cPort.send((int)(eeaddress & 0xFF)); // LSB
- I2cPort.send(rdata);
- I2cPort.endTransmission();
- }
- // 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
- // chip supports 64 byte page write
- void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
- I2cPort.beginTransmission(deviceaddress);
- I2cPort.send((int)(eeaddresspage >> 8)); // MSB
- I2cPort.send((int)(eeaddresspage & 0xFF)); // LSB
- byte c;
- for ( c = 0; c < length; c++)
- I2cPort.send(data[c]);
- I2cPort.endTransmission();
- }
- byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
- byte rdata = 0xFF;
- I2cPort.beginTransmission(deviceaddress);
- I2cPort.send((int)(eeaddress >> 8)); // MSB
- I2cPort.send((int)(eeaddress & 0xFF)); // LSB
- I2cPort.endTransmission();
- I2cPort.requestFrom(deviceaddress,1);
- if (I2cPort.available()) rdata = I2cPort.receive();
- return rdata;
- }
- byte i2c_eeprom_read_next( int deviceaddress ) {
- byte rdata ;
- I2cPort.requestFrom(deviceaddress,1);
- if (I2cPort.available()) rdata = I2cPort.receive();
- return rdata;
- }
- // maybe let's not read more than 30 or 32 bytes at a time!
- // chip supports unlimited sequential read, address will wrap
- void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
- I2cPort.beginTransmission(deviceaddress);
- I2cPort.send((int)(eeaddress >> 8)); // MSB
- I2cPort.send((int)(eeaddress & 0xFF)); // LSB
- I2cPort.endTransmission();
- I2cPort.requestFrom(deviceaddress,length);
- int c = 0;
- for ( c = 0; c < length; c++ )
- if (I2cPort.available()) buffer[c] = I2cPort.receive();
- I2cPort.endTransmission();
- }
- void setup()
- {
- char somedata[] = "this is data from the eeprom"; // data to write
- byte buff[I2CBUFSIZ];
- int i;
- unsigned int t;
- while(!SerialUSB.available() ) {
- SerialUSB.println("hit a key");
- delay(1500);
- }
- SerialUSB.read();
- I2cPort.begin();
- i2c_eeprom_write_page(EEPROM_ID, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM
- delay(10); //delay for write to complete
- SerialUSB.println("Memory written");
- for(i=0;i< sizeof(buff); i++) buff[i]=i;
- t=micros();
- i2c_eeprom_write_page(EEPROM_ID, 128, buff, sizeof(buff));
- t=micros()-t;
- SerialUSB.print("blk write ");
- SerialUSB.println(t,DEC);
- delay(10); //delay for write to complete
- }
- void loop()
- {
- int i,addr=0; //first address
- byte buff[I2CBUFSIZ];
- unsigned int t;
- byte b = i2c_eeprom_read_byte(EEPROM_ID, 0); // access the first address from the memory
- SerialUSB.println(b,HEX);
- while (b!=0xff)
- {
- SerialUSB.print((char)b); //print content to serial port
- addr++; //increase address
- b = i2c_eeprom_read_byte(EEPROM_ID, addr); //access an address from the memory
- }
- SerialUSB.println(" ");
- delay(2000);
- i2c_eeprom_read_byte(EEPROM_ID, 0); // reset to address 0
- b=i2c_eeprom_read_next(EEPROM_ID);
- SerialUSB.println(b,HEX);
- b=i2c_eeprom_read_next(EEPROM_ID);
- SerialUSB.println(b,HEX);
- t=micros();
- i2c_eeprom_read_buffer(EEPROM_ID, 0,buff,sizeof(buff));
- t=micros()-t;
- SerialUSB.print("blk read ");
- SerialUSB.println(t,DEC);
- t=micros();
- for (i=0;i< sizeof(buff); i++){
- buff[i]=i2c_eeprom_read_next(EEPROM_ID);
- }
- t=micros()-t;
- SerialUSB.print("loop read ");
- SerialUSB.println(t,DEC);
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement