CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to use EEPROM with PIC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








How to use EEPROM with PIC
PostPosted: Wed Jun 15, 2005 10:12 am     Reply with quote

Sorry for the newbie question, but I need help to understand the EEPROM and PIC Communication.

I am new to PIC and EEPROM, after I read datasheet for 24LC32 and I have the following questions, I couldn't find answer from the datasheet.

Questions:

Quote:
1. How to use the A2, A1, A0
Let say if I use 3 of 24C32 device connect to the same I2C bus,
How do I config the A2-A0?


2. How to translate(mapping) from 4096 bytes to address 0x0000-0x0fff
and how to use the address ( example, how many word can I store in the
4096 bytes memory, and if I want to store the adc result (255 or 0xFF) to
the 4096 bytes memory, how do I store it and what is the starting
address? How much memory does the 255 use from the memory? How do
I know the next address to use if I want to store more data in to the
EEPROM.
jds-pic



Joined: 17 Sep 2003
Posts: 205

View user's profile Send private message

Re: How to use EEPROM with PIC
PostPosted: Wed Jun 15, 2005 12:34 pm     Reply with quote

Anonymous wrote:
Sorry for the newbie question, but I need help to understand the EEPROM and PIC Communication.

I am new to PIC and EEPROM, after I read datasheet for 24LC32 and I have the following questions, I couldn't find answer from the datasheet.

Questions:

Quote:
1. How to use the A2, A1, A0
Let say if I use 3 of 24C32 device connect to the same I2C bus,
How do I config the A2-A0?


2. How to translate(mapping) from 4096 bytes to address 0x0000-0x0fff
and how to use the address ( example, how many word can I store in the
4096 bytes memory, and if I want to store the adc result (255 or 0xFF) to
the 4096 bytes memory, how do I store it and what is the starting
address? How much memory does the 255 use from the memory? How do
I know the next address to use if I want to store more data in to the
EEPROM.


please register so we don't have so many "guest" posts.

i2c is a bus, with various devices hanging off of it. all of the devices hanging off of the bus hear all communications. the devices need some kind of method of uniquely identifying themselves so that the attached i2c master knows who it is talking to, and so a particular device knows when it is being spoken to.

typical i2c devices have three BCD (binary coded decimal) address pins; these pins set unique adresses for each device on the bus and hence allow the PIC or other uC to communicate with each one. typically, the three pins are hardwired directly to either the Vcc rail or ground. the Vcc is usually 3.3V or 5V.

the weights of the A0, A1, and A2 pins (bits) are 1, 2, and 4, respectively. that is, if you tie all three to ground, the device is set at address 0. if you tie A0 and A1 high, and A2 to ground, the device is set at address 3 (=1+2). if you tie all pins high, the device is set at address 7 (=1+2+4).

if you have three 24Cxx devices,... one approach is to have them at bus addresses 0, 1, and 2. so the first has all address pins grounded. the second has A0 tied high. the third has A1 tied high.

now you can talk to all three devices without a collision. an 12c transaction begins with i2c start(). then, you send a one byte command, i2c_write(0xNN) which includes a device type (4 bits), an address (3 bits), and a R/W flag (1 bit). and next, you send a command of some type, again using i2c_write(0xNN). only the i2c device on the bus that matches the device type and address will respond to your command request.

i2c memory devices, like the 24Cxx series, and either "flat" or "paged" memory devices. in a flat device, the memory is in one big chunk, linearly addressed from 0->N bytes. so the first memory location is 0x00 and the last is the size of the device-1 (e.g. 0xFF for a 256 byte device). note that for memory devices larger than 256 bytes, addressing requires two (or more) bytes. that is, to reach the top location in a 1024byte memory you need more than 8 bits of address. so these larger devices use "16 bit" addressing -- you need to send two bytes to specify the location.

other memory devices are "paged", in that there are banks of memory, each of a fixed size. for example, you may have a device with 8 banks of 256 bytes, giving 2048 bytes total. to read/write these devices, you first select a bank ([0-7]) by writing a byte. from then on you can directly access any of the 256 bytes in that bank to with a single address byte. to access other memory locations outside the bank, first you have to select the correct page.

you can see some sample i2c eeprom code (incl 16 bit address support) here in a library i wrote;
http://www.ccsinfo.com/forum/viewtopic.php?t=19526
scroll down to the 24cXX section.

to your second question, any value that can be expressed in an 8 bit byte can be stored in an EEPROM's bytewide memory location.

for example,
0xFF = 255 = 0b11111111 ---> all of these are representations of the same 8bit value (decimal 255), and hence take up one location in an EEPROM.

if you have values > decimal 255 (i.e. 16 bit A/D results) that have to be stored, you must break the 16 bit value into two 8 bit values and store them independently. upon needing them again, retrieve both and use the "<<" c operator (and/or CCS's make16() function) to shift the MSB back into it's correct position in the 16 bit variable. see the code at the end of this posting for an example of how to do something like this.

jds-pic


Code:
int16 internal_eeprom(int mode, int address, int16 value) {
   int16 temp;
   if (mode==CLEARALL) {
      for(temp=0;temp<=255;temp++)
         write_eeprom(temp,0);             
      return(0);
      }
   if (mode==WRITE16) {
      write_eeprom(address,make8(value,1));  // var,offset
      write_eeprom(address+1,make8(value,0));
      }
   if (mode==INCREMENT16) {
      temp=make16(read_eeprom(address),read_eeprom(address+1)); // hi,lo
      temp++;
      write_eeprom(address,make8(temp,1));  // var,offset
      write_eeprom(address+1,make8(temp,0));
      }
   // if (mode==READ16) // implicit
   //   ;
   return(make16(read_eeprom(address),read_eeprom(address+1))); // hi,lo
   }
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group