BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Fri Aug 17, 2018 11:20 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Help with I2C EEPROM before I go crazy
PostPosted: Mon Jul 18, 2011 1:13 am 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
Ok, here is the issue. I have known good 24C04 and 24C32 EEPROMS. I have an older project that I used IDE with the 24C04 and it worked fine. Here is some test code for IDE that writes 0-100 to the first 100 memory locations then reads them back and displays them in the terminal window.

-------
temp var byte
promin var byte
address var word
temp = 0
address = 0
cbit var byte
cbit =%10100000

SEROUT s_out, i38400, [0, "starting", 13]

FOR address = 0 to 100
cbit.bit1=address.bit8
cbit.bit2=address.bit9
i2cout p14,p15,outerror,cbit,address,[temp]
pause 25
temp=temp+1
next

for address = 0 to 100
cbit.bit1 = address.bit8:cbit.bit2=address.bit9
i2cin p14,p15,inerror,cbit,address,[promin]
serout s_out, i38400, [dec address," Memory location = ",dec promin, 13]
next

goto all_done

outerror:
serout s_out, i38400, ["We had a write error", 13]
goto all_done

inerror:
serout s_out, i38400, ["We had a read error", 13]

all_done:
END
---------

The above test returns the expected values (0-100) with the 24C04. However when using a 24C32, I don't get any write or read error, but I get a result of 255 for all memory locations.

Ok, so I look up some code examples here and they don't work with IDE, so I try Micro Studio. My original code does not work at all with BMS, so I use some of the examples I found and come up with this. I again write 0-100 to the first 100 memmory locations and then read it back and display it in a terminal window.

------
temp var byte
promin var byte
address var word
temp = 0
address = 0
cbit con %10100000

SEROUT s_out, i38400, [0, "starting", 13]

FOR address = 0 to 100
i2cout p14, p15, outerror, cbit, [address.byte1, address.byte0, temp]
temp=temp+1
pause 25
next

FOR address = 0 to 100
i2cout p14, p15, outerror, cbit, [address.byte1, address.byte0]
i2cin p14, p15, inerror, cbit, [promin]
serout s_out, i38400, [dec address," Memory location = ",dec promin, 13]
next

goto all_done

outerror:
serout s_out, i38400, ["We had a write error", 13]
goto all_done

inerror:
serout s_out, i38400, ["We had a read error", 13]

all_done:
end
------

Now this code works fine with the 24C32, but not with the 24C04. I get a jump to the read error routine. If I leave off the read error check, I get incorrect values for the 24C04. So why the heck won't it work with both EEPROMS? And why does IDE with with the 24C04 and not the 24C32 and BMS works with the 24C32 and not the 24C04? How can I get it to work with both? If the same code won't work for both EEPROMS how can I get the 24C04 to work with BMS?

<edit>
I can get rif of the 24C04 read error with BMS by putting a pause in between these lines

i2cout p14, p15, outerror, cbit, [address.byte1, address.byte0]
pause 25
i2cin p14, p15, inerror, cbit, [promin]

However, it still returns the wrong data on the reads. And I can verify that data is not being written correctly either because I have a stand alone eprom burner that lets me read/write the chips. If I program in all zeros and then run the program, I will get some random 'garbage' in the first couple of bytes and that is it.


Top
 Profile  
 
 Post subject: Re: Help with I2C EEPROM before I go crazy
PostPosted: Mon Jul 18, 2011 3:33 am 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
Ok, I got it working (I think), but I need someone to help me understand what is going on. When you don't do this stuff every day, it can be frustrating.

Between looking over the examples given here and reading the manual here is what I found.

For the 24C04 write, I need to use:

i2cout p14,p15, %10100000,[address, temp]

and for a read:

i2cout p14, p15, %10100000, [address]
i2cin p14, p15, %10100000, [promin]

For the 24C32 write, I need to use:

i2cout p14, p15, %10100000, [address.byte1, address.byte0, temp]

and for a read:

i2cout p14, p15, %10100000, [address.byte1, address.byte0]
i2cin p14, p15,%10100000, [promin]

So the 24C04 balks at the 24C32 addressing and vice versa. I'm assumimng that internally the 24C32 is looking for the address to come in as low/high bytes and the 24C04 isn't.


Top
 Profile  
 
 Post subject: Re: Help with I2C EEPROM before I go crazy
PostPosted: Mon Jul 18, 2011 7:43 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
You have it exactly. The first I2C eeproms were very small and they only used one byte addressing. Later they added more memory and started using the 3 address bits in the control byte(%10101XXX0) to expand the memory size to upto 8*256 bytes. Then they came out with 2 byte addressing. And there are even chips that have 2 byte addressing and use the 3 address bits in the control byte to expand that to upto 8 times 65536 bytes. All of this happened over a very long time and they did not make them backwards compatible. You have to know what chip you are talking to to do it write. :(

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Help with I2C EEPROM before I go crazy
PostPosted: Mon Jul 18, 2011 7:59 pm 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
Yeah, I think I finally got it all sorted out. On the 24C04, I never used more than about 100 bytes or so, and I just now figured out that you have to change the control byte from %10100000 to %10100010 to switch to the second 256 byte bank. :? At least the newer larger sized ones seem to be more straight forward and the 3 control bits %1010xxx0 are used for addressing the chip it's self, and not the innards. :lol: Thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO