|
|
View previous topic :: View next topic |
Author |
Message |
stma
Joined: 16 Feb 2004 Posts: 26
|
32 bit components |
Posted: Tue Oct 10, 2006 1:40 am |
|
|
Hi,
I'm sure there is a avery simple solution to this.
I have an int32 and need to access this as 4 bytes. i.e. I need to write it to an eeprom as 4 bytes.
is there an easy way to do this.
Many Thanks
Steve |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 10, 2006 1:52 am |
|
|
You could do it by using the CCS make8() function to extract individual
bytes from int32 value and then write them, one at a time, to sequential
addresses in eeprom. |
|
|
stma
Joined: 16 Feb 2004 Posts: 26
|
|
Posted: Tue Oct 10, 2006 2:00 am |
|
|
Thanks PCM, was just ploughing thru the manual.
Haven't got to M yet!
Thanks for the help
Steve |
|
|
Ttelmah Guest
|
|
Posted: Tue Oct 10, 2006 4:10 am |
|
|
As an alternative, use a union. The advantage of this, is that it is portable to other 'C' implementations (make8, is 'CCS specific), and is completely 'bi-directional' in use. For example:
Code: |
union access {
int32 whole;
int8 b[4];
};
void output_bytes(union access val) {
int8 ctr;
for (ctr=0;ctr<4;ctr++) {
printf("Byte %d is: %2x\n",ctr,val.b[ctr]);
}
printf("32 bit value is: %8lx\n",val.whole);
}
|
If you call the function, with a 32bit integer, the union allows you to access the bytes as seperate elements. You can write values into
val.b[0]...val.b[3], and read them back the same way.
The third way to access the bytes, is with a pointer (again portable) so, if you have a 32 bit value 'val', the first byte is accessible with:
int_val=*((int8 *)&val);
Critically, because the pointer is converted to 'point' to a int8, the next byte is available with:
int_val=*(((int8 *)&val)+1);
CCS, is slightly 'dangerous' when dealing with pointers. In C, if you have a pointer to an int32 type, and increment it by one, it should move forward by 4 bytes. A pointer to an int8, should only move forward by one byte. In CCS, at times, the arithmetic for larger types, behaves as if all pointers are dealing with bytes. Explicitly forcing the pointer to address an int8, allows you to be _sure_ how the compiler will behave!...
So, there are a lot of choices.
If you are going to save multiple values to the EEPROM, of different sizes (int8, int16, float etc.), then consider using a generic save/retrieve, like:
Code: |
//routine to fetch 'num' bytes from EEPROM
void EEPROM_GET(int *ptr,int num,int eaddr) {
int count;
for (count=0;count<num;count++) {
*ptr++=READ_EEPROM(addr+count);
}
}
//write function
void EEPROM_PUT(int *ptr,int num,int eaddr) {
int count;
for (count=0;count<num;count++)
{
WRITE_EEPROM(addr+count,*ptr++);
}
}
|
Then if you have a 32 bit integer 'fred', and want to write it to the EEPROM at address 20, use:
EEPROM_PUT(&fred,sizeof(fred),20);
To get it back, use:
EEPROM_GET(&fred,sizeof(fred),20);
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
|