Hi, Im having a bit of trouble storing and reading a hex value from the eprom, here is the code im using to test it out.
Im trying to store 0x1000 at address 0x0 and 0x3017 at address 0x4, Then id like to read them into a variable and pass them to a function.
The values that are stored in the Eprom are as follow
Address Hex Bin
0x0 E8 232
0x1 62 98
0x2 20 32
0x3 20 32
0x4 C9 201
0x5 30 48
0x6 32 50
0x7 33 51
The first thing that you need to do is to learn the range of values that can be stored in a byte.
0-255, The eprom stores it as binary.
Next, you need to understand something about numbering systems, and bases. 1000 != 0x1000.
0x is needed by the compiler so it knows its HEX, base 16.
Finally, you need to learn to post ALL of your code. What is an EP?
EP is just an instance of an object that has access to a function copied and pasted from one of the examples on this site for reading and writing to i2c eprom, i think the dot indicates that the instance of the object is using the function, it works fine with all the other data Im saving and its writing and reading fine, I do need to learn more about using HEX is programming.
Ok I have put 0x where i declare my two bytes, do i need another 2 bytes declared? Because its HEX each 2 positions of the number can add to 255 which is all I can store in a byte, i need to split these HEX numbers into blocks of 2 hex values then store that to a byte in the eprom.
No. You can not store a value that is larger than a byte in an array of bytes sensibly. You have to use a larger data type, like int or long.
Because its HEX each 2 positions of the number can add to 255 which is all I can store in a byte, i need to split these HEX numbers into blocks of 2 hex values then store that to a byte in the eprom.
Personally, I think you are going about this all wrong. Perhaps you need to explain where the data to be stored in the EEPROM is coming from, to get some real help.
Hex, binary, and decimal are all ways of displaying a value. Internally, all data is stored in binary.
I got it working by using one of the librarys that a user wrote on this forum, for writeanything to eprom, Im now able to store the hex value of an address into eprom. Ill try and explain it a bit better. Every 5 minutes a temp/RH value will be stored in the eprom, I needed to keep track where these variables were so if the arduino got switched off the last address where data was stored would be the starting address for when the arduino was switched back on, the starting address for tempc is 0x1000 and starting address for RH is 0x3017 after data is stored the address is incremented and the new address is written to the eprom so when i switch off arduino and switch it back on i have 0x1001 and 0x3018, ill be putting a button on my processing interface to reset this value back to defaults if need be.
I created a simple struct with 2 default hex values in them assigned during setup.
struct EPD //eprom defaults
{
unsigned int CT;
unsigned int CR;
} epdef;
epdef.CT = 0x1000;
epdef.CR = 0x3017;
I then used a simple if loop to check if the address 0x0 contains a 0 or a 255 if it does write 0x1000 and 0x3017 to eprom as starting address for data loggin if not read whats at that address and set the address for storing the temp/RH values.
the starting address for tempc is 0x1000 and starting address for RH is 0x3017 after data is stored the address is incremented and the new address is written to the eprom so when i switch off arduino and switch it back on i have 0x1001 and 0x3018
Are tempC and RH bytes? If not, then the next addresses where to store data are NOT 0x1001 and 0x3018.
In any case, you need to store these addresses in EEPROM, too. And they can not be stored in single bytes. Depending on the size of your EEPROM, they are probably ints.
You can use lowByte() and highByte() to split the addresses into bytes, and store those bytes in the appropriate places in EEPROM, using EEPROM.write().
Multiply the high byte by 256 and add the low byte, with appropriate casting, to get the actual (int) address when retrieving the address values.
Yes the tempc and Rh are bytes becuase Im not representing any negative values just now and nothing above 255, that will change later on, for now just getting the data to the eprom and reading it back correctly is what im working on, Ill have a look at a few examples of highByte() and lowByte() functions to see how others are using it thanks