This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

C# Language "shortcuts", "BIT" variable defenition

Other Parts Discussed in Thread: MSP430G2231

Hi everyone!

My question may look to obvious to some of you and that is better for me ))))

At this page(this is the MSP430 LaunchPad Drive LED tutorial):http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_Drive_LED

I found some things that were new to me (as a newbie in all this LaunchPad stuff), my question refers to this lines of code:

  [CODE]

#define LED_0 BIT0 
#define LED_1 BIT6
[/CODE]

 As I can understand from the above code,the "LED_0" is a name, and  BIT0 means 0x01 number, so wherever I'll use "LED_01" in my code the compiler will interpret this like "BIT0" which actually means 0x01. I'm little familiar with C# language, had some college courses, but in our labs we never used these shortcuts, only the direct input of Hex numbers (like 0x04 and etc)

 Now, back to "what I actually want from you":

My question is about these code "shortcuts", where can I get more info about it? Some User tutorials, where all this things combined and explained together

There is some Assembler User Guide that I found for LaunchPad but nothing for C# language.


Thanks in advance for your help.

Ivan

 

 

  • The MSP430 is not programmed in C#, but in C or C++.  The statements you are asking about are preprocessor statements.  You would benefit from an internet search on C preprocessor tutorial.  TI documentation does not cover basics of the C language such as the preprocessor.  

    A #define statement creates a symbol that is substituted wherever it appears in the source.  In your case, BIT0 is defined to be (0x0001) in the header file <msp430g2231.h>.  Thus, if you write something like ...

    int var = LED_0;

    Preprocessing turns that into ...

    int var = (0x0001);

    Thanks and regards,

    -George

  • Ok. Got it. I want to narrow my question a little bit.

    Where can I find more info about those defenitions? I mean is there more shortcuts like "BIT" for msp430?

    Of cause there is a direct way , I mean to open a header file and read the code, but the problem  is to understand the code ( for now this not my strong side) .

    Is there some guides about  these shortcuts or something like this ?

    Again,  thanks for your answer.

  • The header file msp430g2231.h (and all the rest like it) is not provided by the compiler team.  Thus, we are not in a position to directly help you.  I looked through the MSP430 wiki pages, and couldn't find any documentation on that file.  I did find this getting started workshop that is probably helpful.

    I suggest you start a new thread in the MSP430 forum.  Or, if you prefer, I can move this thread into that forum.

    Thanks and regards,

    -George

  • Yes, please move it if you think that will bring some more useful answers.

    Thanks George for your help.

    .

  • If you look in the header (.h) file for your device, you will see all the pre-processor macros.

    These are merely substitutions that are performed prior to compilation by the pre-processor (which is run during the compile, but before the C instructions are converted to machine code).

    Hopefully that plus the other material you were referred to will help you understand.

  • Tnx Brian, 

    As I can can see there is no excuses and I'll need to open those header files and understand whats going on there.

  • Ivan Evstegneev said:
    My question is about these code "shortcuts"

    In general, the C/C++ compilation process uses a so-called preprocessor. This preprocessor interprets source lines that begin with a '#' as preprocessor commands. One of these commands is the #define directive. It tells the prepreprocessor to 1) memorize the 'token' behind this directive as a defined token (a token is a chain of characters or digits, surrounded by whitespace or other delimiters). and 2) memorize the text behind the token, until end of line, as its value.

    After reading a define, it will replace any further occurrence of the token it encounters in the further processing of the source file with the value of the token.

    Almost like a 'search and replace' instruction with 'whole words only' modifier set.

    This replace is recursive, so a define will affect the source code of the next define.
    So

    #define BIT0 0x01
    #define LED_0 BIT0
    int i = LED_0;

    will first turn "#define LED_0 BIT0" into "#define LED_0 0x01" and then "int i = LED_0" into "int i = 0x01;"

    The compiler will see only the final version where the #define lines and all occurrences of BIT0 and LED_0 were replaced.

    The MSP430 header files define BITx not really as a shortcut (since 0x01 is as good and as fast to type as BIT0) but rather because using them makes the code more readable.

    P1DIR = BIT4|BIT6; is more readabel than P1DIR = 0x50; when the bits have independent meanings.

    Th eheader files also define a few more, like "LPM4_bits" for the bit combination that enters low power mode 4.

    And al bits or bitfields that appear in any hardware register are defined this way.

    UCA0CTL1 |= UCSWRST; sets the UCSWRST bit in the UCA0CTL1 register. No need to know that UCSWRST is actually bit 0.

    In your project, you can define any number of those tokens yoursels. Besides being 'telling', it has the big advantage that you only have to change one location in your code, the #define, to change all occurrences in your code. If you use direct numeric values, you have to find all occurrences, alter them and also be sure that you change only the right ones, as the value '1' can be used for many purposes, wile 'LED_0' is only used when it really refers to LED #0.

    More information can be found here:

    http://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Compiler/Preprocessor

    Unfortunately, the excellent preprocessor description from the RHEL 4 manual is gone form the RedHat website.

**Attention** This is a public forum