tenzin
December 31, 2016, 3:00pm
1
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;
}
system
December 31, 2016, 3:03pm
2
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)
tenzin
December 31, 2016, 3:07pm
3
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..
system
December 31, 2016, 3:10pm
4
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.
tenzin
December 31, 2016, 3:14pm
5
can you suggest any proper library for 24LC256 EEPROM with aeduino..
thank you
tenzin
December 31, 2016, 9:31pm
7
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
system
December 31, 2016, 9:33pm
8
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.
tenzin
December 31, 2016, 9:37pm
9
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?
system
December 31, 2016, 9:41pm
10
Can you explain your arithmetic there, please?
do you mean retaking memory location of EEPROM?
I have no idea what that means.
tenzin
December 31, 2016, 9:51pm
11
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..
system
December 31, 2016, 9:59pm
12
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.
tenzin
December 31, 2016, 10:31pm
14
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?
tenzin
December 31, 2016, 10:37pm
15
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....
system
December 31, 2016, 10:38pm
16
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.