eeprom confusion

moey

Member
I'm new to eeproms and I just don't get it.
Trying a 24LC256 with 18X.
There's no problem in writing and reading on value at a time, but I just can't successfully write and read 2 variables at once.

I would expect the test code below to look like this when reading the eeprom. (Or something like this).
0,0,10
1,1,11
2,2,12
3,3,13
4,4,14
5,5,15
6,6,16
7,7,17
8,8,18
9,9,19
10,10,20

Instead it reads back like this.
(Using the Program Editor f9 function)
0,1,2,
1,2,3,
2,3,4,
3,4,5,
4,5,6,
5,6,7,
6,7,8,
7,8,9,
8,9,10,
9,10,11,
10,11,12,

'TEST CODE
i2cslave %10100000,i2cslow,i2cword
high 0 'write protect EEPROM

b0=0:b1=10 'initial values
main:
for b11=0 to 50
if pin2 = 1 then PLAYBACK
low 0 'write enable eeprom
b0=b0+1:b1=b1+1 'increment b0 & b1

writei2c w6,(b0,b1) 'write the values
pause 10 'wait EEPROM write time

w6=w6+1 'increment eeprom location
next

finished:
if pin2 = 1 then PLAYBACK
pause 100
goto finished

PLAYBACK:
high 0
pause 1000

for w6=0 to 10
readi2c w6,(b0,b1) 'read b0 & b1 from eeprom
sertxd(#w6,44,#b0,44,#b1,13,10) 'send b0 & b1 to terminal

next
end
 
Last edited by a moderator:

andrewpro

New Member
took me a few minutes to figger out what was wrong. For a bit there, I thought it was just voodoo cause it looke dlike it should be working!! hehe.

But then I saw it. Your writing two bytes at a time. That's fine, as the chip auto increments the address. This, however is where you get shot in the foot!

When you increment the address, W6, manually (well..programatically) you only increment it by one. In essence your doing this:

W6 is equal to 0
Write "1" to address 0
chip auto increments address
write "11" to address 1
increase W6 by one to --ONE---


Now remember, the chip, by auto-incrementing, already filled up address one with "11". At this point, your loop runs again, and this is what happens (continued from the top):

address 0 on the chip already contains "1"
address 1 on the chip already contains "11"
set the address to write to adress 1
---Here, it overwrites the contents of address 1---


In short, what it boils down to, is you need to increment W6 by 2, not 1. If your going to write to the chip using the address auto-incrementing feature, your program needs to take into account how much data your writing at a time.

Change W6=W6+1 to W6=W6+2 and it should work correctly.

--Andy P
 
Top