Go Down

Topic: Read HEX from i2c eprom (Read 3588 times) previous topic - next topic

DirtGambit

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.

Code: [Select]

void setup(){
 
  Serial.begin(9600);
  Wire.begin ();
  byte CT[] = {1000};
  byte CR[] = {3017};

    EP.writeEEPROM(0x50, 0x0, CT, sizeof CT);
    delay(10);
    unsigned int t = EP.readEEPROM (0x50, 0x0, CT, sizeof CT);

    EP.writeEEPROM(0x50, 0x4, CR, sizeof CR);
    delay(10);
    unsigned int v = EP.readEEPROM (0x50, 0x4, CR, sizeof CR);

  Serial.println(v, HEX);
  Serial.println(t, HEX);

  for(int i = 0; i < 0x1f; i++){
  Serial.println(EP.readEEPROM(0x50, i), HEX);
  delay(50);
}
}


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

Regards

PaulS

Code: [Select]
  byte CT[] = {1000};
  byte CR[] = {3017};


The first thing that you need to do is to learn the range of values that can be stored in a byte.

Quote
Im trying to store 0x1000 at address 0x0 and 0x3017 at address 0x4

Next, you need to understand something about numbering systems, and bases. 1000 != 0x1000.

Finally, you need to learn to post ALL of your code. What is an EP?

The art of getting good answers lies in asking good questions.

DirtGambit

#2
Sep 05, 2011, 03:15 pm Last Edit: Sep 05, 2011, 03:17 pm by DirtGambit Reason: 1
Quote
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.

Quote
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.

Quote
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.



PaulS

Quote
0-255, The eprom stores it as binary.

Right. So, 1000 and 3017 are not in range. Why are you trying to store them in bytres?

Quote
0x is needed by the compiler so it knows its HEX, base 16.

Where are you using the 0x prefix then? In your mind only doesn't work.

Quote
EP is just an instance of an object

EP is just an instance of a class. That much was obvious. An instance of WHAT class?
The art of getting good answers lies in asking good questions.

DirtGambit

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.

PaulS

Quote
Ok I have put 0x where i declare my two bytes

Where? I don't see any.

Quote
do i need another 2 bytes declared?

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.

Quote
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.
The art of getting good answers lies in asking good questions.

DirtGambit

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 used the code from post http://arduino.cc/forum/index.php/topic,71219.0.html

I created a simple struct with 2 default hex values in them assigned during setup.

Code: [Select]

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.

Code: [Select]

if(id.readEEPROM(0x50, 0x0) == 0 || id.readEEPROM(0x50, 0x0) == 255)
  {
    eeWrite(0,epdef);
    id.Set_CounterT(epdef.CT);
    id.Set_CounterR(epdef.CR);
  }
  else
  {
    eeRead(0,epdef);
    id.Set_CounterT(epdef.CT);
    id.Set_CounterR(epdef.CR);
  }


I feel its a cheat to just use some ones code just to store 2 hex values, I haven't really learned anything from this post apart from cut and paste.




PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

DirtGambit

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

Go Up