- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I was wondering if anyone had any examples of how to write a floating point number to Em_EEPROM. I remember reading something somewhere along these lines on these forums (though I cannot find it now) about accomplishing this, but do not recall how it was done.
Thank you in advance for any help!
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Declare a static const var or structure and initialize it, without initialization it will reside in ram. Now you may
Result = EmEEPROM_Write((void *)(&Variable or structure), sizeof(Variable or structure));
Do not forget to check the result for successful operation.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Thank you for the information. Is the below code a reasonable test?
// Sets up data structure
struct Data
{
float pi;
char title[10];
int number;
};
struct Data Data1;
struct Data Data2;
strcpy(Data1.title, "Testing");
Data1.pi = 3.14159;
Data1.number = 512;
Em_EEPROM_Write((void *)(&Data1),eepromArray,sizeof(Data1));
// Read flash array back
memcpy((void *)(&Data2),eepromArray,ARRAY_SIZE);
float pi = Data2.pi;
Thank you,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I think Bob meant checking the error code returned by the EEPROM_Write() method.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
No, that is not correct!
You must initialize your structure with ists declaration as
static const struct Data
{
float pi = 3.14; // You may use 4 * atan(1.0)
char title[10] = "Nothing";
int number = 666;
};
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I assume your ARRAY_SIZE is 16 ?
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Sorry for only posting part of my code. I did initialize the structure, and it works great!
Thanks all,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Bob,
I tried to initialize the struct as you put it:
static const struct Data
{
float pi = 3.14; // You may use 4 * atan(1.0)
char title[10] = "Nothing";
int number = 666;
};
The compiler gives me an error if I try to initialize it in the struct declaration. Do you know what the issue is?
Thank you,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Whats the actual error you get?
What you did in your code is to name the type of the struct ('Data'), but not the variable itself (that needs to come after the definition). The proper way for initializing a const struct is
static const struct StructName{ float pi; char title[10]; int number;} varName={3.14, "Nothing",666};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Attached is a stripped down version of what I am trying to do. What I want to do is store the 'eepromData' struct in EEPROM, and recall it at start up, and write to EEPROM when a value changes.
If I make it a const, I cannot write to it. Do I use a different struct to write to the EEPROM?
What the program is doing is checking to see if the EEPROM is empty, and if so (like on initial startup) it writes initialization values into the struct and writes it to EEPROM. Then it posts to the LCD if it was an inital run or not (i.e. if there were any values found in EEPROM). It is not stroring into EEPROM, because after power down, it still prints 'Initial'.
Please let me know where I went wrong.
Thank you,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I already spotted one error, my brackets are off in the data check...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The code seems to work fine with the following substituted:
// Read flash array back and check to see if initial data write is needed
memcpy((void *)(&dataLocal),(void *)&eepromArray,sizeof(dataLocal));
if(dataLocal.averages == 0){dataLocal.averages = 10;dataFlag = TRUE;}
if(dataLocal.baseline == 0){dataLocal.baseline = 150;dataFlag = TRUE;}
if(dataLocal.gain == 0){dataLocal.gain = 1;dataFlag = TRUE;}
if(dataLocal.genre == 0){dataLocal.genre = 1;dataFlag = TRUE;}
if(dataFlag){WriteEEPROM();}
else{dataLocal.baseline++;WriteEEPROM();}
The baseline counts up after each power cycle.
Is this still incorrect? What is the best practice for a situation like this.
Thank you,
Tom
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Did you look at the example project for the EM_EEPROM component? (Right-click on the component, "Find Example project"). It shows you how its supposed to work.
What you need is a 'const uint8' array thats placed in Flash, and a corresponding buffer in sRAM. YOu can read from Flash memory directly, but for writing you place the data into the buffer, and then write the buffer to the array (using Em_EEPROM_Write).
The compiler is right, a variable defined as const is non-modifiable and cannot be written to. Actually I think your sample project is quite OK.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can take any format struc and store/retrieve from FLASH, EE.
Basically you know the size of the struc, you know the address of the first
byte element in RAM and FLASH, so you create a struc in RAM, then
when you read the FLASH version just use an incrementing pointer in a
for() loop to fill the RAM struc byte for byte.
So FLASH does not have a struc per say, just a byte for byte copy of the struc that you
defined in RAM.
The same is true of the writing, you give the API the source pointer, apointer to
the first byte of the struc, the dest pointer, and the size of struc, the rest is taken
care of. So you write the struc in RAM out to FLASH byte by byte.
Regards, Dana.