Optimising C for the TI MSP430

Discussion in 'Programmer's Corner' started by Sparky49, Dec 16, 2012.

  1. Sparky49

    Thread Starter Active Member

    Jul 16, 2011
    Hi everyone,

    Just started with the TI Launchpad - I like it very much! Code Composer Studio is very user-friendly, with few windows in its wizard and an easy to understand layout.

    Anyway. I 've been working my way through the projects on their Launchpad wiki, and have managed to get the first few exercises working, however, I'd like to ask you guys if you think I could make the code smaller in any way.

    In this case, I'm supposed to make a red LED and a green LED switch on and off alternately. What do you guys think?

    I understand that I could've used the xor command '^=' to toggle them off and on, or I could've used or and and to turn the led on and off with '|=' and '&='.

    Is one way better than the other?

    Is there a better way to have accomplished the same task?

    Many thanks,

  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    The amount of code to do this is very minimal (except in the most time critical issues such as simultaneously setting bits) so that should not be of concern.

    What I do not like about using XOR to change bits is if they ever get corrupted they stay that way. Thus I prefer a positive approach to changing bits:

    Code ( (Unknown Language)):
    2.         #define GREEN_LED  0x40
    3.         #define RED_LED    0x01
    5.         ...
    7.         // toggle the green LED off and red LED on
    8.         P1OUT = ( P1OUT & ~(GREEN_LED | RED_LED) ) | GREEN_LED
    10.         ...
    12.         // toggle the green LED on and red LED off
    13.         P1OUT = ( P1OUT & ~(GREEN_LED | RED_LED) ) | RED_LED
    The " & ~(GREEN_LED | RED_LED)" punches a hole in P1OUT where the LEDs are (makes these bits zero), then the OR sets the red or green bit. Thus the bits are positively controlled no matter what their previous state.

    I see your controlling loop did it's deal in a clever way to avoid initialization, but it still assumes both LEDs were off to start with.

    Finally, while a quote of code doesn't keep white space (tabs & spaces). Code tags do, that's the # symbol above the message box.
    Sparky49 likes this.
  3. MrChips


    Oct 2, 2009
    You can combine

    P1DIR |= 0x40;

    P1DIR |= 0x01;


    P1DIR |= 0x41;
    Sparky49 likes this.
  4. MrChips


    Oct 2, 2009
    I have not used Code Composer Studio but you really need to examine the disassembler code and see how optimized is the code.

    I use the IAR Embedded Workbench and disasssembler output is straight forward.

    MSP430 has single bit operations in asm, BIS, BIC, so the question is to see if these are being used effectively.

    The IAR EW libraries have single bit referencing using unions and fields.

    For example:

    P1OUT_bit.P6 = 1; // to turn on GREEN LED

    P1OUT_bit.P6 = 0; // to turn off GREEN LED

    Check to see if Code Composer has the equivalent.
    Sparky49 likes this.
  5. vortmax


    Oct 10, 2012
    The "better" way to do this would be with a timer interrupt. Basically, you setup a timer the length of your delay cycle, then assign an interrupt to it. When the timer rolls over, the interrupt fires and the interrupt handler changes the state of the LEDs. Think of it as setting an alarm to remind you to flip a light switch every 5 minutes instead of watching the clock and counting every second to figure out when you need to do it.

    The benefits are numerous...first of all it allows you to put the mcu into a low power, sleep mode while it is waiting to do something. This can stretch battery life quite a bit. The interrupt also interrupts the main loop code when it runs, so in the case of the LEDs, they will blink at a fixed rate regardless of what is happening in the main loop. With your existing code, the number of cycles consumed in the loop could change, which would affect the static timing.

    Interrupts are probably one of the more confusing things to work with at first, but once you wrap your head around them, they make your code much better.

    A good demo for this is http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_LED_Timer
    Sparky49 likes this.
  6. MrChips


    Oct 2, 2009
    If you are ready to tackle interrupts I can show you how do it with IAR EW.
    I would have to try out Code Composer to see how they do it.
    Sparky49 likes this.