Optimising C for the TI MSP430

Thread Starter

Sparky49

Joined Jul 16, 2011
833
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?


#include​
<msp430g2553.h>


unsigned int i = 0; // Initialize variables. This will keep count of how many cycles between LED toggles


 ​
void​
main(void)
{
WDTCTL = WDTPW + WDTHOLD;
// Stop watchdog timer. This line of code is needed at the beginning of most MSP430 projects.

// This line of code turns off the watchdog timer, which can reset the device after a certain period of time.


P1DIR |= 0x40;​
// P1DIR is a register that configures the direction (DIR) of a port pin as an output or an input.


 
// 0x40 is a green LED, 0x01 is a red LED


P1DIR |= 0x01;

 
 
for (;;) // This empty for-loop will cause the lines of code within to loop infinitely

{

 
 
P1OUT ^= 0x40;​
// toggle the green LED on


 
for(i=0; i< 30000; i++); // for 10000 cycles


P1OUT ^= 0x40;​
// toggle the green LED off


P1OUT ^= 0x01;​
// toggle the red LED on


for(i=0; i< 30000; i++); // for 30000 cycles


P1OUT ^= 0x01;​
// toggle the red LED off

}​
}
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,

Sparky
 

ErnieM

Joined Apr 24, 2011
8,377
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?
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:

Rich (BB code):
        #define GREEN_LED  0x40
        #define RED_LED    0x01

        ...

        // toggle the green LED off and red LED on
        P1OUT = ( P1OUT & ~(GREEN_LED | RED_LED) ) | GREEN_LED

        ...

        // toggle the green LED on and red LED off
        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.
 

MrChips

Joined Oct 2, 2009
30,712
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.
 

vortmax

Joined Oct 10, 2012
102
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
 

MrChips

Joined Oct 2, 2009
30,712
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.
 
Top