# How to pass string for EEPROM write for ATMega32? (in the following code)

How could I pass a string for EEPROM write first and read in the code as:

int main()
{
char display[30];
EEPROM_writedata(display);
LCD_senddata(0,EEPROM_print(1));//this wont work
LCD_senddata(0,display); //this works fine
return 1;
}

void EEPROM_writedata(char* name)
{
eeprom_update_block((const void *)name,(void*)1,20);
}

char* EEPROM_print(int i)
{
char* send;
eeprom_read_block((void*)send,(const void*)1,10);
return send;
}

LCD_sendata(int, char*)
{
//codes for LCD initialize upto print
}


I may have a lot of mistakes here because of less knowledge. Please specify if you get them. My problem is mainly pass string to EEPROM_write() and pass it to main from EEPROM_print(). Any help is appreciated.

-
What do you actually get out of the eeprom printing? – Majenko Oct 18 '11 at 8:38
I need to write a string to EEPROM and read it back to print it on LCD. Actually I need to manage a small database inside EEPROM. – Bishal Paudel Oct 18 '11 at 8:40
Microcontrollers are very unforgiving for new C programmers. I suggest that you mock up the AVR libraries on a PC and test your code there. It may also be useful to brush up on your C, c.learncodethehardway.org – Toby Jaffey Oct 18 '11 at 8:45
@Bishal What exactly does the EEPROM_print() return for you to display on the LCD? Not what do you want it to return, but what does it return? What is "not working" about it? Is it just blank? Is it gibberish? What? – Majenko Oct 18 '11 at 9:04
@bishal yes, I know what it should return. But what are you experiencing it returning? – Majenko Oct 18 '11 at 11:01
show 3 more comments

## 2 Answers

Variables stored in the EEPROM on AVRs in C must be placed in the .eeprom section.

#define     EEMEM   __attribute__((section(".eeprom")))
void        eeprom_write_block (const void *__src, void *__dst, size_t __n)


To store a variable in EEPROM and access it, you want something like:

char EEMEM myVar;

void writeMyVal(char c)
{
eeprom_write_block(&c, &myVar, sizeof(myVar));
}

void readMyVal(char *c)
{
eeprom_read_block((void*)c, &myVar, sizeof(myVar));
}


Here's a tutorial.

-

Thankx to all of you for your great help Actually I got my solution from:
http://mainframe.cx/~ckuethe/avr-c-tutorial/lesson11.c I edited the code as

 int main()
{
char display[30],write[]="write something inside EEPROM";
EEPROM_writedata(&write); //initially, EEPROM_writedata(write);
LCD_senddata(0,EEPROM_print(1));
LCD_senddata(0,&display); //initially, LCD_senddata(0,display);
return 1;
}

-
You still have a bug that will come back and bite you in the future even if it seems to work now. In your EEPROM_print() routine you need to initialize the send pointer to the address of a buffer which has been allocated in a way that while survive the completing of the function. – Chris Stratton Nov 19 '11 at 18:53
Absolutely agree with @ChrisStratton, you need to allocate space for char *send that is not local to the method. Read the question and answer here: stackoverflow.com/questions/6441218/… – Jon L Mar 18 '12 at 5:38