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

optimizing

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







optimizing
PostPosted: Fri May 07, 2004 2:54 am     Reply with quote

I've a problem with memory full, i help do optimizing the code:
I''ve a structure

Code:
struct sgara {
   int8 day;
   int8 months;
   int8 hours;
   int8 minutes;
   int8 seconds;
   int16 years;
   int16 vole;
   int8 F_I;         
   int8 mt;
   int16 p_mt90;
   int16 p_mt70;
   int16 p_mt50;
   int16 p_mt30;
   int16 p_parz;
   int16 p_tot;
   int8 flag_Rit;
   int8 flag_Buz;
};

I must save the structure in eeprom internal 18f452
I write the function that it supplies to make it, but it occupies much memory.
Do I optimize this function?:
Code:
void write_eep_struct(struct sgara *gara){
   write_eeprom(1, gara->day);
   write_eeprom(2, gara->months);
   write_eeprom_16(3,4, gara->years);
   write_eeprom(5, gara->hours);
   write_eeprom(6, gara->minutes);
   write_eeprom(7, gara->seconds);
   write_eeprom_16(8,9, gara->vole);
   write_eeprom(10, gara->F_I);
   write_eeprom(11, gara->mt);
   write_eeprom_16(12,13, gara->p_mt90);
   write_eeprom_16(14,15, gara->p_mt70);
   write_eeprom_16(16,17, gara->p_mt50);
   write_eeprom_16(18,19, gara->p_mt30);
   write_eeprom_16(20,21, gara->p_parz);
   write_eeprom_16(22,23, gara->p_tot);
   write_eeprom(24, gara->flag_Rit);
   write_eeprom(25, gara->flag_Buz);
}

and read:
Code:
void read_eep_blackout(struct sgara *gara){
   gara->day = read_eeprom(1);
   gara->months = read_eeprom(2);
   gara->years = read_eeprom_16(3,4);
   gara->hours = read_eeprom(5);
   gara->minutes = read_eeprom(6);
   gara->seconds = read_eeprom(7);
   gara->vole = read_eeprom_16(8,9);
   gara->F_I = read_eeprom(10);
   gara->mt = read_eeprom(11);
   gara->p_mt90 = read_eeprom_16(12,13);
   gara->p_mt70 = read_eeprom_16(14,15);
   gara->p_mt50 = fread_eeprom_16(16,17);
   gara->p_mt30 = read_eeprom_16(18,19);
   gara->p_parz = read_eeprom_16(20,21);
   gara->p_tot = read_eeprom_16(22,23);
   gara->flag_Rit = read_eeprom(24);
   gara->flag_Buz = read_eeprom(25);
}

where
Code:

int8 write_eeprom_16(char msb,char lsb,int16 dato){
   int8 reg2,reg1;
   int16 reg0;
   int8 m_msb,l_lsb;
   reg0 = dato & 0xFF00; //msb
   reg0 = reg0 >> 8;
   m_msb = (int8) reg0;
   l_lsb = (int8) dato & 0x00FF;
      write_eeprom (msb,m_msb);
      write_eeprom (lsb,l_lsb);
}
int16 read_eeprom_16(char address_h,char address_l){
   int8 msb,lsb;
   int16 dato,datotot;
      msb = read_eeprom(address_h);
      lsb = read_eeprom(address_l);
      dato = msb;
      dato = dato << 8;
      dato = dato | lsb;
      return dato;
}


Thanks and excuse me for my English!
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Fri May 07, 2004 3:04 am     Reply with quote

One quick thing: I strongly recommend you to define these two functions and use them instead:

Code:

void my_write_eeprom(byte Adress, byte Value)
{
    write_eeprom(Address,Value);
}


byte my_read_eeprom(byte Adress)
{
    return read_eeprom(Adress);
}


Now if you use my_write_eeprom() and my_read_eeprom() your code will be MUCH smaller.
the_real_maniac
Guest







PostPosted: Sat Jul 16, 2005 1:50 pm     Reply with quote

// ->> ! My english is bad so I hope you understand me. <<- !

I'm have little experience with C , but I almost learnt the basics of C, so here is and my sugestion ;)

Why you don't use bit fields and write the struct at onse ?

example:

struct sometype {

int num;
char ch;

}structname;

and something similar to fread(structname, sizeof(structname), 1, fd);

this reads size_t -> sizeof bytes from fd ... and keep them in structname and you have (default) 8 bits num and 8 bits ch -> 2 bYtes (16bits).

And if you have number like 20 -> 2^2 -> i.e 2 bits and you don't need the whole 8 bits Smile you can use bit fields and in this way to small the used memory at all.

And in main program (program ROM) and when writing the struct in EEPROM.

for example

int num : 2
int char : 7

now you saved almost 1 byte 6+1 = 7 bits of ram Smile just example.

when you read / write to EEPROM this saves time and ram
when you make the main program and write it to program RAM you save space/ memory again.

I hope you understand me , of course is posible i have some misunderstand and the whole my idea is wrong , but "you have to try" Smile

Good luck !

if you have to contact me ;)

the [dot] real [dot] maniac [at] gmail [dot] com
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