Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
2  
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
2  
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
2  
@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
2  
@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

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;
 }
share|improve this answer
2  
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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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