|
|
View previous topic :: View next topic |
Author |
Message |
skybox63
Joined: 30 Jun 2018 Posts: 17
|
Difference between Define and Variable |
Posted: Sat Jul 07, 2018 6:54 pm |
|
|
Hello,
I have another question:
What is the difference between these two statements:
Code: | int16 pin;
#define PinB0 PIN_B0
|
and
I thought that the define statement gives a name and a location to subsequent calls to RB0. Is it some type of pointer? Can it be passed as an argument via a function?
Isn't the pin statement similar in function?
Sorry for the multiple questions today but I'm trying to learn for future reference. I don't like just copying code and hoping it works without understanding why.
As always thank you.
Roger |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 125 Location: Bombay, India
|
|
Posted: Sat Jul 07, 2018 9:43 pm |
|
|
A #define is simply a text replacement command. So, whenever the compiler sees the #define keyword, it replaces the text following the #define keyword with the text after that. On the other hand, a variable is usually a RAM location that holds a value (which could also be something that you have #defined). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Sun Jul 08, 2018 1:58 am |
|
|
Jerson has given the basic explanation.
#DEFINE replacement is done before the actual code compilation.
On most compilers there is a separate 'preprocessor' pass, and it is this that does the #define substitutions. In CCS it is done 'first', but only as an internal pass inside the compiler operation.
it is fractionally more powerful than basic text replacement, but only in very limited ways. So (for instance) you can concatenate text strings, and can use it with the preprocessor commands to do certain types of test.
Now, neither the lines you actually show would do anything worthwhile without other code. 'PIN_B0' in itself means nothing, unless you have already had a statement to make this a variable than can be read.
The statement pin=PIN_B0, means "at runtime, load the value of the variable PIN_B0, into the variable 'pin'". So what it does is dependant of what the variable 'PIN_B0' actually 'is'.
The #define just substitutes the text 'PIN_B0' whenever the text 'PinB0' is typed.
A variable uses RAM space.
A #define does not.
A typical use of #DEFINE it to improve readability. So:
Code: |
#define MY_POWER_LED PIN_B0
//Then allows you to code:
output_high(MY_POWER_LED); //Turn on power LED
|
Which potentially makes the line 'say' what the pin being operated actually 'does', rather than just having the name it carries on the processor.
Because a #define is 'code wide', it also allows 'single point' changes. So if a chip has a particular I2C address, #defin ing this, then allows a single change in the define to change the address right through the code, while if the address was typed in at every location, dozens of changes would be needed. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9228 Location: Greensville,Ontario
|
|
Posted: Sun Jul 08, 2018 5:17 am |
|
|
couple of comments...
#DEFINE can also be used to create 'macros'. Small 'chunks of code' that
need get used throughout your program and it can save you a LOT of typing !
also
while your project is open, press F11, and the CCS manual 'magically' appears. You can easily search it 'tidbits' of information as well as lookup the correct syntax for functions and things. There are a few typos though generally it's accurate. As well, look at some of the examples CCS supplies in the 'examples' folder. Most are fairly well commented and cover most peripheral devices as well as internals like timers, UART, I2C.
Jay |
|
|
|
|
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
|