PDA

View Full Version : CON vs. =



AvionicsMaster1
- 9th February 2013, 21:44
Is there any difference or advantage to declaring a constant using CON vs. using an = sign to assign a variable value? i.e

widget CON 155 vs. widget = 155

Using just the = sign would require declaring the value beforehand and maybe use another few bits of memory. Beyond that is there any other disadvantage?

wdmagic
- 10th February 2013, 03:21
if you dont use it that way it will be like this

widget VAR Byte
widget = 155

and there is a possibility if your program is large that you might change widget = ?? something else later in your program
also say you want to tell widget that it is 25000 halfway into your program, but you only delared it as a byte, so it will
generate an error. but if you use CON then hey it auto sets the Variable size

rmteo
- 10th February 2013, 05:42
Look into the differences between constants and variables.

AvionicsMaster1
- 21st February 2013, 21:16
Well, when I didn't define the question correctly I got what I asked for. What I should have asked was, do both constants and variables use up memory or resources when programs compile or execute?

According to what I've read in the PBP3 manual, the answer for constants is they don't use memory of the micro-controller. They make substitutions during compile which I would assume uses less program bits.

Variables do use memory and depending how they are defined they can use a goodly portion. I assume that if a variable is defined as a word and you're only using a few bits you'd be wasting space. Better off defining it as a byte or using a small routine to store those bits as part of a larger word/byte. So if you're trying to save memory for other things, and your variable doesn't need to change, then defining it as a constant would free up RAM to be used elsewhere.

I've yet to write a program where I'm concerned about memory but I figure I'd start with good habits. Especially ones that let me program more efficiently.

Thanks for making me read the manual. It really is a wealth of info.

SteveB
- 21st February 2013, 21:57
So, if I understand the question, “Is there a difference between the following code snippets?”


MyByte VAR BYTE
MyByte = 155

MyByte VAR BYTE
MyCon CON 155
MyByte = MyCon
The answer is: No.

The only difference is how you read it as a human. For the PIC and it’s code, they are exactly the same.

AvionicsMaster1
- 22nd February 2013, 01:07
I agree but both of the examples use some of the PICs RAM where declaring MYCON CON 155 only would not.

At least that is my understanding.

SteveB
- 22nd February 2013, 15:30
I agree but both of the examples use some of the PICs RAM where declaring MYCON CON 155 only would not.

At least that is my understanding.

You are correct, constants use no RAM, but variables use RAM space depenedent on their type.