简体   繁体   中英

How do i store a byte array in Arduino flash memory using PROGMEM?

Hello everyone i am new to the world of Arduino and i am having problems storing temporary values in Arduino RAM so i thought i would store them in flash storage using PROGMEM. i am trying to store 2 byte arrays but i cant get them to display after they are stored. After some testing i cant get Serial Print to work properly either but if in the mean time somebody can better explain PROGMEM that would be helpful. my code so far is:

#include <avr/pgmspace.h>
struct GRID{
  boolean isOn;
  uint16_t color;
  uint8_t X;
  uint8_t Y;
};

GRID landed[10][22];
GRID falling[22][10];

//PROGMEM prog_uint8_t xAxis[] = {1,2,3,4,5,6,7,8,9,10};
//PROGMEM prog_uint8_t yAxis[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22};

uint8_t xAxis[] = {1,2,3,4,5,6,7,8,9,10};
uint8_t yAxis[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22};

void iniLanded(){
  for(int i = 0; i < 10; i++)
    for(int j = 0; j < 22; j++){
      landed[i][j].color=0x0000;
      landed[i][j].X = xAxis[i];
      landed[i][j].Y = yAxis[j];
    }
}

void iniFalling(){
  for(int i = 0; i < 22; i++)
    for(int j = 0; j < 10; j++){
      falling[i][j].color=0x0000;
      falling[i][j].X = xAxis[j];
      falling[i][j].Y = yAxis[i];
    }
}

void printLanded(){
  for(int i = 0; i < 10; i++)
    for(int j = 0; j < 22; j++){
      Serial.print(landed[i][j].X); Serial.print(" "); Serial.println(landed[i][j].Y);
    }
}

void printFalling(){
  for(int i = 0; i < 22; i++)
    for(int j = 0; j < 10; j++){
      Serial.print(landed[i][j].X); Serial.print(" "); Serial.println(landed[i][j].Y);
    }
}

void setup(){
  Serial.begin(9600);
  iniLanded();
  iniFalling();

}

void loop(){
  Serial.println(F("Hello World!"));
  delay(100);
  /*printLanded();
  delay(100);
  printFalling();
  delay(100);*/
}

The trick is in the way you call from flash. See my example this is where I define much like your commented code. and here is where you call back the data. Note the use of pgm_read_word_near and the deference &. Where there are other similar functions found here for byte, word, etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM