CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Saving to EEPROM.....

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
JAM2014



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

Saving to EEPROM.....
PostPosted: Tue Dec 08, 2020 4:58 pm     Reply with quote

Hi All,

I have a project that among other things derives latitude and longitude from a connected GPS. I'd like to store these values in EEPROM to speed up the booting process and/or eliminate the GPS requirement once an initial fix has been determined. I parse the latitude & longitude from the NMEA string, and then convert to floats. I'm wondering if there is a 'clean' way to store these values to EEPROM during this process?

Here is an example of how I get the latitude:

Code:

//Get latitude & direction
         p1 = StrFnd(GPRMCStr, ',', p2+1);   //find next comma
         p2 = StrFnd(GPRMCStr, ',', p1+1);   //find next comma
         RMCInfo->Latitude = atof(StrnmCpy(TempStr, GPRMCStr, p1+1, p2-1));
         RMCInfo->N_S = GPRMCStr[p2+1];


I could store the individual values character by character to EEPROM, but it seems like there must be a better way?

I'm writing this code for a 18F46K22, and using the PCH 5.050 compiler.

Jack
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 1:09 am     Reply with quote

If you look in the 'drivers' section of the compiler, there is a standard bit of
code, 'floatee.c', which shows how to write a float value to EEPROM. Basically
you store the four bytes that 'make up' the float. In the library for the
internal EEPROM (internal_eeprom.c), there are similar functions for the
internal EEPROM, and ones for the various sized integers as well.

However big caveat. What you should do, is read the value that is currently
stored, and only write a new value if the value has changed. Thing to
understand is that EEPROM has a limited 'write' life. Every time you perform
a write it uses a 'life' for the cell. This happens even if you write exactly the
same value. Hence adding a test, and not actually writing if the value
hasn't changed is worthwhile. The EEPROM has to be thought of rather like
a stone carving. Quite a lot of work to do and a lot of work to change. Not
something to be written frequently like the RAM.

So make your code perhaps look at the reading, and wait till it has been
stable for several minutes, and only then write it to the EEPROM. When it
actually writes check the already stored value and only perform the actual
write if this is different. This way you won't write when the unit is moving,
or if the value has yet to fully settle. Result very few actual 'writes' to the
EEPROM. Very Happy
JAM2014



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 10:31 am     Reply with quote

Hello Ttelmah,

Awesome! That is exactly what I need!

Yes, understand about writing too often to the EEPROM! BTW, the folks at Dangerous Prototypes did a project a few years ago to determine the ultimate life of the internal EEPROM on a PIC device. My recollection is that the real world experimental result was quite a bit more (2x or 3x) the manufacturers stated life!

Thanks,

Jack
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group