writing and reading to an external eeprom

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Darren.Smith
Flowcode V4 User
Posts: 38
Joined: Thu Feb 24, 2011 4:38 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

writing and reading to an external eeprom

Post by Darren.Smith »

hi guys, guess what i am a little stuck again, what i am trying to do is write to an external eeprom via i2c, i know i need to start with

start
i2c addres of 0xa0
data
stop

what i need to do is write 8 different variables to the eeprom
my question is would i start as address 0x00 >>8 for each variable. i have been looking thru the data sheet but it does not show a start addr for the eeprom
from what i can work out a page on the eeprom would be 1 variable (8 bit's= 255)
is this correct or am i missing something...
cheers
dazza

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: writing and reading to an external eeprom

Post by kersing »

Darren,

It would help if you specified which eeprom you are trying to use. What is the manufacturer and what is the type?

Looking at the Microchip 24AA08 (random example) you would need to send (for byte write):
start
10100BB0b where BB is a two bit page address
(wait for ack)
word address <-- to address one of the 256 memory locations within the selected page
(wait for ack)
data
(wait for ack)
stop

Regards,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Darren.Smith
Flowcode V4 User
Posts: 38
Joined: Thu Feb 24, 2011 4:38 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: writing and reading to an external eeprom

Post by Darren.Smith »

hi jac,
thank you for your reply, my apologies, i was thinking of using a 24lc02 by microchip. this has only one page of 256 bytes.

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: writing and reading to an external eeprom

Post by kersing »

According to the datasheet the sequence is as I posted in the previous message. Just use 00 for BB (block address). (Actually any value is valid as the block address bits are 'don't care')

So to write a single byte:
start
10100000b
(wait for ack)
word address <-- to address one of the 256 memory locations within the selected page
(wait for ack)
data
(wait for ack)
stop

The easiest way to write 8 variables is to create a write to memory macro and call it 8 times with the values of the different variables and a unique address. The eeprom has 256 bytes memory (addressable with the word address)
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Darren.Smith
Flowcode V4 User
Posts: 38
Joined: Thu Feb 24, 2011 4:38 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: writing and reading to an external eeprom

Post by Darren.Smith »

cheers,
it makes sense to me now. would having a mcp23008 on the i2c bus as well change anything, as they need to be addressed on the a0-a2 pins, i think that is what confused me that the eeprom does not get addressed in this way.

cheers
darren

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: writing and reading to an external eeprom

Post by kersing »

Hi Darren,

The 24xx02 devices do not use the A0-A2 address lines so sharing the bus with another device can result in problems. Because the control bytes for the two devices you've mentioned differ you should be able to use them both on the same bus. In general the safest option for multiple devices on the same bus would be to use only devices using the address lines, for the 24xx02 an alternate device would be the 34xx02.

Good luck,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Darren.Smith
Flowcode V4 User
Posts: 38
Joined: Thu Feb 24, 2011 4:38 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: writing and reading to an external eeprom

Post by Darren.Smith »

thank you kindly for your help jac, much appreciated

cheers
darren

Darren.Smith
Flowcode V4 User
Posts: 38
Joined: Thu Feb 24, 2011 4:38 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: writing and reading to an external eeprom

Post by Darren.Smith »

hi guys, am back again...got everything working up to address 255 on the eeprom, at the moment i am using a 24c32 which has 4096 address's, how do i address above 255 do i need to use a 16 bit pic as i am only using a 8 bit pic at the moment????

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: writing and reading to an external eeprom

Post by Benj »

Hello Darren,

You should be able to use addresses over 255 on a 8-bit PIC no problem. One thing you may have to do is use a 16-bit variable to work out the address and then split it into high and low address bytes to send to the EEPROM device.

eg.

Int_Address = 256

Byte_Address_High = (Int_Address >> 8 ) & 0xFF
Byte_Address_Low = Int_Address & 0xFF

Darren.Smith
Flowcode V4 User
Posts: 38
Joined: Thu Feb 24, 2011 4:38 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: writing and reading to an external eeprom

Post by Darren.Smith »

cheers ben, will give that a go

Darren.Smith
Flowcode V4 User
Posts: 38
Joined: Thu Feb 24, 2011 4:38 pm
Has thanked: 2 times
Been thanked: 1 time
Contact:

Re: writing and reading to an external eeprom

Post by Darren.Smith »

hi ben, that worked great, except i have now lost the count below 256, i must be doing something very stupid here.
cheers
Dazza

Post Reply