我正在尝试做一些研发,需要看看RFduino上的温度在5分钟的时间间隔内如何在未连接到串行控制台的位置变化,每秒测量一次。我想使用内置的闪存为此。(我正在存储字节,所以这是300字节的信息,应该适合1K的页面,对吗?)这是我已经提出的代码,但它只保存一个值的flash。或者我只能检索到一个值。它当前运行10秒(while count<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个值!
您可以像访问数组一样访问数据。
为了测试这一点,你可以(暂时)注释掉你的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)
返回float值