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

My PIC runing out of space???
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sonicdeejay



Joined: 20 Dec 2005
Posts: 112

View user's profile Send private message

My PIC runing out of space???
PostPosted: Fri Jan 27, 2006 3:52 am     Reply with quote

I have just written a Rs232 Com code in my PIC18F2331,using ICD2.0...

when i compile with PIC C... it shows as follow..






Is that mean ...I am running of space for futher programming??? Shocked
Ttelmah
Guest







PostPosted: Fri Jan 27, 2006 5:56 am     Reply with quote

Possibly not really...
The first thing is that there are quite a few things loaded initially. So (for instance), the timer code, and serial handler code. Then if you use arithmetic, especially with floats, the individual routines for these are loaded when they are first used. It is common, to find that the first 100 lines of code, uses five or six times as much space as the next 100 lines, with these 'extras', only being loaded once. So you may have more space left than you think. However the amount used, suggests some 'memory hogs' may be at work. One 'classic', is a lot of printf statements. These _drink_ memory, and if (for instance), there are four places in your code, where 'similar' outputs are needed (perhaps a message, and a number in a standard format), if you code four seperate printf's, it'll use a lot more space, than if this is put into a 'wrapper'. Also remember there is a lot of overhead from const strings.

Best Wishes
sonicdeejay



Joined: 20 Dec 2005
Posts: 112

View user's profile Send private message

PostPosted: Fri Jan 27, 2006 1:06 pm     Reply with quote

this is all t code i've got...I am using PIC18F2331 btw...




Quote:

#if defined(__PCM__)
#include <18F2331.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8) // Jumpers: 8 to 11, 7 to 12

#elif defined(__PCH__)
#include <18F2331.h>
#device ADC=8
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8) // Jumpers: 8 to 11, 7 to 12
#endif
#include<stdlib.h>
#include<math.h>

void main() {

int i, min, max,value;
float valuefloat;
float head,flow,freq,x,b,c; //for freq, flow input and head output
char st1[5];


setup_port_a( sAN0 );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );

do {

printf("Sampling:\n\r");

delay_ms(1000);
output_high(PIN_B5);
//output_high(PIN_A4);
//delay_ms(1000);
//printf("Sampling:\n");
// printf("the voltage is ");
//delay_ms(1000);
// delay_ms(1000);
// printf(" HOW ARE YOU? ");
// delay_ms(1000);
// output_low(PIN_A4);
//output_low(PIN_B5);
//delay_ms(1000);


//min=0xffff;
//max=0;
//for(i=0; i<=30; ++i) {
delay_ms(100);
valuefloat = Read_ADC();
valuefloat = (valuefloat/51);
//if(value<min)
// min=value;
//if(value>max)
// max=value;
// }
printf("\n\rValue: %8.4f V \n\r",valuefloat);
//printf("\n\rMin: %4LX Max: %4LX\n\r",min,max);

//for freq input, and calculate head, flow graph.
printf("\n Enter Freq: ");
gets(st1);
freq=atof(st1);

printf("\n You Entered: %8.4f\n\n",freq); ///to test input process
//the graph for a specific freq is y=-0.0019x^2-bx+c
b=-0.00004559 - (0.00092457* freq);
c=-40.06733 + 1.7434*freq;
//after we have got b and c factors, we get head and flow relation for that freq.
printf("\n Enter Flow: ");
gets(st1);
flow=atof(st1);
head=-0.0019 * pow(flow,2) - b * flow + c;
printf("\n The Head now is : %8.4f \n\n",head);
delay_ms(2000);

} while (TRUE);
}


Last edited by sonicdeejay on Thu Feb 02, 2006 12:02 pm; edited 2 times in total
dorinm



Joined: 07 Jan 2006
Posts: 38

View user's profile Send private message

PostPosted: Fri Jan 27, 2006 1:33 pm     Reply with quote

first of all, stdlib.h and math.h eats A LOT of space!

for instance, you could use
flow*flow instead of pow(flow,2) , comment the math include and you save ~35% of ROM, which is a lot for a 4k words PIC (fourth part of a 458)!

you could also get rid of atof() from stdlib.h which could save another ~8%, but i'll leave you to find out how ;)
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Fri Jan 27, 2006 2:47 pm     Reply with quote

--snip--
sonicdeejay wrote:

//the graph for a specific freq is y=-0.0019x^2-bx+c


7th grade Algebra:

y = Ax^2 + Bx + C

y = x(Ax+B) + C <= fewer multiplies and definatly fewer cycles than using pow()!

Next step would be to convert to a fixed point format and use 16-bit or 32-bit integers and ditch the floating point stuff altogether.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
EmbdFreak



Joined: 28 Jul 2005
Posts: 23

View user's profile Send private message

PostPosted: Tue Jan 31, 2006 10:59 pm     Reply with quote

Try to change the pointer size and then compile, i.e add the *=16 in the adc device definition
Code:

#device *=16 ADC=8


Regards,
EF
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Feb 01, 2006 2:25 am     Reply with quote

EmbdFreak wrote:
Try to change the pointer size and then compile, i.e add the *=16 in the adc device definition
Code:

#device *=16 ADC=8


Regards,
EF
This would be a good suggestion when using a 14-bit processor from the PIC16 series. Sonicdeejay has a PIC18 which uses 16 bit pointers by default so this command has no effect.
sonicdeejay



Joined: 20 Dec 2005
Posts: 112

View user's profile Send private message

PostPosted: Wed Feb 01, 2006 9:07 pm     Reply with quote

Guys...eventually I might have to use stdlib and math.h....

If the "ROM" become 100% full...I can't load any more code anymore? Shocked
sonicdeejay



Joined: 20 Dec 2005
Posts: 112

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 6:20 am     Reply with quote

Guys,,,my code is 100% now,,PIC C will not compile and it said the program is too large,,,

How can I solve it???


I was using PIC18F2331 and ready to change to PIC18F4431 which has bigger space,,,but where can I change the setting in PIC C program that I am using PIC18F4331 now,in stead of PIC18F2331......

Sad
sonicdeejay



Joined: 20 Dec 2005
Posts: 112

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 6:23 am     Reply with quote

This is my code... I have about 5 print f sentences...




Quote:
//System Initialisation
#if defined(__PCM__)
#include <18F2331.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000) // 16Mhz Crystal is Used
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8) // RS232 Configuration

#elif defined(__PCH__)
#include <18F2331.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000) // 16Mhz Crystal is Used
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8) // RS232 Configuration
#endif
#include <stdlib.h>

void freq_curve(float freq,float &a1,float &b1,float &c1);
void fricfact_curve(float fricfact,float &a2,float &b2,float &c2);
float newton_root(float a,float b,float c,float maxerr);
float Read_fricfact_percent_from_ADC();
float find_new_freq(float x,float &y,float maxerr);

//Start main program
void main()

{ float freq=55,fricfact=0.5;
float maxerr=0.01;
float a1,b1,c1,a2,b2,c2;
float x,y,yhead;
char st1[5];

//Calculating Frequency curve
freq_curve(freq,a1,b1,c1);
printf("\n %8.4f %8.4f %8.4f\n",a1,b1,c1);

//Calculating Friction Factor curve
fricfact_curve(fricfact,a2,b2,c2);

//Finding the intersection point
x=newton_root(a1-a2,b1-b2,c1-c2,maxerr);

//calculate the y-mark from known x-mark using freq_curve.
y= a1*x*x + b1*x + c1;
printf("\n\rThe intersection point is x =%8.4f, y =%8.4f\n",x,y);

//Inquiry the desired fixed head from user
printf("\n\rEnter desired fixed HEAD : ");
gets(st1);
yhead=atof(st1);

//Reading the friction factor from ADC
fricfact=Read_fricfact_percent_from_ADC();
printf("\n\rThe percent read from ADC = %8.4f",fricfact);

//Calculating new Fricfact_curve
fricfact_curve(fricfact,a2,b2,c2);

//Evaluating new intersection point
x=newton_root(a1-a2,b1-b2,c1-c2,maxerr);
//Calculate the y-mark from new x-mark
y=a1*x*x + b1*x + c1;
//("\n\rThe NEW intersection point is x =%8.4f, y =%8.4f\n",x,y);

//evaluating intersection pt which intersect fricfact_curve & yhead pt.3
x=newton_root(a2,b2,c2-yhead,maxerr);
y=a2*x*x + b2*x + c2;
//("\n\rThe intersection point 3 is x =%8.4f, y =%8.4f\n",x,y);

//finding the new frequency curve which goes through the pt.3 (x,y)
freq=find_new_freq(x,y,maxerr);
printf("\n\rThe new frequency = %8.4f",freq);
("\n\rNew head(y) = %8.4f",y);

}

//the called sub functions
void freq_curve(float freq,float &a1,float &b1,float &c1)
{
a1=-0.0019;
b1=-0.00004559-0.00092457*freq;
c1=0.00307143-0.0001214286*freq+0.018353*freq*freq;
}
void fricfact_curve(float fricfact,float &a2,float &b2,float &c2)
{
a2=0.0491555 - 0.94984*fricfact + 4.8265*fricfact*fricfact;
b2=-0.32667 + 4.355*fricfact - 20.14228*fricfact*fricfact;
c2=1.11171 - 11.9291*fricfact + 56.499*fricfact*fricfact;
}
float newton_root(float a,float b,float c,float maxerr)
{ float x=4,old,t,r;
do{
old=x;
x=x-((a*x*x) + b*x + c)/( 2*a*x + b);
}while(abs((x-old)/x*100)>maxerr);

if (x<0)
{ t=x;
r=b+a*t;
x=-r/a;
}
return(x);
}
float Read_fricfact_percent_from_ADC()
{ float fricfact;
setup_port_a( sAN0 );
setup_adc( ADC_CLOCK_INTERNAL );
set_adc_channel( 0 );
fricfact = Read_ADC();
fricfact = (fricfact/10.2/100); // if 8 bits divide by 51, if 10bits dvides by 204, %=divide by 10.2
if (fricfact<0.1)
fricfact=0.1;
else if (fricfact>1)
fricfact=1;

return (fricfact);
}
float find_new_freq(float x,float &y,float maxerr)
{ float temp,freq,a1,b1,c1;
float stepsize=0;
do{
freq=freq+stepsize;
freq_curve(freq,a1,b1,c1);
temp=a1*x*x + b1*x + c1;
stepsize=(y-temp)*0.5;
}while(abs((y-temp)/y*100)>maxerr);
return (freq);
}[img][/img]


Last edited by sonicdeejay on Thu Feb 02, 2006 12:03 pm; edited 1 time in total
sonicdeejay



Joined: 20 Dec 2005
Posts: 112

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 6:32 am     Reply with quote

or addional RAM chip is a solution.....??

HELP me.... Confused
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Thu Feb 02, 2006 8:39 am     Reply with quote

sonicdeejay wrote:
This is my code... I have about 5 print f sentences...
}


Even with the printf statements commented out you are at 93%. I suspect your call to atof() is killing you. Just substituting
Code:
float atof(char *s)
{
   return(1.0);
}

and commenting out #include <stdlib.h>
dropped your ROM useage about 10%.

Also the use of floating point operations is very expensive in both memory and execution cycles. Switch to fixed point. I'd bet that even doing things with 32 bit signed integers would be faster. Easiest way to start that is look at all your fractions and decide how many places to move the decimal point and still keep a reasonable accuracy. Then just convert everything to an integer and later when you print results, manage the decimal point yourself.

And if you standardize your responses to contain only 1 value per printf you can write a wrapper function for your printf call and that should produce only one instance of the printf code.

Next, why do you have two different sets of #includes and chip setups that are identical except for the compiler type flag? Doesn't take any extra space but just seem like a waste of typing.

If you are going to change chip types, change the #include<chiptype> header. Depending on your editor you may also have to go into the project file and make a change there too. If you are compiling from the command line, probably don't need any other changes but RTFM on that one.

Last thing, please learn to use the Code buttons when posting code samples
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
sonicdeejay



Joined: 20 Dec 2005
Posts: 112

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 12:01 pm     Reply with quote

thx for the reply....


if i want y=3.24 ,, y has to be delcared as "float" or any other alternative to bring the space down....??



or addional RAM chip is a solution.....??
jspencer



Joined: 22 Dec 2003
Posts: 57
Location: Boise, ID USA

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 1:03 pm     Reply with quote

I think what Rob means is that if you need nine decimal place accuracy your numbers would now look like this:

0.0001214286 -> 1214286
1.11171 ->1111710000

and use int32 instead of floats. Depending on the max and min values that you could possibly get, you may even be able to move to an int16, saving space and processing time.
sonicdeejay



Joined: 20 Dec 2005
Posts: 112

View user's profile Send private message

PostPosted: Thu Feb 02, 2006 1:11 pm     Reply with quote

thx for the reply....I'm thinking of changing new PIC with more eeprom to store my stuff.....

my original PIC18F2331/2431/4431...




my new intended PIC18F4620...





and I change my code to

Code:
#include <18F4620.h>



and compile it...

I can manage to get only about 15% ROM while the same code will make 100% FULL on my previous PIC18F2331...




so,,by changing into new PIC...I think my low space issue will be solved...

Will my code works in the New PIC as well??/

Code:
#include <18F4620.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000) // 16Mhz Crystal is Used
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8)  // RS232 Configuration
//#endif//

#include <stdlib.h>
#include <math.h>

void freq_curve(float freq,float &a1,float &b1,float &c1);
void fricfact_curve(float fricfact,float &a2,float &b2,float &c2);

//void float atof(char *s);

float newton_root(float a,float b,float c,float maxerr);
float Read_fricfact_percent_from_ADC();
float find_new_freq(float x,float &y,float maxerr);

//Start main program
void main()

{
   int freq=55;
   float fricfact=0.5;
   float maxerr=0.01;
   float a1,b1,c1,a2,b2,c2;
   float x,y,yhead;
   char st1[5];

   //Calculating Frequency curve
   freq_curve(freq,a1,b1,c1);
   printf("\n %8.4f %8.4f %8.4f\n",a1,b1,c1);

   //Calculating Friction Factor curve
   fricfact_curve(fricfact,a2,b2,c2);

   //Finding the intersection point
   x=newton_root(a1-a2,b1-b2,c1-c2,maxerr);

   //calculate the y-mark from known x-mark using freq_curve.
   y= a1*x*x + b1*x + c1;
   printf("\n\rThe intersection point is x =%8.4f, y =%8.4f\n",x,y);

   //Inquiry the desired fixed head from user
   printf("\n\rEnter desired fixed HEAD : ");
   gets(st1);
   yhead=atof(st1);

   //yhead=st1;
   //printf(" \n\ Gangster = %8.4f",yhead);


   //Reading the friction factor from ADC
   fricfact=Read_fricfact_percent_from_ADC();
   printf("\n\rThe percent read from ADC = %8.4f",fricfact);

   //Calculating new Fricfact_curve
   fricfact_curve(fricfact,a2,b2,c2);

   //Evaluating new intersection point
   x=newton_root(a1-a2,b1-b2,c1-c2,maxerr);
   //Calculate the y-mark from new x-mark
   y=a1*x*x + b1*x + c1;
   printf("\n\rThe NEW intersection point is x =%8.4f, y =%8.4f\n",x,y);

   //evaluating intersection pt which intersect fricfact_curve & yhead pt.3
   x=newton_root(a2,b2,c2-yhead,maxerr);
   y=a2*x*x + b2*x + c2;
   printf("\n\rThe intersection point 3 is x =%8.4f, y =%8.4f\n",x,y);

   //finding the new frequency curve which goes through the pt.3 (x,y)
   freq=find_new_freq(x,y,maxerr);
   printf("\n\rThe new frequency and New Head= %8.4d %8.4f",freq,y);
   ("\n\rNew head(y) = %8.4f",y);

}

//the called sub functions
void freq_curve(float freq,float &a1,float &b1,float &c1)
{
   a1=-0.0019;
   b1=-0.00004559-0.00092457*freq;
   c1=0.00307143-0.0001214286*freq+0.018353*freq*freq;
}
void fricfact_curve(float fricfact,float &a2,float &b2,float &c2)
{
   a2=0.0491555 - 0.94984*fricfact + 4.8265*fricfact*fricfact;
   b2=-0.32667 + 4.355*fricfact - 20.14228*fricfact*fricfact;
   c2=1.11171 - 11.9291*fricfact + 56.499*fricfact*fricfact;
}
float newton_root(float a,float b,float c,float maxerr)
{  float x=4,old,t,r;
   do{
      old=x;
      x=x-((a*x*x) + b*x + c)/( 2*a*x + b);
   }while(abs((x-old)/x*100)>maxerr);

   if (x<0)
   {   t=x;
      r=b+a*t;
      x=-r/a;
   }
   return(x);
}
float Read_fricfact_percent_from_ADC()
{  float fricfact;
  //setup_port_a( sAN0 );
  setup_adc( ADC_CLOCK_INTERNAL );
  set_adc_channel( 0 );
         fricfact = Read_ADC();
         fricfact = (fricfact/10.2/100); // if 8 bits divide by 51, if 10bits dvides by 204, %=divide by 10.2
  if (fricfact<0.1)
      fricfact=0.1;
  else if (fricfact>1)
      fricfact=1;

return (fricfact);
}
float find_new_freq(float x,float &y,float maxerr)
{  float temp,freq,a1,b1,c1;
   float stepsize=0;
   do{
      freq=freq+stepsize;
      freq_curve(freq,a1,b1,c1);
      temp=a1*x*x + b1*x + c1;
      stepsize=(y-temp)*0.5;
   }while(abs((y-temp)/y*100)>maxerr);
   return (freq);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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