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 support@ccsinfo.com

EEPROM as internal variable storage

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



Joined: 12 Jun 2007
Posts: 68
Location: Poland, podlaskie district

View user's profile Send private message

EEPROM as internal variable storage
PostPosted: Thu Sep 08, 2016 5:15 am     Reply with quote

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: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Sep 08, 2016 5:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 08, 2016 6:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Sep 08, 2016 6:19 am     Reply with quote

I see I can use memcpy function. :D
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Sep 08, 2016 11:47 am     Reply with quote

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: 19368

View user's profile Send private message

PostPosted: Mon Sep 12, 2016 7:51 am     Reply with quote

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....
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