PRoblem writing to a 24LC256 EEPROM

After I write to the EEPROM I read from the same address and the value read is allways 0.
I am using the following code for a 28X1 chip:
MEMDEVICE = %10100000 	'First 24LC256 EEPROM, Second will be %10100010 and Third %10100100
	CLKDEVICE = %11010000 	' RTC DS3232
	MEMAD = 0				
	i2cslave CLKDEVICE,i2cfast,i2cbyte    	'Select RTC DS3232 
	readi2c $00,(b0,b1)					'Read seconds and minutes
	i2cslave MEMDEVICE,i2cfast,i2cword		'Select EEPRM
	writei2c MEMAD, (b0,b1)				'Store time
	pause 500
		'Measure temperature					
     High CS							'idle
     Low SCK
     Low CS							'start transmission sequence
     DAT = 0
     For N = 1 to 16
        High SCK
        DAT = DAT * 2 + MISO
        Low SCK
     Next N
     High CS							'terminate the transmission
     DAT = DAT / 8						'use only the 13 most sig bits
     writei2c MEMAD, (DAT)
     readi2c MEMAD, (DAT)
     Pause 500


Senior Member
Some ideas to get started:

- Do you have pull-up resistors on the I2C lines? I.e. 2.2 kOhm going from SDA to 5V and from SCL to 5V? (the exact value is not very critical, something between 2 and 5 kOhm should work fine). Forgetting those will mean the lines are always low so you will always read 0.

- can you read from the RTC? I.e. do you get anything other than 0 from it?



Senior Member
Have you checked that the byte you are writing isn't actually zero ?

It is usually a good idea to start with some simple code which just writes and reads in a FOR-NEXT and reports success or failure. Get each individual part of the program proven and working, then merge the parts together.
Womai, yes I have 4K7 resistors in place (in my schematic) but I have not built the circuit. I am running the code in the simulator.
Hippy I did that. There are other pieces of data the are stored in the same eeprom. as follows:
minutes (one byte) (DS3232)
seconds (one byte) (DS3232)
temperature from a thermocouple through a MAX6675 (one word)
pulses from a light sensor switch (one word)
analog signal from a pitot tube (one word)
When I run the code in the simulator, data is stored in the EEPROM and I can read it back. Except the temperature
Last edited:


Senior Member
@ Marmitas : It might just be me but I'm extremely cautious of results when emulating external hardware, it could easily be that the hardware isn't being emulated entirely accurately, and particularly with respect to timing.

One thing you should try; put a "DAT=$A5" immediately before the Write and see what comes back. If you read back $A5 then it seems to be working, if not, something could be broken within the simulator.

One thing I have just noticed is that you do a Write-Read-Pause. You probably want to move the Pause to after the Write to give the Eeprom time to have updated before trying to read back ( Write-Pause-Read ).

I'd have expected an Eeprom to have returned $FF if an attempt were made to read while it was internally updating, although maybe the read aborts the update so the update never actually gets underway. Behaviour could well be different depending on whether it's simulated or a real chip.
Last edited:


New Member
I'm with hippy ont he simulation part.

As far as reading goes with the chip (aside form the simulator), if it wasn't working you'd get all 1's (FF) because of the pullups. so you're reading the chip correctly. If there is nothing stored in the location, you could get either old data, or 0's. Or something in between, I've had them get flaky like that on me when brand new. (again, this is outside of the simulator..i.e. actual hardware).

I would concentrate on the clock chip, rather than the memory.

--Andy P


I agree that it's probably time to move up to a real circuit.
Also, at the bottom of the snippet you don't have a pause after writing to EEPROM.


Technical Support
Staff member
Dippy is correct, i2c eeproms don't respond correctly when they are internally writing. Most take 5 or 10ms to write, so add a 'pause 10' between the write and read. And try testing with a fixed value as Hippy sugeest!
Thank you gentlemen

I would concentrate on the clock chip, rather than the memory.

--Andy P
Did you see somthing wrong regarding the RTC?

Thanks to everyone for the advice. I will breadboard the circuit and try a live run rather than the simulated run of the code.