I2C example Help!

Hello,

I just ordered 10 pieces of the AT24C512 eproms but i need some help. I saw the IC2 on the playground from the arduino but i want some examples that i can use as test and work it out. :-X

Please Help!

Manny Manny Manny Thanks!!!!

Christian

What is wrong with this code?Arduino Playground - I2CEEPROM

Over at Uchobby they have a great article about I2C, recomended reading :

https://web.archive.org/web/20190609170522/http://www.uchobby.com/index.php/2008/09/16/introduction-to-i2c/

What is wrong with this code?http://www.arduino.cc/playground/Code/I2CEEPROM

Hello Franklin & MikMo,

Nothing i know, but i dont know how to use the subs :-[ im not yet a super star in C :slight_smile: YET. Im more in the Perl. It would be cool if there where some working examples that uses the subs so that i can see how its working.

And thanks for the url!!

Thanks,

Christian

I can't remember if I found this or wrote it - but it is a sketch that demonstrates how to use the functions documented in that playground link.

// i2CEPROM.pde

#include <Wire.h>

#define CHIP_SELECT_BITS 0b000 
#define ADR_24LC128 0b1010000 
#define chipAddress (ADR_24LC128 | CHIP_SELECT_BITS)

void setup(){
   Serial.begin(19200);
   Wire.begin();  
}

int count = 0;
int eeaddress = 0 ; // the starting address in memory, not the chip select address
char buffer[32]; 

void loop(){
    Serial.println("starting loop ");
    Serial.print("reading ");
    for(int i=0; i < 8; i++){
       char c =  i2c_eeprom_read_byte(chipAddress,eeaddress + i );
       Serial.print(c);
    }
    Serial.println();
    
    for(int i=0; i < 8; i++){
      byte data = 'M' + i;
       i2c_eeprom_write_byte(chipAddress, eeaddress + i, data );
    }
    Serial.print("reading ");
    for(int i=0; i < 8; i++){
       char c =  i2c_eeprom_read_byte(chipAddress,eeaddress + i );
       Serial.print(c);
    }
    Serial.println();
    Serial.print("reading block: ");
    memset(buffer,0, sizeof(buffer));
    i2c_eeprom_read_buffer( chipAddress, eeaddress, (byte*)buffer, 8 );
    Serial.println((char*)buffer);

    strcpy(buffer,"World -"); 
    i2c_eeprom_write_block( chipAddress,eeaddress, (byte*)buffer,  8 ) ;

    Serial.println();
    Serial.print("reading block: ");
    memset(buffer,0, sizeof(buffer));
    i2c_eeprom_read_buffer( chipAddress, eeaddress, (byte*)buffer, 8 );
    Serial.println((char*)buffer);
      
    Serial.println("done - waiting ten minutes");
    delay(600000);
    

 //  Serial.print("writing block ");
 //  i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length )    
    
}

void i2c_eeprom_write_block( int deviceaddress, unsigned int eeaddress, byte* data, byte length ) {

  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8)); // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  while(length--){
     Wire.send(*data++);
  }
//  Wire.endTransmission();
  int ret = Wire.endTransmission();
  Serial.print(" End returned ");
  Serial.print(ret,DEC);
}


void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8)); // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  Wire.send(data);
  //Wire.send(data);
  //Serial.print("sending ");
  //Serial.println(data);
  int ret = Wire.endTransmission();
  Serial.print("End returned ");
  Serial.print(ret,DEC);

}

// 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
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddresspage >> 8)); // MSB
  Wire.send((int)(eeaddresspage & 0xFF)); // LSB
  byte c;
  for ( c = 0; c < length; c++)
    Wire.send(data[c]);  
  int ret = Wire.endTransmission();
  Serial.print("End returned ");
  Serial.print(ret,DEC);
  
}

byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
  byte rdata = 0xFF;
  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8)); // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
  Wire.requestFrom(deviceaddress,1);
  if (Wire.available()) rdata = Wire.receive();
  return rdata;
}

// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
  Wire.beginTransmission(deviceaddress);
  Wire.send((int)(eeaddress >> 8)); // MSB
  Wire.send((int)(eeaddress & 0xFF)); // LSB
  Wire.endTransmission();
  Wire.requestFrom(deviceaddress,length);
  int c = 0;
  for ( c = 0; c < length; c++ )
    if (Wire.available()) buffer[c] = Wire.receive();
}

Thank you Mem,

Ps are you realy a mem?? or?!

Thanks,

Christian

Ps are you realy a mem?? or?!

Not sure what you are referring to, if you are asking if I am the Egyptian god of water, then no :wink:

MEM are the initial letters of my first, middle and last name

regards,

Michael

Hello Michael,

Hehehe thanks, thats all what i wanted to know :slight_smile: Thanks for your help!!!

Greetings,

Christian