interfacing arduino to 24C65

Hi guys.

I'm new to arduino, and am working on a project which will heavily involve using i2c protocals. As such I wanted to work out the basic read/write functions. So I connected it to a 24C65 EEPROM chip, and using an example I found on this forum, tried to write to and then read from the external memory chip. My code is below:

#include <Wire.h>
#define CHIP B1010000

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  
  Wire.beginTransmission(CHIP);
  Wire.send(0); //sets memory pointer to 0
  Wire.send(2); // sends value 2 to that location
  Wire.endTransmission();
  delay(1000);
  Wire.beginTransmission(CHIP);
  Wire.send(0x02); // moves pointer to memory location 02
  Wire.send(2);      // sends value two to that location
  Wire.endTransmission();
  delay(1000);
  Wire.beginTransmission(CHIP);
  Wire.send(0x03);
  Wire.send(2);
  Wire.endTransmission();
  delay(1000);
  
  Wire.beginTransmission(CHIP);
  Wire.send(0);
  Wire.endTransmission();
  Wire.requestFrom(CHIP,8);
  while(Wire.available())
  {
    char c = Wire.receive();
    Serial.print(c,HEX);
    Serial.print(", ");
  }


  
  
}

Now, I've got some partial success on the read side, in so much as it does read the required amount of bytes, if I ask it to read 8 bytes, I get 8 values in the serial window, if I ask it to read 16 bytes I get 16 values in the serial monitor etc, etc.

The problem I have is that it always reads the same value, -1 (in DEC), despite what else I try to write to those memory locations.

Can anyone help as to what I am doing wrong?

Cheer sfor any help.

your line:
Wire.send(0); //sets memory pointer to 0
doesnt seem to be ok.

it sends a byte, but in order to set the memory address on your i2c eeprom, you need to send a 2 bytes long address, e.g. an int, since int for arduino is 16bit (2-byte) long.

quoting from an i2c eeprom datasheet:

Each data byte in the memory has a 16-bit (two
byte wide) address. The Most Significant Byte (Table
4) is sent first, followed by the Least significant
Byte (Table 5). Bits b15 to b0 form the address of
the byte in memory. Bit b15 is treated as Don't
Care bits on the M24256-A memory.

so, you need to send a 16bit memory address, for example, to write a byte you can:
int memory_address = 0; // this is 16bit long
Wire.beginTransmission(i2c_address);
Wire.send((byte)(memory_address >> 8)); // first the MSB (8-bits)
Wire.send((byte)memory_address); // then the LSB (8-bits)
Wire.send(data);
Wire.endTransmission();

Sv1eed is right.

Look at this code : Arduino Playground - I2CEEPROM

I don't understand this :

  Wire.send((int)(eeaddresspage >> 8)); // MSB
  Wire.send((int)(eeaddresspage & 0xFF)); // LSB

Why a cast (int) as Wire.send() takes a byte (as explained by Sv1eed) and what is the need for "& 0xFF" ? There is a short note about 6-bit end will wrap around..

These questions are only for culture, as both code may work.

what is the need for "& 0xFF"

This is a bit wise AND operation and zeros all bits except for the 8 least significant bits.

Thanks guys, got it working.

Cheers