Problem getting 24AA512 EEPROM talking

Good night everyone, after getting some samples of the 24AA512 Microchip EEPROM and reading its data-sheet and even a C program for a pic I cant get the EEPROM up and running, can someone help me, or point me to some working code for this EEPROM?
My actual code is this one, its a simple test case and I intended to create a library for other to use this EEPROM.

/*24AA512 Microchip EEPROM
 
 24AA512
 Pin    Function
 1        Adress 0
 2        Adress 1
 3        Adress 2
 4        Vss - Ground
 5        SDA - Serial Data Line 
 6        SCL - Serial Clock Line
 7        WP - Write Protect, active high
 8        Vcc - 1.8v-5.5v
 
 Arduino
 SDA - Analog pin 4
 SCL - Analog pin 5
 
 The adress of the EEPROm is defined by the state of the three adress pins(
 so there can be up to 8 devices in the same bus, in the case of the 512Kbits
 (64Kbytes) devices using 8 of then would result in a total memory amount of 
 512Kbytes, or 4Mbit) and a control nibble, that is given by Microchip.
 The control nibble is 0b1010 or 0xA0, in this code I will assume that A0..A2 are 0
 so the adress will be 0xA0
 */
 
#include <Wire.h>
#define EEPROM_Adress_read 0xA1
#define EEPROM_Adress_write 0xA0
#define Data 0xAA
#define AdressHigh 0x00
#define AdressLow 0x00

void setup(){
  Serial.begin(9600);
  Serial.println("Ready");
  Wire.begin();
  Wire.beginTransmission(EEPROM_Adress_write);
  Wire.send(AdressHigh);
  Wire.send(AdressLow);
  Wire.send(Data);
  Wire.endTransmission();
  Serial.println("Writing done");

  Wire.beginTransmission(EEPROM_Adress_write);
  Wire.send(AdressHigh);
  Wire.send(AdressLow);
  Wire.endTransmission();
  Wire.requestFrom(EEPROM_Adress_read,1);
  Wire.endTransmission();
  
  while(1){
    Serial.println("Loop");
    while(Wire.available()){
      char c = Wire.receive();
      Serial.print(c);
    }
  }
}
void loop(){

}

Here are some functions to read and write to an i2c EEPROM that could easily be wrapped into library:

/*
 * i2CEPROM.pde
 * this version tested with 24LC128
 */
#include <Wire.h>

// all address pins connected to Gnd
const byte EEPROM_ID = 0x50;      // I2C address for 24LC128 EEPROM 

// test sketch to write and then read ascii characters:

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33; 


void setup()
{
  Serial.begin(9600);
  Wire.begin();  

  Serial.println("Writing 1024 bytes to EEPROM"); 
  for(int i=0; i < 1024; i++)
  {   
    i2cEEPROM_Write(i, thisByte);    
    // go on to the next character
    thisByte++; 
    if(thisByte == 126)   // you could also use if (thisByte == '~') 
      thisByte = 33;     // start over
  }

  Serial.println("Reading 1024 bytes from EEPROM");   
  int thisByte = 33; 
  for(int i=0; i < 1024; i++)
  {   
    char c = i2cEEPROM_Read(i);  
    if( c != thisByte)
    {
      Serial.println("read error");
      break;
    }
    else      
    {
      Serial.print(c);
    }
    thisByte++; 
    if(thisByte == 126)  
    {
      Serial.println();
      thisByte = 33;     // start over on a new line
    }
  }
  Serial.println();
}

void loop()
{

}

/ This function is similar to EEPROM.write() 
void i2cEEPROM_Write( unsigned int address, byte data )
{
  Wire.beginTransmission(EEPROM_ID);
  Wire.send((int)highByte(address) ); 
  Wire.send((int)lowByte(address) ); 
  Wire.send(data);
  Wire.endTransmission();
  delay(5); // wait for the I2C EEPROM to complete the write cycle
}

// This function is similar to EEPROM.read() 
byte i2cEEPROM_Read(unsigned int address )
{
  byte data;
  Wire.beginTransmission(EEPROM_ID);
  Wire.send((int)highByte(address) ); 
  Wire.send((int)lowByte(address) ); 
  Wire.endTransmission();
  Wire.requestFrom(EEPROM_ID,(byte)1);
  while(Wire.available() == 0) // wait for data
    ;
  data = Wire.receive();
  return data;
}

Looks like I didn't included the delay and had the address bytes swapped, thanks a lot!!!

After some tweaking in the code, and some reading in the playground i almost have this in a library format, but there are still some errors that I cant correct :frowning:

The code from .h:

#ifndef i2cEEPROM_h
#define i2cEEPROM_h

#include "WProgram.h"

class i2cEEPROM {

      public:
      void begin(uint8_t eeprom_address);
      template <class T> void write(uint16_t address, const T& data);
      template <class T> void read(uint16_t address, T& data);

      private:
      uint8_t _eeprom_address;

}

#endif

The .ccp:

#include <WProgram.h>
#include "Wire.h"
#include "i2cEEPROM.h"

void i2cEEPROM::begin(uint8_t eeprom_address){
      Wire.begin();
      _eeprom_address = eeprom_address;
}

template <class T> void i2cEEPROM::write(uint16_t address, const T& data){
      const byte* p = (const byte*)(const void*)&data;
      uint8_t i=0;
      for(i=0; i<sizeof(data);i++){
              Wire.beginTransmission(_eeprom_address);
              Wire.send((int)address>>8); //High address
              Wire.send((int)address);    //Low address
              Wire.send(*p);
              Wire.endTransmission();
              address++;
              *p++;
              delay(5);
      }
}

template <class T> void i2cEEPROM::read(uint16_t address, T& data){
      byte* p = (byte*)(void*)&data;
      uint8_t i=0;
      for(i=0; i<sizeof(data);i++){
            Wire.beginTransmission((uint8_t)_eeprom_address);
              Wire.send((int)address>>8);  //High address
              Wire.send((int)address);     //Low address
            Wire.endTransmission();
            Wire.requestFrom(_eeprom_address,(uint8_t)1);
            while(!Wire.available());
            *p++ = Wire.receive();
            address++;
      }
}

And the test sketch:

#include <i2cEEPROM.h>

float abc=1.234;
float lido=0.0;

void setup(){
  i2cEEPROM.begin(0x50);
  Serial.begin(9600);
  
  Serial.println("Going to write");
  i2cEEPROM.write(12345,abc);
  Serial.println("Now reading");
  i2cEEPROM.read(12345,lido);
  Serial.println(lido,4);
}

void loop(){
  
}

The errors:

/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:2:18: erro: Wire.h: Ficheiro ou directoria inexistente
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:5: erro: new types may not be defined in a return type
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:5: nota: (perhaps a semicolon is missing after the definition of 'i2cEEPROM')
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:5: erro: two or more data types in declaration of 'begin'
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp: In member function 'void i2cEEPROM::write(uint16_t, const T&)':
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:14: erro: 'Wire' was not declared in this scope
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp: In member function 'void i2cEEPROM::read(uint16_t, T&)':
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:29: erro: 'Wire' was not declared in this scope

One of the worst things is the inability to include the wire.h and thus all the wire functions.

Is it safe to assume that you have the Wire library? Does it appear if you select Sketch + Import Library...? Does the code compile any further if you include Wire.h in the sketch?

I had all this working all in a skecth, but now I want to wrap it in a library and as I follow the library tutorial and trying to compile the the case it keeps saying that it cant find the wire library with is pretty strange, I have already tried <Wire.h> and "Wire.h", is there any special thing needed to do when I want to use an arduino lib inside another lib?

The only special thing that you need to do has to do with the (strange) way that the Arduino decides what you can include and what needs to be compiled. Basically, if you don't include a header file in the sketch (or include something that includes that header file), you can't include it in any cpp files, either.

So, back to the question you didn't answer.

Does the code compile any further if you include Wire.h in the sketch?

First of all, thanks to the help provided!!!
After searching a bit I discovered how to include the wire library, just need to used:

#include <../Wire/Wire.h>
instead of:
#include <Wire.h>

And yes, before discovering this including the #include "Wire.h" compiled the lib a bit more and the present state of errors is this:

/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:5: erro: new types may not be defined in a return type
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:5: nota: (perhaps a semicolon is missing after the definition of 'i2cEEPROM')
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:5: erro: two or more data types in declaration of 'begin'

Any clues?

The is now like this, test program:

#include <i2cEEPROM.h>
float abc=1.234;
float lido=0.0;

i2cEEPROM i2cEEPROM();

void setup(){
  i2cEEPROM.begin(0x50);
  Serial.begin(9600);
  
  Serial.println("Going to write");
  i2cEEPROM.write(12345,abc);
  Serial.println("Now reading");
  i2cEEPROM.read(12345,lido);
  Serial.println(lido,4);
}

void loop(){
  
}

The header file:

#ifndef i2cEEPROM_h
#define i2cEEPROM_h

#include <WProgram.h>
#include <WConstants.h>
#include <../Wire/Wire.h>

class i2cEEPROM {

      public:
      i2cEEPROM();
      void begin(uint8_t eeprom_address);
      template <class T> void write(uint16_t address, const T& data);
      template <class T> void read(uint16_t address, T& data);

      private:
      uint8_t _eeprom_address;

}[glow];[/glow]

#endif

In the arduino libraries that are included with the ide, all the headers files have an ';' where i have highlighted it, is it necessary as I don't really understand it.
And the cpp file:

#include <WProgram.h>
#include <../Wire/Wire.h>
#include <i2cEEPROM.h>

i2cEEPROM::i2cEEPROM(){
}

void i2cEEPROM::begin(uint8_t eeprom_address){
      Wire.begin();
      _eeprom_address = eeprom_address;
}

template <class T> void i2cEEPROM::write(uint16_t address, const T& data){
      const byte* p = (const byte*)(const void*)&data;
      uint8_t i=0;
      for(i=0; i<sizeof(data);i++){
              Wire.beginTransmission(_eeprom_address);
              Wire.send((int)address>>8); //High address
              Wire.send((int)address);    //Low address
              Wire.send(*p);
              Wire.endTransmission();
              address++;
              *p++;
              delay(5);
      }
}

template <class T> void i2cEEPROM::read(uint16_t address, T& data){
      byte* p = (byte*)(void*)&data;
      uint8_t i=0;
      for(i=0; i<sizeof(data);i++){
            Wire.beginTransmission((uint8_t)_eeprom_address);
              Wire.send((int)address>>8);  //High address
              Wire.send((int)address);     //Low address
            Wire.endTransmission();
            Wire.requestFrom(_eeprom_address,(uint8_t)1);
            while(!Wire.available());
            *p++ = Wire.receive();
            address++;
      }
}

And the strange error that I cant understand:

/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:5: erro: new types may not be defined in a return type
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:5: nota: (perhaps a semicolon is missing after the definition of 'i2cEEPROM')
/media/EA14EFF714EFC4A3/Programação/Arduino-0019/libraries/Eeprom_24AAxxx/i2cEEPROM.cpp:5: erro: return type specification for constructor invalid

Any help?

In the arduino libraries that are included with the ide, all the headers files have an ';' where i have highlighted it, is it necessary as I don't really understand it.

The semicolon IS required at the end of a struct or class definition. It's notoriously easy to forget, and difficult to wade through all the errors that are produced when you forget it.

I'm not an expert on templates, but according to what I just read, the class keyword in the template statement needs to be followed by the name of a class. T is not a class that the compiler knows anything about.

I think that you want to be using the typename keyword in place of class.

best to keep things simple. Use a simple class instead of templates unless you have a good reason otherwise.

If you are making a library to share with others, avoid using technology that you have not mastered (or include a statement indicating that the code was a learning exercise)

I have used it so it could save chars, ints, longs, long long, floats, all that with a small code, if someone can teach me how to store floating point in 8bits "pieces" in the eeprom I will change the code.
Can you point me somewhere to read about classes and keywords?
It is indeed a learning exercise and I hope to master it someday.

if someone can teach me how to store floating point in 8bits "pieces" in the eeprom I will change the code.

Floats (and doubles) on Arduino are 32 bit values.
These are the routines I use do convert between float and ints.

union wordToFloat
{
  unsigned int  wordData[2];
  float         floatData;
}  toFloat;

void sendFloat(float val)
{  
  toFloat.floatData = val;
  sendInt(toFloat.wordData[1]);
  sendInt(toFloat.wordData[0]);
}

float readFloat()
{
  toFloat.wordData[1] =  readWord();
  toFloat.wordData[0] =  readWord();  
  return toFloat.floatData;
}

Templates are not commonly used for Arduino libraries and if your intention is to publish your work then it may be better using the familiar form even if it takes a few extra lines of code.

In the union, you can also declare a byte array of size 4 and simply write the bytes to EEPROM, and read them from EEPROM.

I'm not undestanding how that code works :-[
Well, I want to make the library but when it is done I will give credit to everyone who helped me putting it together.