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 support@ccsinfo.com

RAM over usage!!

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



Joined: 06 Sep 2006
Posts: 29

View user's profile Send private message

RAM over usage!!
PostPosted: Tue Aug 05, 2008 4:53 am     Reply with quote

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.
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Tue Aug 05, 2008 5:08 am     Reply with quote

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 Smile

Cheers,
Jos
Ttelmah
Guest







PostPosted: Tue Aug 05, 2008 6:23 am     Reply with quote

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
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Tue Aug 05, 2008 9:18 am     Reply with quote

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
ckielstra



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

View user's profile Send private message

PostPosted: Tue Aug 05, 2008 1:48 pm     Reply with quote

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;
}
domdom



Joined: 06 Sep 2006
Posts: 29

View user's profile Send private message

PostPosted: Tue Aug 05, 2008 6:34 pm     Reply with quote

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);
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