PDA

View Full Version : I2C eeprom read write



retepsnikrep
- 13th July 2010, 08:02
I'm experiencing a problem with a corrupt byte in my AT24C512B 512K (65,536 x 8) eeprom code.




TempRom: 'Read & Store 2 x I2C DS18B20 Temp Sensor Rom Codes

debug 27,67,"Ins 1st sensor",10,13,"Any key to scan" 'Video Display

pause 2000 'Pause for 2 seconds

LoopA: 'Loop here to wait for button press
gosub MenuButtons 'Gosub MenuButtons gets button data (0,1,2,3,4,5 = no button) in VarC2

If VarC2 = 5 then LoopA 'If no button pressed goto loopA

OWOUT DigitalTemp, 1, [$33] 'Issue Read ROM command
OWIN DigitalTemp, 0, [STR I2C1\8] 'Read 64-bit device data into the 8-byte array "I2C1"

debug 27,67,"Ins 2nd sensor",10,13,"Any key to scan",10,13 'Video Display

pause 2000 'Pause for 2 seconds

LoopB: 'Loop here to wait for button press
gosub MenuButtons 'Gosub MenuButtons gets button data (0,1,2,3,4,5 = no button) in VarC2

If VarC2 = 5 then LoopB 'If no button pressed goto loopB

OWOUT DigitalTemp, 1, [$33] 'Issue Read ROM command
OWIN DigitalTemp, 0, [STR I2C2\8] 'Read 64-bit device data into the 8-byte array "I2C2"

'Save 2 x I2C Temp sensor data into first 16 bytes of AT24C512B External EEPROM

VarA = 1
I2CWRITE Scdeeprom,Sckeeprom,AT24C512B,VarA,[STR I2C1\8] 'Load data into I2c eeprom
pause 10 'Pause for 10ms

VarA = 9
I2CWRITE Scdeeprom,Sckeeprom,AT24C512B,VarA,[STR I2C2\8] 'Load data into I2c eeprom

For VarA = 1 to 16 'Load I2C1 data from eeprom
I2CREAD Scdeeprom,Sckeeprom,AT24C512B,VarA,[VarB1] 'Read data from I2c eeprom
DEBUG HEX2 VarB1," "
Next VarA


The code allows a user to connect two DS18B20 temp sensors in turn to the input, the program reads and then stores the device data into the external eeprom.

This data is then used elsewhere to facilitate the reading of the sensors when both are connected at the same time. This works fine If i manually type in the device data.

I added some check code to simply display what was stored in the eeprom immediately after the devices had been interogated, and this shows that the first byte of sensor 1 is being corrupted and always returns a $10. I tried moving the data store up a byte to avoid the eeprom first location and data is still corrupted on return.

So in esscence the code works for the sensor 2 and that is stored correctly, but for the first sensor the first byte is always $10 the rest of the 8 bytes for that sensor is fine?

Very strange and ideas?

Could it be something to do with the device needing a dummy write before it can start reading?

From the data sheet



RANDOM READ: A random read requires a “dummy” byte write sequence to load in the data
word address. Once the device address word and data word address are clocked in and
acknowledged by the EEPROM, the microcontroller must generate another start condition. The
microcontroller now initiates a current address read by sending a device address with the
Read/Write select bit high. The EEPROM acknowledges the device address and serially clocks
out the data word. The microcontroller does not respond with a “0” but does generate a following
stop condition

mackrackit
- 13th July 2010, 08:59
Maybe it is an ATMEL / PIC conflict :rolleyes:


Could it be something to do with the device needing a dummy write before it can start reading?
I2C read/write takes care of that.

What PIC and what pins is this thing connected to. ADC pin maybe??

retepsnikrep
- 13th July 2010, 09:33
8mhz int osc 16F886 pic (pin 14 Clock & 15 Data used)

I can read and write to the eeprom it's just the first byte which is being corrupted?

mackrackit
- 13th July 2010, 10:18
Interesting...

but for the first sensor the first byte is always $10 the rest of the 8 bytes for that sensor is fine?
Maybe it is the sensor?

Have you tried a simple test read/write? Maybe something like this to see if it is the ROM?
http://www.picbasic.co.uk/forum/content.php?r=165-Serial-EEPROM-Part-1

retepsnikrep
- 13th July 2010, 11:40
It's not the I2C sensors i have tried several. If i scan them as sensor 2 then the code is stored correctly, if i scan them as sensor one the first byte is corrupted when stored I tried moving the eeprom to store starting at a different location other than address 0 this still returns a corrupt first byte but works otherwise. very odd.

retepsnikrep
- 13th July 2010, 13:14
Further testing reveals the codes are being stored correctly in the eeprom and can be read back and displayed with this code.

This writes the codes correctly to the eeprom



VarA = 0
I2CWRITE Scdeeprom,Sckeeprom,AT24C512B,VarA,[STR I2C1\8,STR I2C2\8] 'Load data into I2c eeprom



This check works even after powering on/off so the data is in the eeprom ok and can be read back.



For VarA = 0 to 15 'Load I2C1 data from eeprom
I2CREAD Scdeeprom,Sckeeprom,AT24C512B,VarA,[VarB1] 'Read data from I2c eeprom
DEBUG HEX2 VarB1
Next VarA


However trying to put the codes back into the array variables with this code.



VarA = 0
I2CREAD Scdeeprom,Sckeeprom,AT24C512B,VarA,[STR I2C1\8,STR I2C2\8] 'Read data from I2c eeprom


Appears to work correctly but the first byte of I2C1 array is always corrupted? Could someone try and replicate the fault please.

retepsnikrep
- 14th July 2010, 17:49
Even when i manually fill the first byte of the array I2C(0) it is corrupted when next read strange.