avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-libc-dev] documentation - initialized variables only in program spa


From: Brano Katreniak
Subject: [avr-libc-dev] documentation - initialized variables only in program space
Date: Fri, 10 Sep 2004 15:48:25 +0200
User-agent: Mutt/1.4.1i

I found not clear the way of keeping the initialized variables only in
Flash. It took me much time to understand that one can keep all the
pointer computation to the compiler and than send the address to the
pgm_read_??? function.

I suggest this example into the FAQ:


#include <avr/pgmspace.h>
struct S { 
        char a; 
        char s[10];
        PGM_P s_P;
}
const char foo[] PROGMEM = "Foo";
const char bar[] PROGMEM = "Bar";

const struct S data[] PROGMEM = {
{2, "Hello", foo},
{7, "Foo", bar},
};
int main(void)
{
        char buf[20];
        //read data[1].a
        pgm_read_byte( &(data[1].a) );
        
        //read data[0].s (compiler computes the address of string)
        strncpy_P(buf, data[0].s, sizeof(buf));
        
        //read data[1].s (the address of string must be read from FLASH)
        strncpy_P(buf, (PGM_P) pgm_read_word(&(data[1].s_P)), sizeof(buf)); 
}


Brano

PS. Thank you for great tools.
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]