Pages: [1]   Go Down
Author Topic: saving data to flash  (Read 490 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there a way to save some data to the flash memory area so that it will persist across power cycles? In my application, I need to specify the resolution in cps of quadrature encoders. When that value changes via a command from the serial port when the user connects the device to a new encoder, I would like to be able to retrieve the new value next time the device is powered on.

Don Leuenberger
Logged

London
Offline Offline
Newbie
*
Karma: 0
Posts: 47
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Don,
There is a little bit of EEPROM memory available where you can store stuff which won't be lost when you turn your device off.  (Just 512 bytes, but should be fine for the use you describe.)

See the following page on the playground for the growing documentation on this:
http://www.arduino.cc/playground/Code/EEPROM-Flash

Here, quickly, are the relevant bits of code.
Code:
#include <avr/eeprom.h>

//location in eeprom to read/write to.  0-511.
unsigned int addr = 0;        

//To write...
byte val_to_store = 57;
eeprom_write_byte((unsigned char *) addr, val_to_store);

//To read back...
byte val_retrieved = eeprom_read_byte((unsigned char *) addr);

Values stored in this area will also survive the download of new programs to the arduino so long as you're using the standard arduino bootloader (which doesn't touch the eeprom, currently).

I'd prefer storing this kind of "high-score" information in the eeprom rather than in the flash, as you're never sure how much space you're going to have left, if any, in the flash.  Note that you get around 100,000 writes to the eeprom, which is better than to the flash (1/10th of that), but still a finite number capable of biting you...

Hope this solution fits.
NeillZero
« Last Edit: January 12, 2007, 01:14:18 am by neillzero » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perfect! That's exactly what I need. Apologies for forgetting to look at the Playground for an answer but thank you very much.

Don
Logged

Pages: [1]   Go Up
Jump to: