TI MSP-EXP430G2 – Hello world

Hi folks,

as I promised to myself, I’ve started playng with MCUs. I’d like to get started with Arduino board but, the big deal with TI Stellaris Launchpad was to big. Because of I’m still waiting for the board (maybe at home for late november), I decided to buy another interesting board: the Texas Instruments MSP-EXP430G2. If you like you can find tons of info about this board over the net.

In this post I’d like to talk you about my very first experience with the MSP.

It all started from the TI MSP430 wiki page (http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_(MSP-EXP430G2)?DCMP=launchpad&HQS=Other+OT+launchpadwiki)

Once I’ve installed my CCS copy, I’ve copyed the source from the first example in the TI 430 WIKI page:

Blink your first led

follow the original code (I suggest to copy and paste the source code elswere to have a better experience):

[codesyntax lang=”c” container=”pre” title=”Blink your LED” lines=”normal” lines_start=”1″]

#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 |= 0x01;                            // P1DIR is a register that configures the direction (DIR) of a port pin as an output or an input. 

                                            // To set a specific pin as output or input, we write a '1' or '0' on the appropriate bit of the register.

                                            // P1DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>

                                            // Since we want to blink the on-board red LED, we want to set the direction of Port 1, Pin 0 (P1.0) as an output

                                            // We do that by writing a 1 on the PIN0 bit of the P1DIR register
                                            // P1DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>
                                            // P1DIR = 0000 0001
                                            // P1DIR = 0x01     <-- this is the hexadecimal conversion of 0000 0001

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

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

                                            // P1OUT is another register which holds the status of the LED. 
                                            // '1' specifies that it's ON or HIGH, while '0' specifies that it's OFF or LOW
                                            // Since our LED is tied to P1.0, we will toggle the 0 bit of the P1OUT register

    for(i=0; i< 20000; i++);                // Delay between LED toggles. This for-loop will run until the condition is met. 
                                            //In this case, it will loop until the variable i increments to 20000.
  }
}

[/codesyntax]

Now, this code let the board makes blink one led, the red one,

 

The first change suggested is to increase or decrease the toogle time. You can easily find this information at row 32. The less you insert, the fast the led wil blink.

Now, the first interesting change for me was trying to let blink the other led, the green one.

Dealing with the source code, this time, has been really puzzling. After a little search I’ve found that the green led is identified with the address “0x40”

This way, changing every 0x01 with 0x40 will let blink your green led.

This is also my first experience in programming and C programming so I’ll try to explain all doubts I’ve encountered during my trip.

Duiring a lab with a Raspberry PI device, using Java language, I noticed that to define GPIO actors and other stuff was really easy. We can achieve the same results merging code from other sources. From TI WIKI I’ve found this new source code:

“Blink your led using timer”

[codesyntax lang=”c” lines=”normal” container=”pre” title=”Led with timer”]

//***************************************************************************************
// MSP430 Timer Blink LED Demo - Timer A Software Toggle P1.0 & P1.6
//
// Description; Toggle P1.0 and P1.6 by xor'ing them inside of a software loop. 
// Since the clock is running at 1Mhz, an overflow counter will count to 8 and then toggle
// the LED. This way the LED toggles every 0.5s. 
// ACLK = n/a, MCLK = SMCLK = default DCO 
//
// MSP430G2xx
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | P1.6|-->LED
// | P1.0|-->LED
//
// Aldo Briano
// Texas Instruments, Inc
// June 2010
// Built with Code Composer Studio v4
//***************************************************************************************
#include <msp430g2231.h>

#define LED_0 BIT0 
#define LED_1 BIT6
#define LED_OUT P1OUT
#define LED_DIR P1DIR

unsigned int timerCount = 0;
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
LED_DIR |= (LED_0 + LED_1); // Set P1.0 and P1.6 to output direction
LED_OUT &= ~(LED_0 + LED_1); // Set the LEDs off

CCTL0 = CCIE;
TACTL = TASSEL_2 + MC_2; // Set the timer A to SMCLCK, Continuous
// Clear the timer and enable timer interrupt

__enable_interrupt();

__bis_SR_register(LPM0 + GIE); // LPM0 with interrupts enabled

} 

// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
timerCount = (timerCount + 1) % 8;
if(timerCount ==0)
P1OUT ^= (LED_0 + LED_1);
}

[/codesyntax]

 

Now, as you can see, LEDs are defined at the very beginning of the code. LEDs and directions.

This source make happen exactly the same thing but it use another method and…this is not what I want to describe now (I’m not able to. 😀 ).

So, my wish is to merge the two source code using the first method to make the led blink but I want to define them at the very beginning.

To make things a little more complicated (for me, of course), I’ve tried to write a code where both LEDs blink alternately or one LED blinking and the ore always on.

The following code is the result:

[codesyntax lang=”c” lines=”normal” container=”pre” title=”Final CODE”]

//***************************************************************************************
// MSP430 Timer Blink LED Demo - Timer A Software Toggle P1.0 & P1.6
//
// Description; Toggle P1.0 and P1.6 by xor'ing them inside of a software loop.
// Since the clock is running at 1Mhz, an overflow counter will count to 8 and then toggle
// the LED. This way the LED toggles every 0.5s.
// ACLK = n/a, MCLK = SMCLK = default DCO
//
// MSP430G2xx
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | P1.6|-->LED
// | P1.0|-->LED
//
// Aldo Briano
// Texas Instruments, Inc
// June 2010
// Built with Code Composer Studio v4
//***************************************************************************************
#include <msp430g2231.h>

#define LED_0 BIT0
#define LED_1 BIT6
#define LED_OUT P1OUT
#define LED_DIR P1DIR

 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

LED_DIR |= (LED_0 + LED_1);                            // P1DIR is a register that configures the direction (DIR) of a port pin as an output or an input.

                                          // To set a specific pin as output or input, we write a '1' or '0' on the appropriate bit of the register.

                                          // P1DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>

                                          // Since we want to blink the on-board red LED, we want to set the direction of Port 1, Pin 0 (P1.0) as an output

                                          // We do that by writing a 1 on the PIN0 bit of the P1DIR register
                                          // P1DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>
                                          // P1DIR = 0000 0001
                                          // P1DIR = 0x01     <-- this is the hexadecimal conversion of 0000 0001

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

//  LED_OUT ^= (LED_0 + LED_1);                          // Toggle P1.0 using exclusive-OR operation (^=)
//  LED_OUT ^= LED_0;                          // Toggle P1.0 using exclusive-OR operation (^=)
  LED_OUT ^= LED_1;                          // Toggle P1.0 using exclusive-OR operation (^=)

                                          // P1OUT is another register which holds the status of the LED.
                                          // '1' specifies that it's ON or HIGH, while '0' specifies that it's OFF or LOW
                                          // Since our LED is tied to P1.0, we will toggle the 0 bit of the P1OUT register

  for(i=0; i< 10000; i++);                // Delay between LED toggles. This for-loop will run until the condition is met.
                                          //In this case, it will loop until the variable i increments to 20000.
}
}

[/codesyntax]

 

This way the red led will remain always on while the green one will blink. You can swap between the last commented rows to swap the behavior.

Finally, if you uncomment row number 51 (commenting row 52 and 53), you’ll be able to lock at both LEDs blink alternately.

That’s all for now and remember: I’m a newbie of programming and I’m doing really simple stuff (for the rest of the world).

Be patinet looking to my post and write your comments, if you like.

Thank you very much.

Leave a Reply

Your email address will not be published. Required fields are marked *