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 CCS Technical Support

Saving a 32bit int

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



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

Saving a 32bit int
PostPosted: Sun Feb 05, 2006 3:07 am     Reply with quote

I've always wondered what the best way to save a 32bit int is. What method if any would you use and why?

// Structure for 32 bit int
struct byteint
{
char b1;
char b2;
char b3;
char b4;
};

union I8
{
int32 I32;
struct byteint I8;
};

union I8 timer_value;



void Save1(void)
{
WRITE_EXT_EEPROM(ee_timer, timer_value.I8.b1);
WRITE_EXT_EEPROM(ee_timer+1, timer_value.I8.b2);
WRITE_EXT_EEPROM(ee_timer+2, timer_value.I8.b3);
WRITE_EXT_EEPROM(ee_timer+3, timer_value.I8.b4);
}

void Save2(void)
{
int temp=0;
int temp16=0;

do
{
if(temp==0)
temp16=timer_value.I8.b1;
if(temp==1)
temp16=timer_value.I8.b2;
if(temp==2)
temp16=timer_value.I8.b3;
if(temp==3)
temp16=timer_value.I8.b4;

WRITE_EXT_EEPROM(ee_timer+temp, temp16);
temp+=1;
}while(temp<4);
}

void Save3(void)
{
int temp=0;
do
{
WRITE_EXT_EEPROM(ee_timer+temp, timer_value.I32>>((temp+1)*8));
temp+=1;
}while(temp<4);
}

void Save4(void)
{
int temp=0;
do
{
WRITE_EXT_EEPROM(ee_timer+temp, *(&timer_value.I32+temp));
temp+=1;
}while(temp<4);
}
Ttelmah
Guest







PostPosted: Sun Feb 05, 2006 10:07 am     Reply with quote

Save1, or write the fourth version, using make8, and make32.

The union approach, or the CCS 'MAKEn' functions, do direct I/O to the individual bytes. The pointer approach, adds significant code, and time. There are probably another dozen versions you could actually generate, but generally these two are efficient. As an alternative approach:
Code:

union tobyte {
   int8 b[4];
   int32 word;
} ;

void Save5(union tobyte value) {
   int8 ctr;
   for (ctr=0;ctr<4;ctr++) {
       write_ext_eeprom(ee_timer+ctr,value.b[ctr]);
   }
}

and call this with an int32 value 'passed' to the function.

So:
Code:

int32 val_to_send;
val_to_end=56L;
Save5(val_to_send);



Will automatically 'pass' the int32 value, into the 32bit part of the union, and then send the bytes.
I suggest you simply look at the actual code size generated, to get an idea of the efficiency of each approach. There is a 'tradeoff', in that pointer/variable versions, will add code for the byte accesses, but 'looped' versions will remove the need for multiple EEPROM writes. I use a 'generic' version, which receives a pointer, a count to save, and the EEPROM address, and can then be used for any variable size, rather than being designed just to handle an int32.

So:
Code:

void write_eeprom_block(int8 *from_mem,int8 no_to_save,int16 locn) {
   while(no_to_save--) {
       write_ext_eeprom(from_mem++,locn++);
   }
}


So for an int32 'value', 'write_eeprom_block(&value,4,ee_timer);'.
This is more efficient, when you have a lot of different I/O operations (I do).

Best Wishes
Eugeneo



Joined: 30 Aug 2005
Posts: 155
Location: Calgary, AB

View user's profile Send private message

PostPosted: Sun Feb 05, 2006 11:47 am     Reply with quote

Code:

void write_eeprom_block(int8 *from_mem,int8 no_to_save,int16 locn) {
   while(no_to_save--) {
       write_ext_eeprom(from_mem++,locn++);
   }
}

write_eeprom_block(&value,4,ee_timer);'.



I am more of an electronics person less programmer so my knowledge base was from Qbasic. But that is really slick. I never would have considered passing a pointer through the function. After looking at this, I can tell that this will produce the best results with multiple eeprom writes.

Thank-you for that.
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