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.

Basic Syntax for programming MSP430 - beginner questions

Other Parts Discussed in Thread: MSP430F6638

This may seem like a simple or obvious answer; partly because it is and partly because I've looked everywhere and can't find what I'm looking for. I've yet to find something explaining what exactly some of these commands do and how they work. I'll list a few examples below:


P6DIR |= (BIT0) ;

So I know this makes pin 6.0 an output pin. But why? If I am reading correctly this statement can be written as P6DIR = P6DIR | BIT0 so you are ORing the register and the LSB together? How does this make it an output?

P6OUT|= (BIT0) ; // P6.0 High

This sets P6.0 to High, but how?

P6OUT&=~(BIT0); // P6.0 Low

This sets P6.0 to low, how and why?

 

I mean is there something in stone that explains why these operators work the way they do with these bits/registers?

Also the statement:

LCDCTL = (LCDSG0_3|LCD4MUX|LCDSON|LCDON);

I understand that the left hand operand is the LCD control register that consists of 8 bits. The selection of those bits apply to different functions(2/4mux, LCDon/off etc) and that the righthand identifiers correspond to the ones in the msp430xw42x.h file but I'm wondering if this was my first time looking at this stuff(I'm very new to this) how would I know what this statement is doing? I know what it does, but I am still unsure as to how it works and why it works the way it does.

 

Another example:

P5SEL = BIT4 | BIT3 | BIT2

I know this is setting bits 2,3,4 in the P5SEL register to 1 in order to pick it's secondary function of com0-com2 and not general i/o but why does this statement accomplish this.

 

Forgive me if my questions make no sense; I tried to be as clear as possible. I guess you could say I'm looking for a beginner's guide to programming the MSP430/coding techniques? Any questions please ask me.

Thanks,

Brock

  • Brock,

    I think this is a fairly common questions for beginners :)

    Checkout the section on "Bitwise Operators" in this Wikipedia link:

    http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

    To help you get started; the |= operator does a bitwise OR and the &= ~ does a bit clear.

    P5SEL = BIT4 | BIT3 | BIT2; is the same as

    P5SEL |= BIT4 + BIT3 + BIT2;

    where BITx  represents a bit position 0 through 7 in the PSEL register.

    This way the code is easier to understand instead of

    P5SEL |= 0x0E; which does the same thing.

    Here is another example:

    LCDCTL |= LCD4MUX;

    By ORing the register with this bit you are setting the bit to 1.

    LCDCTL &= ~LCD4MUX;

    By ANDing the register with the bit complement you are clearing the bit.

     

    Regards,

    Priya

  • Priya Thanigai said:

    Brock,

    I think this is a fairly common questions for beginners :)

    Checkout the section on "Bitwise Operators" in this Wikipedia link:

    http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

    To help you get started; the |= operator does a bitwise OR and the &= ~ does a bit clear.

    P5SEL = BIT4 | BIT3 | BIT2; is the same as

    P5SEL |= BIT4 + BIT3 + BIT2;

    where BITx  represents a bit position 0 through 7 in the PSEL register.

    This way the code is easier to understand instead of

    P5SEL |= 0x0E; which does the same thing.

    Here is another example:

    LCDCTL |= LCD4MUX;

    By ORing the register with this bit you are setting the bit to 1.

    LCDCTL &= ~LCD4MUX;

    By ANDing the register with the bit complement you are clearing the bit.

     

    Regards,

    Priya

    Priya I understand the operators but what I'm trying to ask is for example what you posted :

    LCDCTL |= LCD4MUX;

    By ORing the register with this bit you are setting the bit to 1.

    To get the result of 1 the bit would have to be set to 1 right? How do you know the bit is 1 here? Is that assumed?

  • The bit definition of LCD4MUX and all other such things are defined in the header file for the device you are using.

  • OK, I understand your question better now...

    YEs, these bit definitions and the values they represent are defined in msp430xxxx.h

    See an example below:

    [Extract from msp430f6638.h]

    The various bits for the LCDBCTL0 are defined as:

    // LCDBCTL0
    #define LCDON               (0x0001)  /* LCD_B LCD On */
    #define LCDSON              (0x0004)  /* LCD_B LCD Segments On */
    #define LCDMX0              (0x0008)  /* LCD_B Mux Rate Bit: 0 */
    #define LCDMX1              (0x0010)  /* LCD_B Mux Rate Bit: 1 */

    The easiest way to decipher code is to look for the functionality of the bits in the User's Guide and  then compare it to the # defines in the header file.

    Sometimes these #defines are used to represent a more complex setting/ functionality such as:

    #define WDT_ARST_1000       (WDTPW+WDTCNTCL+WDTSSEL0+WDTIS2)                         /* 1000ms  " */

    This definition is used to set the timer to run off ACLK and have an interval of 1000ms.

    Regards,

    Priya

  • Perhaps some even more basic informations are necessary, to fully answer the original question.

    Unlike the 80x86 processors (and some others), the MSP does not have a separate I/O port. All peripherals are occupying an address (or more) in the common address space. There you find not only RAM and FLASH, but also configuration registers for all the hardware the MSP has built-in.

    For convenience, the memory locations of these registers are mapped to 'variables' of the proper type (unsigned char/unsigned int) together with the information that they are placed at a fixed address and not in the common data segment.

    These mappings are in the variuos include files which are automatically loaded and compiled when you include the proper io.h or msp430xxxx.h header files (depends on the compiler) into your source code.

    Furthermore, also the meanings of the bits or bit combinations withing these registers are defined with meaningful names (such as LCDON). The names used usually correspond with the descriptions of the MSP hardware inthe MSP family guides and device manuals.

    Writing to such a register can be considered as writing an 8-bit/16 bit  value into a latch with 8/16 digital output lines controlling some built-in hardware circuitry.

    in case of the port 6, writing a value to P6OUT results in 8 digital lines set to 0 or 1 respectively, and these lines enable or disable the output buffers of the port pins. Similarly, writing to P5SEL, you 'program' 8 digital control lines which control a switch that will map either the port6.x output buffer (if 0) or some peripherals input or output signal (if 1) to the corresponding processor pin.

    So P6OUT is an alias to a memory address which is connected to the P6 output control hardware rather than just a memory cell.  From a programmers view, P6OUT and his brothers and sisters can be handled like just a normal global C variable. But any writing (and sometime also reading) access results in a direct reaction of some hardware module that is lurking on the MSPs address/data bus, waiting for someone accessing a certain memory address.

**Attention** This is a public forum