View previous topic :: View next topic |
Author |
Message |
webgiorgio
Joined: 02 Oct 2009 Posts: 123 Location: Denmark
|
how to write int16 into eeprom? |
Posted: Fri May 30, 2014 8:23 am |
|
|
To my understanding of the manual write_eeprom (address, value) only accepts int8 as value. Then how do I save an int16 in eeprom??
I guess that to read it I can do something like
Code: |
int8 hi, lo;
int16 x;
lo=read_eeprom(0);
hi=rad_eeprom(1);
x = make16(hi,lo);
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
Re: how to write int16 into eeprom? |
Posted: Fri May 30, 2014 8:41 am |
|
|
webgiorgio wrote: | To my understanding of the manual write_eeprom (address, value) only accepts int8 as value. |
Just a note. The basis for the 'only accepts int8 as value' is rooted in the fact that EEPROM memory is organized in 8 bit bytes in the PIC architecture. This is true at least for the PIC16 & PIC18 parts.
John |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19554
|
|
Posted: Fri May 30, 2014 9:12 am |
|
|
As a slightly more general version, I use:
Code: |
#define to_eeprom(address, val) write_eeprom_block(address, &val, sizeof(val))
#define from_eeprom(address,val) read_eeprom_block(address, &val,sizeof(val))
void write_eeprom_block(int16 address, int8 *data, int16 count)
{
while (count--)
write_eeprom(address++,*data++);
}
void read_eeprom_block(int16 address, int8 *data, int16 count)
{
while (count--)
*data++ = read_eeprom(address++);
}
//Then to use
int16 fred;
float dick;
to_eeprom(0,fred); //writes fred to address 0 in the eeprom
to_eeprom(16,dick); //writes dick to address 16
from_eeprom(0,fred); //reads fred from address 0
from_eeprom(16,dick); //reads dick from address 16
|
Obviously you can vary these to work with internal or external EEPROM, and the macros write/read any size variable - even structures etc..
You have to manage the addresses to use - I usually #define these.
Best Wishes |
|
|
|