如何在RFduino闪存中存储整数数组?

时间:2015-05-28 18:41:27

标签: arrays arduino rfduino

我正在尝试做一些R&amp; D,并且需要查看RFduino上的温度如何在未连接到串行控制台的位置以5分钟的时间间隔发生变化,每秒测量一次。我想使用内置闪光灯。 (我存储字节,以便300个字节的信息应该适合1K页面,对吗?)这是我提出的代码,但它只为闪存保存了一个值。或者我可能只能检索单个值。它目前运行10秒(而计数<10)。如何保存和检索整个值数组?

// select a flash page that isn't in use (see Memory.h for more info)
#define  MY_FLASH_PAGE  251

// double level of indirection required to get gcc
// to apply the stringizing operator correctly
#define  str(x)   xstr(x)
#define  xstr(x)  #x

byte values[500];
int count = 0;

void setup() {
  Serial.begin(38400);
  Serial.println("All output will appear on the serial monitor.");

  // a flash page is 1K in length, so page 251 starts at address 251 * 1024 = 257024 = 3EC00 hex
  byte *p = (byte*)ADDRESS_OF_PAGE(MY_FLASH_PAGE);
  int rc;

  Serial.println("The data stored in flash page " str(MY_FLASH_PAGE) " contains: ");
  Serial.println(*p);

  Serial.print("Attempting to erase flash page " str(MY_FLASH_PAGE) ": ");
  rc = flashPageErase(PAGE_FROM_ADDRESS(p));
  if (rc == 0)
    Serial.println("Success");
  else if (rc == 1)
    Serial.println("Error - the flash page is reserved");
  else if (rc == 2)
    Serial.println("Error - the flash page is used by the sketch");

  Serial.println("The data stored in flash page " str(MY_FLASH_PAGE) " contains: ");
  Serial.println(*p);


  while (count<10){
    RFduino_ULPDelay(SECONDS(1));
    byte temp = RFduino_temperature(CELSIUS);
    Serial.println (count);
    Serial.println (temp);
    values[count]=temp;
    count++;
  }

  Serial.print("Attempting to write data to flash page " str(MY_FLASH_PAGE) ": ");
  rc = flashWriteBlock(p, &values, sizeof(values));
  if (rc == 0)
    Serial.println("Success");
  else if (rc == 1)
    Serial.println("Error - the flash page is reserved");
  else if (rc == 2)
    Serial.println("Error - the flash page is used by the sketch");

  Serial.println("The data stored in flash page " str(MY_FLASH_PAGE) " contains: ");
  Serial.println(*p);
}

void loop() {

  }

这是我的输出:

All output will appear on the serial monitor.
The data stored in flash page 251 contains: 
255
Attempting to erase flash page 251: Success
The data stored in flash page 251 contains: 
255
0
33
1
33
2
33
3
33
4
33
5
33
6
33
7
33
8
33
9
33
Attempting to write data to flash page 251: Success
The data stored in flash page 251 contains: 
33

我希望它能够编写和检索所有10个值!

1 个答案:

答案 0 :(得分:1)

您可以像访问阵列一样访问数据。

要对此进行测试,您可以(暂时)注释掉您的FLASH编写代码并运行此代码段:

byte *p = (byte*)ADDRESS_OF_PAGE(MY_FLASH_PAGE);      

Serial.println("The data stored in flash page " str(MY_FLASH_PAGE) " contains: ");
for (count = 0; count < 10; count++)
{
    Serial.print(count);
    Serial.print(":");    
    Serial.print(p[count]);
    Serial.print("; ");    
}
Serial.println();

请注意:RFduino_temperature(CELSIUS)返回浮点值。