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.

Can anyone be able explain the following guidelines/commands? of lab_02a_css

Other Parts Discussed in Thread: MSP430F5529, ENERGIA

#include <msp430.h>

int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
P1DIR |= 0x01; // Set P1.0 to output direction

for(;;) {
volatile unsigned int i; // volatile to prevent optimization

P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR

i = 10000; // SW Delay
do i--;
while(i != 0);
}

return 0;
}

  • Adoni,

    what is the question about it? Your program simply blinks the LED on P1.0 (I assume it is from the MSP-EXP430G2 LaunchPad). The comments explain everything. The not commented part counts down the variable i until it is 0, then the program jumps back to the start, changes the state of P1.0 (high if it was low and vice versa) and then counts down again. The counting down is just a software delay. Otherwise the LED would flash too fast to see it - it would then look like it was only glowing.

    Dennis

  • 1. WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
    "Where is this line can be found". Im doing the MSP 430 F5529 Workshop

    2. P1DIR |= 0x01; // Set P1.0 to output direction
    "Can you explain what is happening in this statement Like how"

    3. for(;;) {
    "What is this function from"

    4. P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR
    "Can you explain what is happening in this statement Like how"

    5. i = 10000; // SW Delay
    do i--;
    "This syntax also not familiar"

    6. while(i != 0);
    "This also i dont understand"

    I liked driverlib.h more as compared to msp430f5529.h

    Can you answer it?
  • Adoni,

    1) Have a look into the user's guide, chapter 16 Watchdog Timer, there you have the register of the watchdog:

    The code line

    WDTCTL = WDTPW | WDTHOLD;

    sets the WDTPW bits (15...8) in combination with the WDTHOLD bit (7) in the WDTCTL register. It stops the watchdog.

    2) Have a look in the users' guide, chapter 12 Digital I/O Module, there you will find all about the pins of the microcontroller in digital input/output mode:

    The code line

    P1DIR |= 0x01; // Set P1.0 to output direction

    sets, as the comment already says, bit 0 in the P1DIR register which will set P1.0 to output direction.

    3) This statement

    for( ; ; )
    {
    
    }

    is an infinite loop, you could also write

    while( 1 )
    {
    
    }

    This is your main program, everything that is written into these brackets is executed over and over again. If it is empty, then the microcontroller simply does nothing (except from checking if the loop shall be executed one more time).

    4) Have a look at  2) - it is the same chapter, but now the output register:

    The code line

    P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR

    toggles bit 0 in the P1OUT register. This means that if it was set, it is now reset and vice versa. It sets the output high or low, each time this line is executed.

    5) This is simple counting down - 6) belongs to it, too

    i = 10000; // SW Delay
    
    do
    i--;
    while( i != 0);

    The variable i is set to 10000 and then the do-while-loop is executed as long as i is greater than 0, it works like this

    i = 10000; // SW Delay
    
    do
    i--;            // i is now 9999
    while( i != 0); // is i > 0? YES, so
    i--;            // i is now 9998
    while( i != 0); // is i > 0? YES, so
    i--;            // i is now 9997
    while( i != 0); // is i > 0? YES, so
    ...
    i--;            // i is now 0
    while( i != 0); // is i > 0? NO, end

    Adoni, I think you should start with some reading about C first - I can recommend this book.

    Dennis

  • Adoni Aamir said:
    I liked driverlib.h more as compared to msp430f5529.h

    That is why TI provides different programming styles (or layers) when coding the MSP processors.

    1. Energia  (http://energia.nu)
    2. DriverLibrary 
    3. Register

    You can choose to program in any one of these styles - or all of them, as they are compatible with each other. Energia is the easiest style to program with - for example, it automatically handles things like turning off the Watchdog timer for you.

    DriverLibrary (as you alluded to with "driverlib.h") is next on the list. C code written with the Driver Library is easy-to-read and maintain, while giving you access to almost all of the features found in the MSP and it's built-in peripeherals.

    Writing C code while directly accessing the Registers is more work than the other two styles. We started with it in the CCS Chapter of the workshop because this code was one of the default examples that is provided for each of the devices within CCS. It also gives you a chance to see, using a very simple program, how the register syntax works. For those MSP experts who know and understand the processor, this C code isn't too difficult to figure out - that said, I much prefer to use the Driver Library as it's easier to read and maintain when I come back to it days or weeks later.

    In your example, "driverlib.h" actually depends upon "msp430f5529.h" (register layer definitions). In this way, the two styles can be used together in the same C program, should you ever need to do so. 

    Hope this helps,
    Scott

**Attention** This is a public forum