|
|
View previous topic :: View next topic |
Author |
Message |
Oli Guest
|
storing large numbers of variables in EEPROM |
Posted: Tue Aug 05, 2008 9:31 am |
|
|
Hi there
Working on quite a large project at the moment and it has approximately ~160 global system configuration settings that need to be stored in non volatile memory.
- I have in the past used a fixed addressed system of storing each variable to EEPROM, but this became cu mbersome with larger numbers of settings.
- I then wrote a mini indexing / storing system to save each one and read each one with a single function (including arrays).... however this also has its drawbacks.
What I want to do now is declare all of the variables that need storing in one place, and dictate the memory address' that they use. Then when I need to store these, I can simply copy the whole address range with a single loop.
Can anybody give me any pointers on declaring the variables (without specifying *every* variable RAM address individually using #byte) and how to access them to copy each byte..
something like the below pseudocode....
Code: |
// dictate the address for variables after this point to be stored
unsigned int16 test1 = 0;
unsigned int16 test2 = 0;
unsigned int16 test3 = 0;
unsigned int16 test4 = 0;
byte test_array[100];
// declare end of specified address
|
and then to store to eeprom, obviously
Code: |
for (i=0; i<108; i++)
write_ext_eeprom(i, (i+(memory_address)));
|
Any other effort saving approached would be really appreciated!!!
Many thanks |
|
|
Ttelmah Guest
|
|
Posted: Tue Aug 05, 2008 9:47 am |
|
|
Use a structure. So something like:
Code: |
struct {
int16 conf1;
int16 conf2;
int16 conf3;
int8 vals[10] = {1,2,3,4,5,6,7,8,9,10};
float zero_point;
int32 bigger_value;
} conf;
//Then access these as needed - conf.conf1 etc..
#byte INTCON=0xFF2 //Change for a 16 chip
#bit INT_GIE=INTCON.7
//EPROM save/retrieve routines. Keep interrupts 'on', if enabled, for
//as much as possible
void EEPROM_GET(int *ptr,int num,int addr) {
int count;
int1 int_enabled;
int_enabled=GIE;
for (count=0;count<num;count++)
{
disable_interrupts(global);
ptr[count]=READ_EEPROM(addr+count);
if (int_enabled) enable_interrupts(global);
}
}
void EEPROM_PUT(int *ptr,int num,int addr) {
int count;
int1 int_enabled;
int_enabled=GIE;
for (count=0;count<num;count++)
{
disable_interrupts(global);
WRITE_EEPROM(addr+count,ptr[count]);
if (int_enabled) enable_interrupts(global);
}
}
//So to retrieve the structure from the start of the EEPROM
EEPROM_GET(conf,sizeof(conf),0);
//While to save it
EEPROM_PUT(&conf,sizeof(conf),0);
|
By using a structure, you guarantee that the data _will_ be stored consecutively.
Obviously, if you have a couple of different main areas, you could use a couple of structures, and could use named defines for their get/put, like:
#define CONFIG &conf,sizeof(conf),0
#define IO_CONF &conf2,sizeof(conf2),sizeof(conf)
and then just use:
EEPROM_GET(CONFIG);
EEPROM_GET(IO_CONF);
to fetch these two structures. Note that the second define has the EEPROM address offset by the size of the first structure.
Best Wishes |
|
|
Oli Guest
|
|
Posted: Wed Aug 06, 2008 3:34 am |
|
|
Thanks Ttelmah. I'll give it a crack now and let you know how it goes. I already use structures for some of the variables so some of the work is done for me. |
|
|
Guest
|
|
Posted: Thu Aug 07, 2008 8:58 am |
|
|
Hi
Why must GIE be disable when write to EEPROM?
But the code work:-) |
|
|
Ttelmah Guest
|
|
Posted: Thu Aug 07, 2008 9:50 am |
|
|
Check the chip data sheet.
This was originally written for the 18F452, and the data sheet has the comment 'It is strongly recommended that interrupts be disabled during this code segment'. It is a recommendation, rather than a 'requirement'.
There are two problems. The first is that the code sends the byte sequence to 'unlock' writes to the memory, and the same sequence will also work for the main program memory. If the EECON1, EEPGD bit, got changed, this sequence could write anywhere, hence it is worth ensuring that the risk of a memory location being changed is 'minimised', by not allowing interrupts in this code area.
Also the unlock itself is only guaranteed to work, if there is no code between writing the 0XAA, and setting the WR bit. Again an interrupt here might cause problems.
The same recommendation in present in most chips.
Best Wishes |
|
|
Oli Guest
|
|
Posted: Fri Aug 08, 2008 7:36 am |
|
|
Worked a treat. Made quite a few modifications to the code (larger ints etc.) but the concept works flawlessly. Best of all I can add new variables without having to touch the code!!!
Thank you for your help - greatly appreciated. |
|
|
|
|
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
|