3
\$\begingroup\$

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ What do you actually get out of the eeprom printing? \$\endgroup\$ – Majenko Oct 18 '11 at 8:38
  • \$\begingroup\$ 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. \$\endgroup\$ – Bishal Paudel Oct 18 '11 at 8:40
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – Toby Jaffey Oct 18 '11 at 8:45
  • 2
    \$\begingroup\$ @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? \$\endgroup\$ – Majenko Oct 18 '11 at 9:04
  • 2
    \$\begingroup\$ @bishal yes, I know what it should return. But what are you experiencing it returning? \$\endgroup\$ – Majenko Oct 18 '11 at 11:01
1
\$\begingroup\$

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

(From http://www.nongnu.org/avr-libc/user-manual/group_avr_eeprom.html)

#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.

\$\endgroup\$
1
\$\begingroup\$

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;
 }
\$\endgroup\$
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – Chris Stratton Nov 19 '11 at 18:53
  • \$\begingroup\$ 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/… \$\endgroup\$ – Jon L Mar 18 '12 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.