24LC256 EEPROM

Hi i am new to this EEPROM library

here i am facin problem reading the data from 24LC256 EEPROM
I write value 10000 in EEPROM but i get some other value while reading data from it…

heres my codes

#include <Wire.h>

#define EEPROM_I2C_ADDRESS 0x50

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

long address = 80;
long val = 100000;

writeAddress(address, val);
long readVal = readAddress(address);

Serial.print("The returned value is ");
Serial.println(readVal);
}

void loop()
{
//nothing empty loop
}

void writeAddress(long address, long val)
{
Wire.beginTransmission(EEPROM_I2C_ADDRESS);
Wire.write((long)(address >> 8)); // MSB
Wire.write((long)(address & 0xFF)); // LSB

Wire.write(val);
Wire.endTransmission();

delay(5);
}

long readAddress(long address)
{
long rData = 0xff;

Wire.beginTransmission(EEPROM_I2C_ADDRESS);

Wire.write((long)(address >> 8)); // MSB
Wire.write((long)(address & 0xFF)); // LSB
Wire.endTransmission();

Wire.requestFrom(EEPROM_I2C_ADDRESS, 1);

rData = Wire.read();

return rData;
}

You're only reading and writing bytes, aren't you?
(Maybe it would be easier to tell, if it weren't for all the smilies)

Yes i m..but i am sending values in decimal form..
i cant find proper source to learn about this 24LC256 with arduino..
And no idea how they used those library.. no idea about many function they used..

The processor doesn’t care if you’re using octal, decimal or hex. As far as it is concerned, its all binary.
But 10000 doesn’t fit in a byte.

can you suggest any proper library for 24LC256 EEPROM with aeduino…

thank you

(deleted)

even i make to location but how...base on your logic then max will be 500 rite..

and how can you say its max value is 255..i am very new to this concept...can you guide me in this external 24LC256 EEPROM working function..because i cant understand the logic of code and many o library function..

please guide me. i try google many reference but all are not so clear about the working of library function in arduino

base on your logic then max will be 500 rite..

Wrong. Two bytes can store 0 to 65535, or -32768 to +32767.

Binary arithmetic 101 needs retaking.

but i am using long data types and it should store max 9 ,223,372,036,854,775,807 value..
do you mean retaking memory location of EEPROM?

Can you explain your arithmetic there, please?

do you mean retaking memory location of EEPROM?

I have no idea what that means.

okay there is two function i called each function with value in constructor , one for reading and other writing
all variable uses long data types..

why should i make two location and recalling...?

can u clarify your answer please..

thank you in advance...happy new year..

Please can you start using language we all understand?
A long variable will usually comprise 32 bits, givng a range of 0 to 4,294,967,295, or -2,147,483,648 to 2,147,483,647

A long is 4 bytes, so you have to write 4 bytes to eeprom. And obviously read 4 back.

yeah that i know..but Spycatcher2K said max value is 255 and i am sending 100000 value and he suggest to use to location...how come.i don't understand...

i am sorry if u feel bad about my language..

what is your suggestion on this?

i really don't understand the read method..my code writes the value 100000 to eeprom but when i read that value and print it it gives me 165....

log2(255) = 7.99. So you need at least eight bits to store it.

log2(100000) = 16.6. So, you need at least 17 bits to store it.

my code writes the value 100000 to eeprom

Does it?

100000 = 0x186A0

0xA0 = 160. Coincidence?

Your long variable is 4 bytes long. You need to write them one by one to eeprom

  long address = 80;
  long val = 100000;

  writeAddress(address + 0, (byte)(val));
  writeAddress(address + 1, (byte)(val >> 8));
  writeAddress(address + 2, (byte)(val >> 16));
  writeAddress(address + 3, (byte)(val >> 24));

Next you can read the bytes back, recombine them and print the result.

  long readVal = 0;
  for (int cnt = 0; cnt <4; cnt++)
  {
    byte b = readAddress(address + cnt);

    Serial.print("The read byte is ");
    Serial.println(b, HEX);

    readVal |= ((long)b << (cnt * 8));

  }

  Serial.print("The returned value is ");
  Serial.println(readVal, HEX);

Code needs consistency improvements but does the trick.