Go Down

Topic: Extenal EEPROM Database 24c04/24c16 (Read 1 time) previous topic - next topic

ashish-k

May 01, 2012, 07:27 pm Last Edit: May 01, 2012, 07:29 pm by ashish-k Reason: 1
I am using AT24C04 with arduino,
my question is, how i can use with EEPROM DATABASE.

working CODE for this ic:
Code: [Select]

#include <Wire.h> // specify use of Wire.h library.

// address in 24ATC04 values from 0 to 511.

int BEGIN = 0;
char* TEST[] = {"Test1","Test2"};

void setup(){
 Wire.begin(); // join i2c bus (address optional for master)
 Serial.begin(9600);  // setup serial for output

   Wire.beginTransmission(0x50); // connect to 24LC04 device address
   Wire.send(BEGIN); // beginning address within EEPROM
   Wire.send(TEST[0]);
   Wire.endTransmission();
   delay(10);


}

void loop(){
 Wire.beginTransmission(0x50); // link to 24LC04
   Wire.send(BEGIN); // must act as a position pointer
   Wire.endTransmission();
   delay(10);
   Wire.requestFrom(ADDR[0], 5); // request 5 bytes from slave device 24LC04
   delay(10);
   // below will loop until 5 bytes are received.

   while(Wire.available()) // slave may send less than requested
   {
char c = Wire.receive(); // receive a byte as character
Serial.print(c); // print the character
   }
   Serial.print("\n"); // next line
  delay(1000); // wait one second.
}


Thank you in advance.
Rock it n loop it.

dxw00d

You'd need to rewrite the library to use the external EEPROM, rather that the internal one.

ashish-k

#2
May 01, 2012, 07:46 pm Last Edit: May 01, 2012, 07:48 pm by ashish-k Reason: 1
Thankx bro for your help, i will look at DB.cpp and db.h
:)
Rock it n loop it.

ashish-k

I've modified EEPROM code and made this,

EEPROM04.cpp
Code: [Select]

#include "WProgram.h"
#include "Wire.h"
#include "EEPROM04.h"

uint8_t EEPROM04Class::read(int address)
{
//Wire.begin();
Wire.beginTransmission(0x50); // link to 24LC04
    Wire.send(address); // must act as a position pointer
    Wire.endTransmission();
    delay(5);
    Wire.requestFrom(0x50,1); // request 1 byte from slave device 24LC04
    delay(5);
char c =Wire.receive();
return c;
}
void EEPROM04Class::write(int address, uint8_t value)
{
//Wire.begin();
    Wire.beginTransmission(0x50); // connect to 24LC04 device address
    Wire.send(address); // beginning address within EEPROM
    Wire.send(value);
    Wire.endTransmission();
    delay(5);
}
EEPROM04Class EEPROM04;


EEPROM04.h
Code: [Select]

#ifndef EEPROM04_h
#define EEPROM04_h
//#include <inttypes.h>
class EEPROM04Class{
  public:
  uint8_t read(int);
  void write(int, uint8_t);
};
extern EEPROM04Class EEPROM04;
#endif


DB04.cpp
Code: [Select]


#include "WProgram.h"
#include "DB04.h"

/**************************************************/
// private functions
int DB04::writeHead()
{
    byte * p = (byte*)(void*)&DB04_head;
int ee = DB04_head_ptr;
    int i;
    for (i = 0; i < (int)sizeof(DB04_head); i++)
      EEPROM04.write(ee++, *p++);
    return i;
}

int DB04::readHead()
{
    byte* p = (byte*)(void*)&DB04_head;
int ee = DB04_head_ptr;
    int i;
    for (i = 0; i < (int)sizeof(DB04_head); i++)
      *p++ = EEPROM04.read(ee++);
    return i;
}

int DB04::EEPROM04_dbWrite(int ee, const byte* p)
{
    int i;
    for (i = 0; i < DB04_head.rec_size; i++)
      EEPROM04.write(ee++, *p++);
    return i;
}

int DB04::EEPROM04_dbRead(int ee, byte* p)

    int i;
    for (i = 0; i < DB04_head.rec_size; i++)
      *p++ = EEPROM04.read(ee++);
    return i;
}

/**************************************************/
// public functions

void DB04::create(int head_ptr, byte recsize)
{
  DB04_head_ptr = head_ptr;
  DB04_head.n_recs   = 0;
  DB04_head.rec_size = recsize;
  writeHead();
}

void DB04::open(int head_ptr)
{
  DB04_head_ptr = head_ptr;
  DB04_tbl_ptr  = head_ptr + DB04_HEAD_SIZE;
  readHead();
}
//other operations commit DB04_head edits to EEPROM so no need for a DB04_close


boolean DB04::write(byte recno, const DB04_Rec rec)
{
  DB04_error = DB04_OK;
  if (recno>0 && recno<=DB04_head.n_recs+1)
    EEPROM04_dbWrite(DB04_tbl_ptr+((recno-1)*DB04_head.rec_size), rec);
  else
    DB04_error = DB04_RECNO_OUT_OF_RANGE;
  return DB04_error==DB04_OK;
}


boolean DB04::read(byte recno, DB04_Rec rec)
{
  DB04_error = DB04_OK;
  if (recno>0 && recno<=DB04_head.n_recs)
    EEPROM04_dbRead(DB04_tbl_ptr+((recno-1)*DB04_head.rec_size), rec);
  else
    DB04_error = DB04_RECNO_OUT_OF_RANGE;
  return DB04_error==DB04_OK;
}


boolean DB04::deleteRec(byte recno)
{
  DB04_error = DB04_OK;
  if (recno<0 || recno>DB04_head.n_recs)
  {  Serial.println("recno out of range");
    DB04_error = DB04_RECNO_OUT_OF_RANGE;
    return false;
  }
  DB04_Rec rec = (byte*)malloc(DB04_head.rec_size);
  for (int i=recno+1; i<=DB04_head.n_recs; i++)
  {
    read(i,rec);
    write(i-1,rec);
  } 
  free(rec);
  DB04_head.n_recs--;
  EEPROM04.write(DB04_head_ptr,DB04_head.n_recs);
  return true;
}


boolean DB04::insert(byte recno, DB04_Rec rec)
{
  DB04_error = DB04_OK;
  if (recno<0 || recno>DB04_head.n_recs)
  {  Serial.println("recno out of range");
    DB04_error = DB04_RECNO_OUT_OF_RANGE;
    return false;
  }
  DB04_Rec buf = (byte*)malloc(DB04_head.rec_size);
  for (int i=DB04_head.n_recs; i>=recno; i--)
  {
    read(i,buf);
    write(i+1,buf);
  }
  free(buf);
  write(recno,rec); 
  DB04_head.n_recs++;
  EEPROM04.write(DB04_head_ptr,DB04_head.n_recs);
  return true;
}

void DB04::append(DB04_Rec rec)
{
  DB04_error = DB04_OK;
  DB04_head.n_recs++;
  write(DB04_head.n_recs,rec);
  EEPROM04.write(DB04_head_ptr,DB04_head.n_recs);
}

byte DB04::nRecs()
{
  return DB04_head.n_recs;
}


DB04.h
Code: [Select]


#ifndef DB04_PROM
#define DB04_PROM

#include "EEPROM04.h"

struct DB04_Header
{
  byte n_recs;
  byte rec_size;
};

// slightly padded for the time being
#define DB04_HEAD_SIZE 4

// DB04_error values
#define DB04_OK 0
#define DB04_RECNO_OUT_OF_RANGE 1

#define DB04_REC (byte*)(void*)&

typedef byte* DB04_Rec;

class DB04 {
  public:
    void    create(int head_ptr, byte recsize);
    void    open(int head_ptr);
    boolean write(byte recno, const DB04_Rec rec);
    boolean read(byte recno, DB04_Rec rec);
    boolean deleteRec(byte recno);                 // delete is a reserved word
    boolean insert(byte recno, const DB04_Rec rec);
    void    append(DB04_Rec rec);
byte   nRecs();
    DB04_Header DB04_head;
    byte DB04_error;
  private:
    int writeHead();
    int readHead();
    int EEPROM04_dbWrite(int ee, const byte* p);
    int EEPROM04_dbRead(int ee, byte* p);
    int DB04_head_ptr;
    int DB04_tbl_ptr;
};

extern DB04 db04;

#endif


So, the EEPROM04 code works well but this DB04 is not working as expected, it gives me an error after uploading "recno out of range" and i scanned the EEPROM and it was empty.

please help me with this DB04 library.


PS: pde attached.
Rock it n loop it.

Go Up