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