![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
domdom
Joined: 06 Sep 2006 Posts: 29
|
RAM over usage!! |
Posted: Tue Aug 05, 2008 4:53 am |
|
|
I have a few variables which consume large amount of RAM
Code: |
#include <16F877A.h>
#device *=16
#device PASS_STRINGS = IN_RAM
unsigned char Buffer[86];
unsigned char Command[40];
unsigned char AIS_String[78];
unsigned char ServerIP[16] = "203.251.115.154";
unsigned char E[2] = "E";
unsigned char PORT[5] = "4949" ;
unsigned char S_ID[4] = "02";
unsigned char *ptr,term[3];
unsigned char checksum1[5];
unsigned char print_string[5];
unsigned char Status[3];
char const Filter[5][17] =
{
{"VDO"},
{"VDM"},
{"ALR"},
{"TXT"},
{"203.251.115.147"},//Client_IP
};
.
.
|
my program is basically like
Code: |
Buffer[index] = getc;
sprintf(AIS_String,"%s,%s,%d,%d,%s",ClientID,ID,1,2,Buffer);// I will be using AIS_String variable to calculate checksum
|
My program is out of RAM usage. Besides sprintf function, is there any way to minimize the RAM usage without using the AIS_String variables. |
|
![](templates/subSilver/images/spacer.gif) |
Foppie
Joined: 16 Sep 2005 Posts: 138 Location: The Netherlands
|
|
Posted: Tue Aug 05, 2008 5:08 am |
|
|
2 things come up in my mind:
first: How are ClientID and ID defined?
second: Buffer is bigger than AIS_String, how do you handle this possible overflow?
AS answer on your question about handling your RAM problem: I would personally get rid of the AIS_String. The 16F877A doesn't have that much ram to play with so personnaly I would spare the RAM and make some functions that can calculate the crc by taking the pieces of the string piece by piece. Something like: Code: | int8 crc,i;
crc = ClientID[0];
for (i = 1; i < strlen(ClientID); i++)
crc ^= ClientID[i];
crc ^= ',';
for (i = 0; i < strlen(ID); i++)
crc ^= ID[i];
crc ^= ',';
crc ^= '1';
crc ^= ',';
crc ^= '2';
crc ^= ',';
for (i = 0; i < strlen(Buffer); i++)
crc ^= Buffer[i];
|
Above could will most likely not match your crc algorithm and I doubt it is without errors. But hopefully thsi gives you an idea
Cheers,
Jos |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Tue Aug 05, 2008 6:23 am |
|
|
Also, do you _need_ to use PASS_STRINGS_IN_RAM?. If it is only because you want to use pointers, then look at the #ROM declaration instead.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Aug 05, 2008 9:18 am |
|
|
One idea, albeit saves just a little, would be to use an int8 instead of a char to store data. For example, your ServerIP uses 16 characters to store your IP address. You could declare four int8 variables and store the same thing in less space, ie:
int8 ServerIP1 = 203;
int8 ServerIP2 = 251;
int8 ServerIP3 = 115;
int8 ServerIP4 = 154;
You could re-assemble/convert these numbers to be used later in your code. If there are other variables that could be done this way it might save a bit of space.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Aug 05, 2008 1:48 pm |
|
|
If the checksum code is send after the bytes over which the checksum is to be calculated you can use another approach. This technique uses the unique CCS feature where you can redirect the output of fprintf to your own output function.
something like: Code: | int8 Crc = 0; // reset CRC to initial value
// Your long printf, redirected using an fprintf
fprintf(PutcAndUpdateCrc, "%s,%s,%d,%d,%s", ClientID, ID, 1, 2, Buffer);
// Send the checksum using a normal printf.
printf("%d", Crc);
...
///////////////////////////////////////////////////////////////////////////////
// Send 1 byte over serial port and update internal CRC.
void PutcAndUpdateCrc(int8 Data)
{
putc(Data);
Crc8(Data);
}
///////////////////////////////////////////////////////////////////////////////
// Update Checksum
void Crc8(int8 Data)
{
Crc ^= Data;
} |
|
|
![](templates/subSilver/images/spacer.gif) |
domdom
Joined: 06 Sep 2006 Posts: 29
|
|
Posted: Tue Aug 05, 2008 6:34 pm |
|
|
Foppie wrote: | 2 things come up in my mind:
first: How are ClientID and ID defined?
second: Buffer is bigger than AIS_String, how do you handle this possible overflow?
|
Code: |
unsigned char Client_ID[6] = "$SDCLT";
unsigned char ID = "03";
|
Since the Buffer size is larger than AIS_String, therefore I need to figure out another way to calcutate the checksum. Here is my checksum function which is tested and works.
Code: |
BYTE ComputeChecksum(char *str,int string_length)
{
BYTE checksum_value = 0;
int index = 1; // Skip over the $ at the begining of the sentence
while( index < string_length && str[index] != '*' )
{
checksum_value ^= str[index];
index++;
}
return(checksum_value);
} |
I would use the above function to get a checksum like following:
Code: |
unsigned char AIS_String[79];
sprintf(AIS_String,"$SDCLT,%s,%d,%d,%s",S_ID,2,1,Buffer);
len = strlen(AIS_String);
checksum = ComputeChecksum(AIS_String,len);
sprintf(checksum1, "*%02X", (int) checksum);
fprintf(Server,"%s%s%c%c", AIS_String,checksum1,0x0D,0x0A); |
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|