How to store constants in ROM (Atmel)

我只是一个虾纸丫 提交于 2019-12-25 18:36:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!