Writing data to eeprom memory

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

David Halliday
Posts: 9
Joined: Tue Jan 10, 2006 4:36 pm
Location: Hampshire
Contact:

Writing data to eeprom memory

Postby David Halliday » Mon Jan 08, 2007 11:12 am

Hi all

I've been trying to learn how to save several variables of information to eeprom memory which can be recalled later which will be stored like a selection of recipes.
I know the 16F877A has 256 Bytes of eeprom memory but how many byte will each number variable take-up.
Also the manual says it takes up 5 memory addresses between each save of data (00H to FFH ) is this correct or have I read this incorrectly, as this will depend upon the size of the data that you are trying to save ???

Does anyone have any code examples on the best way to save several recipes containing 8 numbers variable to memory.

Here is the code I have been using so far.



Code: Select all

unsigned char read_eeprom(unsigned char addr)
{
eeadr = addr; // low byte
eeadrh = 0; // high byte
clear_bit(eecon1,EEPGD); // select eeprom memory
set_bit(eecon1,RD); // set read bit
return(eedata);
}



void write_eeprom(unsigned char addr, unsigned char data)
{
eeadrh = 0; // high byte
eeadr = addr; // low byte
eedath = 0; // high byte
eedata = data; // low byte
clear_bit(eecon1,EEPGD); // select eeprom data memory
set_bit(eecon1,WREN); // enable write
eecon2 = 0x55;
eecon2 = 0xaa;
set_bit(eecon1,WR); // write command
while(eecon1&2); // wait untill writing is ready
clear_bit(eecon1,WREN); // disable write
}


Would it be best to put the 8 variables into an array an increment through them as the address is incremented ??


Many thanks in advance Dave :?:

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

Postby Benj » Mon Jan 08, 2007 6:06 pm

Hello David

The EEPROM memory is taken up depending on which variable you are placing into the EEPROM. However you can only write to the EEPROM a byte at a time so.

A Byte / Char / 8 bit variable will take up 1 byte of EEPROM.

A Int / Double / 16 bit variable will take up 2 bytes of EEPROM and involves 2 write instruictions for the Low byte and the High byte.

As for placing your data in an array. That is a great idea. Since you already have your EEPROM write routine set out as a function it would be easy to set up a for loop to step through each of the values you wish to write to the EEPROM.