问题
Good Day,
I am writing firmware for an ATMega645A using AtmelStudio 7. I am starting a localization project and need to display messages on a 16x2 character display in 3 different languages. The existing (English messages) are stored in SRAM, but I will end up overflowing this data memory space if I have to triple the number of messages.
SO, I am trying to store the messages in ROM where space is a-plenty. Problem is, Atmel app notes are either (a) wrong or (b) incomplete because I have tried multiple methods per their suggestion and the code does not compile (ref: Atmel Tips & Tricks App Note doc8453.pdf page 8, and http://www.atmel.com/webdoc/AVRLibcReferenceManual/FAQ_1faq_rom_array.html). My code looks like this:
// before main(void)
#include <avr/pgmspace.h>
const char string_1[] PROGMEM = "String 1";
const char string_2[] PROGMEM = "String 2";
const char string_3[] PROGMEM = "String 3";
const char string_4[] PROGMEM = "String 4";
const char string_5[] PROGMEM = "String 5";
PGM_P string_table[5] PROGMEM =
{
string_1,
string_2,
string_3,
string_4,
string_5
};
inside main(void) I have the following:
char buffer[10];
PGM_P p;
int i;
memcpy_P(&p, &string_table[i], sizeof(PGM_P));
strcpy_P(buffer, p);
When compiling, I get a message: variable 'string_table' must be const in order to be put into read-only section by means of 'attribute((progmem))'
I have tried 'char' instead of 'const char', but nothing seems to compile. Any advice on the proper syntax to get these static character strings to be stored in ROM would be greatly appreciated.
回答1:
after fussing with this all day and many trial and error attempts, I came up with the following:
Change the line
`PGM_P string_table[5] PROGMEM =`
to
`PGM_P const string_table[5] PROGMEM =`
That seems to compile and I can display the contents of the string "buffer" on the display ("String 1" is actually what is sent to the display). I hope this will help others that have had similar issues getting constants in and out of ROM.
来源:https://stackoverflow.com/questions/39990163/how-to-store-constants-in-rom-atmel