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

General tips to conserve ROM in large applications

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



Joined: 11 Sep 2008
Posts: 44

View user's profile Send private message

General tips to conserve ROM in large applications
PostPosted: Tue Sep 01, 2009 9:15 am     Reply with quote

What are some great ways to conserve ROM in large programs on a PIC16F877A chip?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Sep 01, 2009 12:12 pm     Reply with quote

Here is a post of mine from the old CCS forum on this topic.
These comments apply to 16F PICs.

===================================
I had a similar problem. My 16c77 was 93% full.
I know that the compiler starts to have problems if it
gets to 95%, so I really only had 2% space left.

So I looked at the .LST file. I saw many, many places
where the compiler used ROM very inefficiently.
In the first hour of examining the .LST file, I recovered
400 bytes by re-writing my code. In the next hour I
got back maybe 300 more bytes, and so on. Eventually,
I probably recovered at least 1500 bytes.

1. CCS does pointer operations very inefficiently.

Don't do:
ptr = buffer;
a = *ptr++;
b = *ptr++;
c = *ptr++;

Do this, if possible. It uses a lot less ROM:
a = buffer[0];
b = buffer[1];
c = buffer[2];

-----------------------------------
2. Avoid using CCS string functions. They use up tons of ROM.

If you are calling one string function many times,
then it's not so bad. But if you use a CCS string
function just once, then you are wasting a lot of
ROM for just one function. There is usually a cheaper
way to do it.

----------------------------------
3. Avoid using printf() or lcd_putc() with strings.
If you are sending 5 characters or less, then it is
cheaper to use constant characters.

Don't do:
printf("ABC");

Do:
putc('A');
putc('B');
putc('C');

----------------------------------------------
Code can usually be written more efficiently.
Here was the original way that I sent commands.
Note: I sent a lot more than 3 commands, and they
were spread out all over the program. I am just
using 3 commands together, below, as an example.

puts("$PXXXP"); // Send P command
puts("$PXXXA"); // Send A command
puts("$PXXXR"); // Send R command

Here's a cheaper way to do it. Create a central
routine that sends the command string. Then just
call that routine with the command letter.

send_command('P');
send_command('A');
send_command('R');

void send_command(char command_letter)
{
printf("$PXXX");
putc(command_letter);
putc(0x0d);
putc(0x0a);
}

It might take you a few days, but I will bet that
you can recover 1K to 2K of your ROM space by looking
at the .LST file.
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

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

RE:
PostPosted: Thu Sep 03, 2009 3:42 am     Reply with quote

excellent tips indeed.

I think a listing of similar coding tips should be made sticky...

thanks
a
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