View previous topic :: View next topic |
Author |
Message |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
Beginner problem about shifting >> or << |
Posted: Fri Nov 01, 2013 2:19 am |
|
|
Hello mates.
I have a question.
I want to work with a variable of type word (int16) or long (Int32) but can only save in EEPROM values of type byte (INT8).
My question is: how to turn "word" in 2 values of type "byte" and how to make "long" in 4 values of "byte" and vice versa by shifting method with >> and << sign.
and read from EEPROM values type byte and transform in word or long.
value = read_eeprom (address)
write_eeprom (address, value)
I understand this syntax and know how to work.
Transforming I don't know.
Mathematically I know but for processor need to much time for conversion.
Thank you.
Best regards. |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Fri Nov 01, 2013 2:21 am |
|
|
See the use of a struct and union, discussed on this forum a couple of times already. That will do what you want without any overhead.
Regards |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19561
|
|
Posted: Fri Nov 01, 2013 4:58 am |
|
|
Seriously though the EEPROM write time will be hundreds of times the delay involved even using >>....
Remember a single byte write to the EEPROM, takes typically 2 to 4mSec (depending on the chip). You could rotate the number several thousand times even at quite a low clock rate in this time.
Look in the manual, at the section entitled "How do I write variables to EEPROM that are not a byte". The 'pointer' route is not (quite) as fast as the union, but the difference here will be insignificant.
Best Wishes |
|
|
darkrush
Joined: 29 Sep 2011 Posts: 18 Location: Colombia
|
|
Posted: Wed Nov 06, 2013 9:59 am |
|
|
I'm not sure if I can post this code.
Based on CCS manual I have always used this without problem hope it helps you.
To write:
Code: |
void write32_eeprom(long int n, int32 data) {
int i;
for (i = 0; i < 4; i++)
write_eeprom(i + n, *((int8 *)(&data) + i));
}
|
To read:
Code: |
int32 read32_eeprom(long int n) {
int i;
int32 data;
for (i = 0; i < 4; i++)
*((int8 *)(&data) + i) = read_eeprom(i + n);
return(data);
}
|
It can be easily changed to int16.
Regards |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Wed Nov 06, 2013 10:14 am |
|
|
also consider that if this is critical information data being stored in EEPROM, you want to be adding some form of inclusive data checkbyte(s) to test and insure on readback that the data written is not corrupted.
i see code like this all the time that blissfully ignores the possibility of reading back either uninitialized or corrupt data with no means of detecting the
possibility of a fault in it.
when i write a block of eeprom data, i ALWAYS write a checksum WORD including each value written starting with a seed of 0xC5C5 , and then store the two bytes after the last data value.
this gives a chance of only i part in 2^16 of data being wrong when read back
just my 2 cents |
|
|
|