CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Beginner problem about shifting >> or <<

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

Beginner problem about shifting >> or <<
PostPosted: Fri Nov 01, 2013 2:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Nov 01, 2013 2:21 am     Reply with quote

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: 19328

View user's profile Send private message

PostPosted: Fri Nov 01, 2013 4:58 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Nov 06, 2013 9:59 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Wed Nov 06, 2013 10:14 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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