View previous topic :: View next topic |
Author |
Message |
id31
Joined: 19 Dec 2014 Posts: 16
|
access to a bit |
Posted: Mon Jan 23, 2017 2:08 am |
|
|
Hi,
I have a question please,
I declared:
#bit TRMT=TX1STA.1
if I want to create another variable - NEW_VAR that I can access to TRMT value so:
if (TRMT)
and
if (NEW_VAR)
will be the same..
is this possible?
is
int1 NEW_VAR=TRMT; ,is good?
#define NEW_VAR TRMT ,is good?
#bit NEW_VAR=TRMT not compile..
thank you |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Mon Jan 23, 2017 2:56 am |
|
|
Code: |
#define NEW_VAR TRMT
|
Is the way to do it... but it is not a "new variable", it is simply the same TRMT with another name.
However, I do not see any reason why this might be a good idea, and it is pretty much certainly not necessary. It is rarely better to have two or more names for the same thing: it just creates confusion. What is wrong with TRMT? Especially when it is the MIcrochips's name for a peripheral control bit? |
|
|
id31
Joined: 19 Dec 2014 Posts: 16
|
|
Posted: Mon Jan 23, 2017 3:02 am |
|
|
OK, got it! thank you |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19548
|
|
Posted: Mon Jan 23, 2017 3:08 am |
|
|
This:
int1 NEW_VAR=TRMT;
won't work.
It declares a new variable sitting somewhere else in memory, that is then loaded at the moment of declaration with the value in 'TRMT'.
Writing to NEW_VAR would write to it, not to TRMT. Reading NEW_VAR, would read it, and if TRMT has changed, the value would be out of date. Also wastes a bit of RAM to hold this.
This:
#define NEW_VAR TRMT
is perfectly fair. All #define does is create a text macro. So whenever 'NEW_VAR is typed in the code, it'll be replaced at compile time, with TRMT.
You can also just use:
#bit NEW_VAR=TXSTA.1
This creates another variable called 'NEW_VAR' that points to the same location in memory as TRMT.
Declaring an actual new variable will have the same effect in CCS. However in languages that implement more stringent type checking, it ensures that the compiler 'knows' the actual type of the variable involved, when performing syntax checking. |
|
|
|