Since MrDEB couldn't be arrsed to try the simple experiment I suggested in posts 46 and 50, I did them. Why? Because I wasn't certain how things would work, and it was easy enough to give it a try and learn a few things in the process.
In part 1, I saved byte values in 4 adjacent EEPROM locations. Reading the values back into a byte variable yielded the results expected. Reading the values back into a word variable yielded garbage of course, because each EEPROM location is byte-sized, and when a word is read, two adjacent bytes are read as a word.
In part 2, I saved word variables, each containing a byte value, into adjacent EEPROM locations. This is of course wrong, as a word requires two bytes. Reading these locations back as bytes yielded the expected numbers, but this is a happy circumstance of the order in which they were written, because stored a word writes in two EEPROM locations. It might look ok during a quick test, but it will yield unexpected results.
Reading these values back into a word variable resulted in garbage, as this reads adjacent cells as a word.
In part 3, I wrote a byte-sized word variable into every other cell. Reading back the values as bytes shows the expected result, the byte-sized number in one cell and a zero in the other (since the value stored was less than 256). Reading the word value from the expected location yielded the expected results.
When word values were read from the wrong location of the pair, garbage was the result, as byte values from two diffent words were combined un the wrong order.
Part 4 repeated part 3 with values greater than 255 (i.e., greater than byte-sized) with the expected results.
Thd output of the tests is shown here. This picture shows the EEPROM values as read by the PICkit 2.
In part 1, I saved byte values in 4 adjacent EEPROM locations. Reading the values back into a byte variable yielded the results expected. Reading the values back into a word variable yielded garbage of course, because each EEPROM location is byte-sized, and when a word is read, two adjacent bytes are read as a word.
In part 2, I saved word variables, each containing a byte value, into adjacent EEPROM locations. This is of course wrong, as a word requires two bytes. Reading these locations back as bytes yielded the expected numbers, but this is a happy circumstance of the order in which they were written, because stored a word writes in two EEPROM locations. It might look ok during a quick test, but it will yield unexpected results.
Reading these values back into a word variable resulted in garbage, as this reads adjacent cells as a word.
In part 3, I wrote a byte-sized word variable into every other cell. Reading back the values as bytes shows the expected result, the byte-sized number in one cell and a zero in the other (since the value stored was less than 256). Reading the word value from the expected location yielded the expected results.
When word values were read from the wrong location of the pair, garbage was the result, as byte values from two diffent words were combined un the wrong order.
Part 4 repeated part 3 with values greater than 255 (i.e., greater than byte-sized) with the expected results.
Thd output of the tests is shown here. This picture shows the EEPROM values as read by the PICkit 2.
Code:
EEPROM Test
Part 1 - Data written as bytes
EE.0 = 42, EE.1 = 43, EE.2 = 44, EE.3 = 45, stored as bytes.
Read as bytes
Addr0: 42
Addr1: 43
Addr2: 44
Addr3: 45
Read as words
Addr0: 11050
Addr1: 11307
Addr2: 11564
Addr3: 65325
===========================================================
Part 2 - Data written as words, in adjacent locations
EE.4 = 42, EE.5 = 43, EE.6 = 44, EE.7 = 45, stored as words.
Note: a word should require 2 locations.
Read as bytes
Addr4: 42
Addr5: 43
Addr6: 44
Addr7: 45
Read as words
Addr4: 11050
Addr5: 11307
Addr6: 11564
Addr7: 45
===========================================================
Part 3 - Data written as words, in every other location
EE.9 = 42, EE.11 = 43, EE.13 = 44, EE.15 = 45, stored as words.
Read as bytes
Addr8: 0
Addr9: 42
Addr10: 0
Addr11: 43
Addr12: 0
Addr13: 44
Addr14: 0
Addr15: 45
Addr16: 0
Read as words, odd cell numbers
Addr9: 42
Addr11: 43
Addr13: 44
Addr15: 45
Read as words, even cell numbers
Addr8: 10752
Addr10: 11008
Addr12: 11264
Addr14: 11520
Addr16: 65280
===========================================================
Part 4 - Data written as words, in every other location
EE.20 = 747, EE.22 = 748, EE.24 = 749, EE.26 = 750, stored as words.
Read as bytes
Addr18: 255
Addr19: 255
Addr20: 235
Addr21: 2
Addr22: 236
Addr23: 2
Addr24: 237
Addr25: 2
Addr26: 238
Addr27: 2
Addr28: 255
Read as words, even cell numbers
Addr18: 65535
Addr20: 747
Addr22: 748
Addr24: 749
Addr26: 750
Addr28: 65535
Read as words, odd cell numbers
Addr19: 60415
Addr21: 60418
Addr23: 60674
Addr25: 60930
Addr27: 65282
Addr29: 65535