Writing to external EEPROM

Shack

Member
I am new. I don't know how to write a word value to an external eeprom and then get the value back correctly so I can display it on an LCD screen. I know the mechanics of writing and reading the EEProm, but I don't understand how to break the Word value into two Byte values for storage.

Example:

Word value = 33347
What are the two byte values (MSB and LSB)I need to write?

When I read the values, how do I put them back together to get back to my original Word value for a display.

Thank you
 

hippy

Technical Support
Staff member
b0 and b1 are the bytes which make up w0. b2 and b3 make up w1 and so on. The even numbered 'b' variables are the lsb's of the word variables.

Because b0 and b1 are the parts of w0 ( alter one and the other alters also) to split word w0 into two bytes you don't have to do anything, just use b0 and b1, likewise if you have b0 and b1 you also have w0.

So to write w0 using I2C and read it back ...

-- w0 = 33347
-- I2cWrite adr,(b0,b1)
-- w0 = 0
-- I2cRead adr,(b0,b1)

Your w0 will be restored back to 33347. Bingo !
 

Shack

Member
Thank you sir. I was trying to make it a LOT more complicated!

I understand that so far .... what are the actual values that are written to the device as b0 and b1? That is the part I am not understanding. So if I just read b0 for example what will be returned?
 
Last edited:

BeanieBots

Moderator
In the example w0=33347
b0 would be 67 and b1 would be 130
if you only read back b0 (and b1 was 0), then w0 would be 67
if you only read back b1 (and b0 was 0) then w0 would be 33280

That's because w0=256*b1+b0
A byte (eg b0) can be any number between 0 and 255
if you add 1 to 255 it rolls over back to 0.
if a word is 255 and you add 1, the first byte (b0) rolls over back to 0 but the second byte (b1) gets one added.
Similar in decimal 9+1 = 10 but with bytes 255+1=256
shown in hex it becomes more obvious $00FF+1=$0100
 
Top