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?
Is one way better than the other?
Is there a better way to have accomplished the same task?
Many thanks,
Sparky
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 '&='.
#include<msp430g2553.h>
unsigned int i = 0; // Initialize variables. This will keep count of how many cycles between LED toggles
voidmain(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
}}
Is one way better than the other?
Is there a better way to have accomplished the same task?
Many thanks,
Sparky