View previous topic :: View next topic |
Author |
Message |
silelis
Joined: 12 Jun 2007 Posts: 68 Location: Poland, podlaskie district
|
EEPROM as internal variable storage |
Posted: Thu Sep 08, 2016 5:15 am |
|
|
Hello,
I want to use PIC eeprom as internal data storage during power off.
I will write different types of vars (int8, char, int32), and I wonder how to declare the function to take multiple types of variables.
I consider some kind of function:
void write_var_2_eeprom (var "different" - which type to declare, int8 eeprom_start_address, int 8 size_of_var)
Second question is how to read data from EEPROM to var:
How to declare read_eeprom_2_var (var "different" - which type to declare, int8 eeprom_start_addres, int 8 size_of_var) |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Thu Sep 08, 2016 5:27 am |
|
|
It's all in the manual and the datasheets !
First you need to read the PIC datasheet to see if and how your PIC handles EEPROM.
Second, then read the CCS manual( press F11 when your project is open) and read the section on EEPROM.
3rd. Now locate the examples that CCS supplies in the 'EXAMPLES' folder.
3 easy steps to happiness.
warning: Be sure NOT to write to one cell of the EEPROM in a forever loop. They can only be written to so many times ! This knowledge IS in the datasheets. One 'trick' during coding is to toggle an LED in the code where the EEPROM write is commented out. If the program is in a forever loop the LED will be glowing, bad news !!
Jay |
|
|
silelis
Joined: 12 Jun 2007 Posts: 68 Location: Poland, podlaskie district
|
|
Posted: Thu Sep 08, 2016 6:14 am |
|
|
I know how to write read eeprom but I'm consider how to build universal C function which will take different types of vars like int8, int16, chars, arrays. |
|
|
silelis
Joined: 12 Jun 2007 Posts: 68 Location: Poland, podlaskie district
|
|
Posted: Thu Sep 08, 2016 6:19 am |
|
|
I see I can use memcpy function. :D |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Thu Sep 08, 2016 11:47 am |
|
|
Do you also understand the logic you need to deploy ?
1- reliability of data retrieved from EEPROM .
I always use a checksum with enough bits to assure
that data from EEPROM is not likely corrupted . This implies
rolling accumulation as each byte is written and or retrieved
to assure correctness.
2-minimizing EEPROM writes. Before changing a value
in eeprom, - always check to see
that what you propose to write - is not the value already stored.
3- avoid writing values to EEPROM in regard to watchdog triggers
or under any conditions where your PIC can be detected as operating at too
low a voltage. The write may not complete in full.
4-and overall, READ all you want but make EEPROM writes as infrequent
as you can. It is a perishable resource. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19509
|
|
Posted: Mon Sep 12, 2016 7:51 am |
|
|
What you do, is have a function that writes 'n' bytes to the EEPROM from a memory location, at an address. Then you write using this, pointer to your variable, and 'sizeof' for n.
You can use addressmod, which will allow you to have the variables treated by name, and handle the sizing automatically, but it is complex code.
Easiest way, is to have all the variables you want to store, held in a single structure. Then you just save that structure as one entity, so no fiddling with variable sizes involved. You can also save significant time, by having your 'write' code, read each byte, and then not write, just advance to the next byte if the byte doesn't need to change. Saves time, and EEPROM lives.... |
|
|
|