Error to read eeprom
dar
Posts: 15
Hi:
I'm trying to write/read eeprom, and i have a problem:
When i try to read a eepromLocation and i go to the next location, it doesn`t read a correct data, but if i increase the location 4 positions, it read right data.
Can you tell me why it happens?
The code i'm using:
if i2cObject.devicePresent(SCL,eepromAddr) == true
debug.str(string("EEPROM Present"))
eeprom_present:=1
else
debug.str(string("EEPROM Missing"))
waitcnt(clkfreq*3+cnt)
eepromData2:=100.23
if eeprom_present==1
eepromLocation := EEPROM_Base
eepromData2 := 0
repeat 10
' write long
eepromData2 :=100.23
i2cObject.WriteLong(SCL, EEPROMAddr, eepromLocation, eepromData2)
' next
eepromLocation +=1
' slowit
waitcnt(clkfreq/2 + cnt)
eepromLocation := EEPROM_Base
repeat 10
' read long
eepromData :=0
eepromData := i2cObject.ReadLong(SCL, EEPROMAddr, eepromLocation)
' debug
debug.str(string("Read Location="))
debug.dec(eepromLocation)
debug.NewLine
debug.str(ftos.FloatToString(eepromData))
'debug.dec(eepromData)
debug.NewLine
' next
eepromLocation +=1
' slowit
waitcnt(clkfreq/2 + cnt)
When i do this, only the last eepromLocation read 100.23, but if i change and increase eepromLocation+=4 then all eepromLocation read the right data. (100.23)
Thanks
I'm trying to write/read eeprom, and i have a problem:
When i try to read a eepromLocation and i go to the next location, it doesn`t read a correct data, but if i increase the location 4 positions, it read right data.
Can you tell me why it happens?
The code i'm using:
if i2cObject.devicePresent(SCL,eepromAddr) == true
debug.str(string("EEPROM Present"))
eeprom_present:=1
else
debug.str(string("EEPROM Missing"))
waitcnt(clkfreq*3+cnt)
eepromData2:=100.23
if eeprom_present==1
eepromLocation := EEPROM_Base
eepromData2 := 0
repeat 10
' write long
eepromData2 :=100.23
i2cObject.WriteLong(SCL, EEPROMAddr, eepromLocation, eepromData2)
' next
eepromLocation +=1
' slowit
waitcnt(clkfreq/2 + cnt)
eepromLocation := EEPROM_Base
repeat 10
' read long
eepromData :=0
eepromData := i2cObject.ReadLong(SCL, EEPROMAddr, eepromLocation)
' debug
debug.str(string("Read Location="))
debug.dec(eepromLocation)
debug.NewLine
debug.str(ftos.FloatToString(eepromData))
'debug.dec(eepromData)
debug.NewLine
' next
eepromLocation +=1
' slowit
waitcnt(clkfreq/2 + cnt)
When i do this, only the last eepromLocation read 100.23, but if i change and increase eepromLocation+=4 then all eepromLocation read the right data. (100.23)
Thanks
Comments
Each location in EEPROM can hold an 8-bit byte value. ReadLong and WriteLong take a long word (32-bits) and break it up into four 8-bit bytes that are stored in four successive EEPROM locations starting at the address specified. That's why your program only works properly when the EEPROM address is incremented by 4. That's how many bytes it takes to store the value.
You can use ReadByte and WriteByte to read and write a single 8-bit byte if you want.
Larry,
It looks like dario was displaying the float value. He used this line of code:
So it looks like "ftos" is some sort of float to string object.
I'm guessing the "100.23" is displaying properly.
I only want to know why i can't to read a location in eeprom, i didn't think that a float it's 32 bits.