|
|
View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
EEPROM |
Posted: Fri Jan 27, 2012 4:04 am |
|
|
How could I place the variable in the internal EEPROM of a PIC, and then read that value? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Fri Jan 27, 2012 4:37 am |
|
|
Look in the manual.
"How do I write variables to EEPROM that are not a byte?"
Shows how to read and write a float variable to the EEPROM. Same technique applies for any other type required.
Best Wishes |
|
|
mhjccsinfo
Joined: 17 Oct 2011 Posts: 23 Location: Iran-tehran
|
|
Posted: Mon Oct 29, 2012 3:16 am |
|
|
Could you please help me on making a simple variable (not float) on eeprom?
I know there is a read_at_address and write_at_address but I want to write in variable.
thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Oct 29, 2012 4:06 am |
|
|
OK. Start with a simple thing. A _variable_ can't be in the EEPROM (comment below). The whole point of the EEPROM is it's contents are basically fixed. It takes a long time (in computing terms) to write to the EEPROM, and the write life is limited (not as badly so as the program memory, but you don't want to write it more than you have to...). What you do, is have the variable in RAM, and then on boot up (say), load this variable _from_ the EEPROM. Then when you shut the system down, or do some specific update, you write the variable back to the EEPROM.
You do this as _rarely_ as you can.
This is what the example in the manual shows you.
You can create a 'virtual' variable that lives in the EEPROM, using the addressmod function, but this is bulky, and if you are not careful can easily result in using up the write life of the EEPROM. Better by far to only write when you are sure the data needs to be updated.
Code: |
//Obviously headers to suit your chip
//Generic routine to read/write a variable to EEPROM
//int8 variables need to change to int16 if your chip has more than 256
//bytes of EEPROM
void eeprom_get(byte *ptr,int8 num,int8 addr) {
int8 count;
for (count=0;count<num;count++) {
ptr[count]=read_eeprom(addr+count);
}
}
void eeprom_put(byte *ptr,int8 num,int8 addr) {
int8 count;
for (count=0;count<num;count++) {
write_eeprom(addr+count,ptr[count]);
}
}
#define VARIABLE_PUT(x,y) eeprom_put(&x,sizeof(x),y)
#define VARIABLE_GET(x,y) eeprom_get(&x,sizeof(x),y)
#define STR_LOC (10) //see later comment
void main(void) {
int16 fred;
int8 dick;
float harry;
struct {
int8 b;
int16 w;
} tom;
char str[8];
//now read these variables from the EEPROM
VARIABLE_GET(fred,0); //from the first address
VARIABLE_GET(dick,2); //third address
VARIABLE_GET(harry,3); //fourth address
VARIABLE_GET(tom,7); //seventh address
VARIABLE_GET(str,STR_LOC); //tenth address
//Note that the routine can deal with any size of variable
//The variables now contain the data from the first 18bytes of the EEPROM.
//Note how much easier the addressing is in the last example, using a
//#defined location.. Safer, better.
//At some point when you change the variables you write them back, _but_
//_as rarely as possible_. On a system recording motor movements for
//example, I only write them when the power goes off (using the INT_LVD),
//or if they stay 'unchanged' for a couple of minutes. _Every_ time you
//write to the EEPROM, you use one of it's 'lives'.
//So to write the string back:
VARIABLE_PUT(str,STR_LOC);
//etc.....
}
|
Think of the EEPROM, as data put away in a filing cabinet, while variables are the stuff on your desk. If you want to work on the file, you get it from the cabinet, do the work, and when you have finished put the file back.
Best Wishes |
|
|
|
|
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
|