View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
read/write int16 value in eeprom |
Posted: Fri Dec 11, 2009 11:34 pm |
|
|
Hi,
I want to read/write int16 value in/from eeprom.
int16 takes two bytes.
Can I read/write in the same way as we treat floating numbers or is there another way ?
For example:
Code: |
void write_on_time(long int on=0,int16 high_time);//write function
int16 read_on_time(long int on=0);//read function
int16 high_time=456;//value to be write,this values also be entered from keypad
int16 ee_high_time;
int on=0;//location in eeprom
void main()
{
....
write_on_time(on,high_time);//write
....
....
ee_high_time=read_on_time(on);//read
....
}
void write_on_time(long int on=0,int16 high_time)
{
int j;
for(j=0;j<2;j++)
write_eeprom(j+on,*(&high_time+j));
}
int16 read_on_time (long int on=0)
{
int i;
int16 high_time;
for(i=0;i<2;i++)
*(&high_time+i)=read_eeprom(i+on);
return(high_time);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Dec 12, 2009 12:14 am |
|
|
Look in the following CCS driver file:
Quote: | c:\program files\picc\drivers\internal_eeprom.c |
It has these two functions to read/write an 'int16' to internal eeprom:
Code: |
write_int16_eeprom()
read_int16_eeprom()
|
Look in this file. It shows how to call them.
Quote: | c:\program files\picc\drivers\compass.c |
|
|
|
mbradley
Joined: 11 Jul 2009 Posts: 118 Location: California, USA
|
|
Posted: Sat Dec 12, 2009 12:15 am |
|
|
you can try these:
by using a macro instead of a function, we can eliminate the int8 or int16 defines for address
Code: |
// a few internal eeprom macro wrappers
#define mReadByte(addr) read_eeprom(addr)
#define mWriteByte(addr,data) write_eeprom(addr,data)
// little endian
#define mReadWord(addr) make16( read_eeprom(addr + 1), read_eeprom(addr) )
#define mWriteWord(addr, data) { write_eeprom(addr, make8(data,0) ); write_eeprom(addr + 1, make8(data,1) };
|
_________________ Michael Bradley
www.mculabs.com
Open Drivers and Projects |
|
|
|