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 CCS Technical Support

External EEPROM Basics For Newbies

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



Joined: 02 Jun 2004
Posts: 16

View user's profile Send private message Send e-mail MSN Messenger

External EEPROM Basics For Newbies
PostPosted: Thu Jul 28, 2005 7:50 am     Reply with quote

I have been teaching myself C and PIC programming for the last twelve months and thanks to this forum i have got to grips with many of the basics including LCDs Keypads A/Ds etc.

My next task is to learn how to read and write to an external I2C EEPROM using a 16F877 and a 24L256 32K X 8 (SDA RC4 and SCL RC3).

I have read many of the posts and the examples from CCS. What i apear to be missing is a basic understanding of memory addresses.

QUESTIONS AND INFO REQUIRED

1) I need and explination of the page system.

2) I need to understand how to organise and manage my memory.

3) Example problem:
If i have a routine that reads an A/D port and returns a value called "volts" where volts is = 1023 how would this be written in to an external memory?

would i write and read "volts" or would i have to write and read 1023 to/from a defined address?

If i have "volts" and "amps" and they both need to be written to memory how do i define where one finishes and the other starts?

I dont have a problem with code examples as the are many in the posts. I have a lack of knowlage on memory. I need to understand what the code examples do if i am to make any progress

If any one can explain how it works in dummies talk or know of a dummies type link i would appreciate it
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Thu Jul 28, 2005 9:23 am     Reply with quote

The page system is not that hard to grasp.

Imagine a book with 500 pages. Imagine that you are dictating to a person who is actually typing the book for you. This rather gifted person can listen and retain one page worth of text before they start typing. Let me also add that no words get lost either. However, if you try to dictate more than one page worth of text, the extra "spills over" or "loops back" onto the beginning of the page.

That's sort of like the page system with external EEPROMs. For instance, I'm presently using a 24LC32, a 32k serial eeprom. This particular one can accept 32 bytes for storage, at most, before actually writing the data all at once. In this case, 32 bytes is the page size. Note that you can't overflow one page when doing a page write. In the case of the 24LC32, I can do a page write to address 0x0000, then again at address 0x0020 (32), and so on. I can start a page write at address 0x0014, but the data being written will start at address 0x14, then "roll over" at address 0x1f back to address 0x00.

I should also mention that the 24LC32 is a 32 kbit EEPROM, which means you can store 32/8 = 4 kbytes. You can store one byte/address, in addresses from 0x0000 - 0x0fff.

The datasheet for your EEPROM will tell you how big a page is.

As for storing data, that's up to you how you'd like to handle it. Since it sounds like you're storing only two things, V & I, I'll use that as a basis for the explanation.

First you should note that serial EEPROMs are usually 8 bit. That is, they are capable of storing 8 bits in every address. If you have a variable, such as voltage, which goes up to 1023, you'll need a 16 bit variable to store it. That means that you'll need to break up this variable into a low and a high byte, and store those individually in the EEPROM. When you read voltages from the EEPROM, you'll need to reassemble the 16 bit variable yourself based on the low and high bytes you read from memory.

Note that you should always put the low & high bytes of 16 bit variables right next to each other in memory (i.e. address = high byte, address + 1 = low byte, or vice-versa).

For instance,

Code:
int16 voltage, address;
int8 high_byte, low_byte;
// for write, break apart the 16 bit variable
low_byte = voltage;
high_byte = voltage >> 8;
// do writes here

// for read
high_byte = read_from_memory(address);
low_byte = read_from_memory(address + 1);

voltage = (0x100 * high_byte) + low_byte; // reassemble the 16 bit variable


Since you are storing V & I readings, what I'd do is group them together. I'll assume that the I is also a 16 bit variable. This requires the storage of 4 bytes total, 2 for voltage, 2 for current. If you have a 16 bit variable called address which contains the address where you store these things in the EEPROM, then you could do something like this:

Code:
int16 address = 0, resistance = 10;
int8 i;
for (i = 0; i < 100; i++) {
   voltage = read_adc(); // read voltage
   current = voltage / resistance; // calculate current
   save_value(voltage, address); // store voltage in EEPROM
   save_value(current, address + 2); // store current in EEPROM, right next to the voltage
   address = address + 4; // add 4 to address so that the next time through the loop the next values will be stored next to the previous ones
}


I hope this helps to clear some of it up for you.
zogbog



Joined: 02 Jun 2004
Posts: 16

View user's profile Send private message Send e-mail MSN Messenger

External EEPROM Basics For Newbies
PostPosted: Thu Jul 28, 2005 1:32 pm     Reply with quote

Good clear explanation just what i have been looking for. Now all the example code that i have been reading is crystal clear. If only more subjects could be explained in this way. Maybe a series of books?......

Thanks for the help.
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