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 used in variable declaration

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



Joined: 19 Jul 2004
Posts: 33

View user's profile Send private message

RAM used in variable declaration
PostPosted: Tue Sep 14, 2004 6:51 pm     Reply with quote

Hi..

We are making a project(using 16F877) in which we require to
declare more variables. But I am worried about the RAM.

Can anybody give me an idea to declare variables which take
minimum RAM ?

And good tips of programming to reduce the RAM used.

Thanks...

Regards
Prashant
bdavis



Joined: 31 May 2004
Posts: 86
Location: Colorado Springs, CO

View user's profile Send private message

PostPosted: Tue Sep 14, 2004 7:55 pm     Reply with quote

Use bit variables. Only one bit of ram per variable, and VERY efficient use of ram. These of course are a boolean type of variable to state on or off...
alexbilo



Joined: 01 Jun 2004
Posts: 39
Location: Trois-Rivières

View user's profile Send private message

PostPosted: Tue Sep 14, 2004 8:18 pm     Reply with quote

The best way I know to save RAM is to use effeciently the different data types. Meaning that you should ask yourself what is the range needed for a given variable and choose the appropriate data type for this variable.

Moreover, you should use global variables only when it is absolutely necessary. If you can make a variable local to a function, then do it. That will make the code more readable, for a first, but it is also - supposed - to reduce the used RAM since the memory used by these local variables is allocated only when the function is executed. This may depends on the compiler, though.

These suggestions may seem obvious to many programmers, but since I don't know your knowledge level, I state them anyway.

Good luck,
_________________
Alex
Prashant Patel



Joined: 19 Jul 2004
Posts: 33

View user's profile Send private message

PostPosted: Tue Sep 14, 2004 8:38 pm     Reply with quote

Well, I know these things ..but want to know some detailed idea to manage the variables.

Thanks,
Prashant
bdavis



Joined: 31 May 2004
Posts: 86
Location: Colorado Springs, CO

View user's profile Send private message

PostPosted: Tue Sep 14, 2004 9:26 pm     Reply with quote

Oh to just manage the variables - I thought you just wanted to declare more variables in the same amount of space. To manage them, you can use structures and unions. I think naming conventions could also fall into this category.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Tue Sep 14, 2004 9:39 pm     Reply with quote

Quote:
Can anybody give me an idea to declare variables which take
minimum RAM ?


Don't use a data type bigger than what you need.
Use constants where possible. Especially when it comes to strings.
Prashant Patel



Joined: 19 Jul 2004
Posts: 33

View user's profile Send private message

PostPosted: Tue Sep 14, 2004 9:41 pm     Reply with quote

Sorry, I was meant to have more variables in the same space.
I know struct, union and naming convention.

I want to have variable to not declare to take more space unnecessary.

Let me ask you,
Does it make any difference in memory usage if we declare the
variable like

[1]
int Mode;
int ExeMode;

and/or

int Mode, ExeMode; // Both are in same line...

[2]
#define PULSE_TIME 65036

or

int16 CONST PULSE_TIME= 65036;



Thanks..
Regards
Prashant


Last edited by Prashant Patel on Tue Sep 14, 2004 9:51 pm; edited 1 time in total
bdavis



Joined: 31 May 2004
Posts: 86
Location: Colorado Springs, CO

View user's profile Send private message

PostPosted: Tue Sep 14, 2004 9:49 pm     Reply with quote

No need to appologize - I just mis-understood...
You probably want examples...

Try stuff like the below for your port pin definitions:
struct
{
unsigned char SWITCH_A0 : 1; /* 0 */
unsigned char SWITCH_A1 : 1; /* 1 */
unsigned char SWITCH_A2 : 1; /* 2 */
unsigned char SWITCH_A3 : 1; /* 3 */
unsigned char RESET_PIN : 1; /* 4 */
unsigned char MODE_SELECT : 1; /* 5 */
unsigned char NOT_USED_A6 : 1; /* 6 */
unsigned char NOT_EXIST_A7 : 1; /* 7 */
} PORTA_b;
#locate PORTA_b=0xF80

OR - if not using a struct, you could use something like...
#bit B_SWITCH_B0 = PORTB.0
#bit B_SWITCH_B1 = PORTB.1
#bit B_SWITCH_B2 = PORTB.2
#bit B_ICD_PIN_B3 = PORTB.3
#bit B_SWITCH_B3 = PORTB.4
#bit B_NOT_USED_B5 = PORTB.5
#bit B_ICD_PIN_B6 = PORTB.6
#bit B_ICD_PIN_B7 = PORTB.7

Hope that helps...
bdavis



Joined: 31 May 2004
Posts: 86
Location: Colorado Springs, CO

View user's profile Send private message

PostPosted: Tue Sep 14, 2004 9:54 pm     Reply with quote

To answer your previous question - the answer is no.

Space used is number of variables multiplied by the size of each variable for all global variables. For local variables, the CCS compiler will optimize for you by reusing the same memory region for multiple variables that aren't used at the same time, which saves data memory.

You can also have constant data variables stored in Flash that won't take up local data memory in the PIC.
Prashant Patel



Joined: 19 Jul 2004
Posts: 33

View user's profile Send private message

PostPosted: Tue Sep 14, 2004 10:15 pm     Reply with quote

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