PDA

View Full Version : External Eeprom - trying to store multiple variables



bluesmoke
- 5th April 2010, 00:43
I am struggling with an eeprom 24lc256. I can read/write a one word variable to it. My test example below has the variable increasing in increments of 3 and being stored in sequential addresses. That worked OK. But what I'm struggling with is storing multiple variables.

This code worked OK:


DPIN var PORTD.4 ' I2C data pin
CPIN var PORTD.5 ' I2C clock pin
I2CAddress var Word 'Changed to WORD

B1 var Byte
B2 var Word

Start

Pause 500

debug 13,10,"Begin "

B1 = 0
B2 = 0

Pause 1000

For I2CAddress = 0 To 600 step 2 ' Number of Steps equals number of bytes of data (so we don't overwrite)
I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B2] ' Write each location's address to itself
Pause 10 ' Delay 10ms after each write
B1 = B1 + 1
B2 = B2 + 3
Next I2CAddress

mainloop: For I2CAddress = 0 To 600 step 2 ' Loop
I2CREAD DPIN,CPIN,$A0,I2CAddress,[B2] ' Read 2 locations in a row
debug 13,10, " Data ",Dec B2
Pause 10

Next I2CAddress


debug 13,10,"END ",13,10


END


To store multiple variables I tried this: (Changed step 2 to Step 2 and added B1)



For I2CAddress = 0 To 600 step 3 ' Number of Steps equals number data bytes (so we don't overwrite)
I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B1,B2] ' Write to locations on Eeprom
Pause 10 ' Delay 10ms after each write
B1 = B1 + 1
B2 = B2 + 3
Next I2CAddress

mainloop: For I2CAddress = 0 To 600 step 3 ' Loop
I2CREAD DPIN,CPIN,$A0,I2CAddress,[B1,B2] ' Read 2 locations in a row
debug 13,10, "Data 1 ", Dec B1," Data 2 ",Dec B2
Pause 10

Next I2CAddress



And I tried this: (Changed step 2 to step 3, added B1 with plus high byte/low byte)




For I2CAddress = 0 To 600 step 3 ' Number of Steps equals number data bytes (so we don't overwrite)
I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B1,B2.byte0,B2.byte1] ' Write to locations on Eeprom
Pause 10 ' Delay 10ms after each write
B1 = B1 + 1
B2 = B2 + 3
Next I2CAddress

mainloop: For I2CAddress = 0 To 600 step 3 ' Loop
I2CREAD DPIN,CPIN,$A0,I2CAddress,[B1,B2.byte0,B2.byte1] ' Read 2 locations in a row
debug 13,10, "Data 1 ", Dec B1," Data 2 ",Dec B2
Pause 10

Next I2CAddress


It sort-of worked but there were some errors. Here's the output:



Data 1 0 Data 2 16128 <---- wrong!
Data 1 1 Data 2 3
Data 1 2 Data 2 6
Data 1 3 Data 2 9
Data 1 4 Data 2 12
Data 1 5 Data 2 15
Data 1 6 Data 2 18
Data 1 7 Data 2 21
Data 1 8 Data 2 24
Data 1 9 Data 2 27
Data 1 10 Data 2 30
Data 1 11 Data 2 33
Data 1 12 Data 2 36
Data 1 13 Data 2 39
Data 1 14 Data 2 42
Data 1 15 Data 2 45
Data 1 16 Data 2 48
Data 1 17 Data 2 51
Data 1 18 Data 2 54
Data 1 19 Data 2 57
Data 1 20 Data 2 60
Data 1 21 Data 2 32352 <---- wrong!
Data 1 22 Data 2 66
Data 1 23 Data 2 69



I am missing something... but what?
Thanks for your help.

aratti
- 5th April 2010, 06:53
You cannot write or read a word directly as you are trying to do. Split the word into two bytes (lowbyte + highbyte)


I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B2.byte0,B2.byte1] ' Write each location's address to itself


same thing for I2CREAD.

Edited: Sorry I didn't read the whole post so I didn't see you have already splitted the word into two bytes. But the point remains You CANNOT handle the word as a single piece.


Al.

ralfmayr
- 5th April 2010, 10:43
Hi,
i had same problem, i do not calc in the write command:
a = B1.highbyte
b = B1.lowbyte
I2CWRITE sda, scl, add, loc, [a,b]
Ralf

bluesmoke
- 5th April 2010, 23:23
Thanks for the help.

I really am not sure what exactly is/was wrong. The only way for me to make it work is to write one byte on each I2CWRITE command. If I attempted to write more than one variable at a time there would be the occasional error in the written data. Maybe it's a wiring issue (although the mistakes were always at the same data points....?)

Here's what worked:



For x = 1 To 200 ' Number of Steps equals number of bytes of data (so we don't overwrite)
I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B1.highbyte] ' Write each location's address to itself
I2CAddress = I2CAddress + 1
Pause 10
I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B1.lowbyte] ' Write each location's address to itself
I2CAddress = I2CAddress + 1
Pause 10
I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B2.highbyte] ' Write each location's address to itself
I2CAddress = I2CAddress + 1
pause 10
I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B2.lowbyte] ' Write each location's address to itself
I2CAddress = I2CAddress + 1
pause 10
I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B3.highbyte] ' Write each location's address to itself
I2CAddress = I2CAddress + 1
Pause 10 ' Delay 10ms after each write
I2CWRITE DPIN,CPIN,$A0,I2CAddress,[B3.lowbyte] ' Write each location's address to itself
I2CAddress = I2CAddress + 1
Pause 10 ' Delay 10ms after each write
B1 = B1 + 1
B2 = B2 + 3
B3 = B3 + 5
pause 10
Next x

aratti
- 6th April 2010, 01:45
Note: Page write operations are limited to writing
bytes within a single physical page,
regardless of the number of bytes actually
being written. Physical page boundaries
start at addresses that are integer
multiples of the page buffer size (or ‘page
size’) and end at addresses that are
integer multiples of [page size – 1]. If a
Page Write command attempts to write
across a physical page boundary, the
result is that the data wraps around to the
beginning of the current page (overwriting
data previously stored there), instead of
being written to the next page, as might be
expected. It is, therefore, necessary for the
application software to prevent page write
operations that would attempt to cross a
page boundary.


The above text comes from the device datasheet and is the answer to your problem.

Al.

bluesmoke
- 6th April 2010, 02:07
Al,

You're exactly right. Thanks.

I was seeing data errors at specific intervals, specifically every multiple of 64. This explains it!

'Physical page boundaries start at addresses that are integer multiples of the page buffer size'