# Msp430 Launchpad Programming

I have code like this in IAR Embedded Workbench. At least Led0 should blink?

#include <io430.h>
#include<signal.h>

void main(void)
{
WDTCTL = WDTHOLD + WDTPW;
P1DIR = BIT6 + BIT4 + BIT3 + BIT7 + BIT0;
P1OUT |= BIT6 + BIT4 + BIT3 + BIT7+ BIT0;
}

-

That won't do anything. You need an infinite loop, a delay, and some way of toggling the output. Something like this should work:


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

for (;;)
{
volatile unsigned int i;

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

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

-
I have never before seen do without a {} code block do (i--). Interesting. GCC seems to allow dropping the () too –  Toby Jaffey Apr 30 '11 at 23:15
Joby: A single statement doesn't need braces round it. –  Leon Heller Apr 30 '11 at 23:27
You can't increase i to more than 65535, because it's 16-bit integer. (It's a good habit to use types like "uint16_t" instead of "unsigned int", so that overflows don't sneak up on you. Also, this causes the code to behave the same when compiled on your desktop PC, where it's easier to debug tricky algorithms.) –  markrages May 1 '11 at 3:51
"do (i--); while (i != 0);" is the same as "while (--i);", no? –  markrages May 1 '11 at 3:52
Make i an unsigned long and increase the value. Invest in a good book on C, such as The C Programming Language by Kernighan and Ritchie, read it, and you will be able to work this stuff out for yourself! –  Leon Heller May 1 '11 at 10:43
show 4 more comments
#include  "msp430.h"

// set names for bits to improve code readability
#define     LED0                  BIT0
#define     LED1                  BIT6
#define     LED_DIR               P1DIR
#define     LED_OUT               P1OUT

void main (void)
{
WDTCTL = WDTPW + WDTHOLD;      // halt the watchdog timer

LED_DIR |= LED0 + LED1;        // set both pins with our two LEDs as ouputs
LED_OUT &= ~(LED0 + LED1);     // make sure both LEDs are off to begin with

TACCTL0 = CCIE;                // enable timer interrupt
TACCR0 = 16384;                // set timer value to count

// Start timer A0, in UP mode. which counts from 0x0000 to the value
// in TACCR0, then generate an interrupt.
TACTL = TASSEL1 + MC_1;

// Enter low power mode one, with general interrupts enabled.
__bis_SR_register(LPM1_bits + GIE);
}

#pragma vector=TIMERA0_VECTOR    // timer A0 interrupt routine
__interrupt void timerA0_isr(void) {
// Flip the output state of both LEDs.
// After each interrupt, the state changes, which makes the LEDs blink.
LED_OUT ^= (LED0 + LED1);
}


You can change timer to make it blink at proper interval. Also if you initially set one led on and the other off, when the interrupt occurs they will blink alternately.

-
Maybe you can explain what you did here. Not everybody will understand your code at first reading (esp. without comments) –  stevenvh Jun 17 '11 at 10:38