MSP430
Getting Started

Modified

Ray Wisman

Resources

LaunchPad store - https://estore.ti.com/MSP-EXP430G2-MSP430-LaunchPad-Value-Line-Development-kit-P2031.aspx

LaunchPad Resources - http://processors.wiki.ti.com/index.php/MSP430_LaunchPad_(MSP-EXP430G2)

Wiki description - http://en.wikipedia.org/wiki/TI_MSP430

Programming the MSP430 in C - http://www.swarthmore.edu/NatSci/echeeve1/Class/e72/dl/E72IntroToMSP430_C.pdf

Calling Assembler from C - http://www.ti.com/lit/an/slaa140/slaa140.pdf

Texas Instruments MSP430x2xx Family User Guide - http://focus.ti.com/lit/ug/slau144e/slau144e.pdf

Texas Instruments IAR Embedded Workbench Kickstart - http://focus.ti.com/docs/toolsw/folders/print/iar-kickstart.html

Texas Instruments Code Composer Studio - http://focus.ti.com/docs/toolsw/folders/print/ccstudio.html

MSP430 Assembly Language Tools v 3.3 User's Guide  MSP430 - http://focus.ti.com/lit/ug/slau131e/slau131e.pdf

MSP430 C/C++ Programming - http://focus.ti.com/lit/ug/slau132e/slau132e.pdf

MSP430F20xx, G2x01, G2x11, G2x21, G2x31 Code Examples - http://www.ti.com/lit/zip/slac080

LaunchPad examples - http://dbindner.freeshell.org/msp430/

MSP430 IAR Assembly Language Getting Started - http://www.ece.uah.edu/~milenka/cpe323-09S/labs/lab2.s09.pdf

 

LaunchPad Development Board

LaunchPad has a 14-pin or 20-pin processor in a socket that can be programmed in C or Assembler.

The MSP430G2553 processor is an upgrade to and can execute all the MSP430G2231 programs we will encounter.

 

P1.0 - P1.7                         Input/Output to the MSP430G2231 or MSP430G2553 processor on the LaunchPad

P1.0    Red LED

P1.3    Pushbutton

P1.6    Green LED

 

Description (http://www.ti.com/product/msp430g2553)

The Texas Instruments MSP430 family of ultra-low-power microcontrollers consists of several devices featuring different sets of peripherals targeted for various applications. The architecture, combined with five low-power modes, is optimized to achieve extended battery life in portable measurement applications. The device features a powerful 16-bit RISC CPU, 16-bit registers, and constant generators that contribute to maximum code efficiency. The digitally controlled oscillator (DCO) allows wake-up from low-power modes to active mode in less than 1 s.

The MSP430G2x21/31 series is an ultra-low-power mixed signal microcontroller with a built-in 16-bit timer and ten I/O pins. The MSP430G2x31 family members have a 10-bit A/D converter and built-in communication capability using synchronous protocols (SPI or I2C).

Typical applications include low-cost sensor systems that capture analog signals, convert them to digital values, and then process the data for display or for transmission to a host system.

Main characteristics of a MSP430 microcontroller

Although there are variants in devices in the family, a MSP430 microcontroller can be characterized by:

Low power consumption:
       0.1 uA for RAM data retention;
       0.8 uA for real time clock mode operation;
       250 uA/MIPS at active operation.

Low operation voltage (from 1.8 V to 3.6 V).
      < 1 us clock start-up.
      < 50 nA port leakage.

Zero-power Brown-Out Reset (BOR).

On-chip analogue devices:
       10/12/16-bit Analogue-to-Digital Converter (ADC);
       12-bit dual Digital-to-Analogue Converter (DAC);
       Comparator-gated timers;
       Operational Amplifiers (OP Amps);
       Supply Voltage Supervisor (SVS).

16 bit RISC CPU:
       Instructions processing on either bits, bytes or words;
       Compact core design reduces power consumption and cost;
       Compiler efficient;
       27 core instructions;
       7 addressing modes;
       Extensive vectored-interrupt capability.

Flexibility:
       Up to 256 kB In-System Programmable (ISP) Flash;
       Up to 100 pin options;
       USART, I2C, Timers;
       LCD driver;
       Embedded emulation.

MSP430 functional diagram

MSP430 devices implement complete systems of:

  1. RISC CPU
  2. 128 bytes RAM
  3. 1-2kB Flash
  4. Analog-to-digital convertor
  5. 8 I/O ports
  6. Serial interface
Functional Diagram

Memory map

For all devices, each memory location is formed by 1 data byte.

The CPU is capable of addressing data values either as bytes (8 bits) or words (16 bits).

Words are always addressed at an even address, which contain the least significant byte, followed by the next odd address, which contains the most significant byte. For 8-bit operations, the data can be accessed from either odd or even addresses, but for 16-bit operations, the data values can only be accessed from even addresses.

Memory mapping of peripheral

Peripheral modules are memory-mapped, that is they appear as though part of the regular memory.

For example:

P1IN is mapped to memory address 0x20.

Reading memory address 0x20 reads the value of P1IN peripheral.

 

Interrupt vector table

The interrupt vector table is mapped at the very end of memory space (upper 16 words of Flash/ROM), in locations 0FFE0h through to 0FFFEh (see the device-specific datasheets).

The priority of the interrupt vector increases with the word address.

Central Processing Unit (MSP430 CPU)

The RISC type architecture of the CPU is based on a short instruction set (27 instructions), interconnected by a 3-stage instruction pipeline for instruction decoding.

The CPU has a 16-bit ALU, four dedicated registers and twelve working registers, which makes the MSP430 a high performance microcontroller suitable for low power applications. The addition of twelve working general purpose registers saves CPU cycles by allowing the storage of frequently used values and variables instead of using RAM.

The orthogonal instruction set allows the use of any addressing mode for any instruction, which makes programming clear and consistent, with few exceptions, increasing the compiler efficiency for high-level languages such as C.

Logical bitwise operations review

AND/OR/XOR/NOT

Definitions
And Or Xor Not
A B|A AND B 
0 0| 0
0 1| 0
1 0| 0 
1 1| 1
A B|A OR B 
0 0| 0
0 1| 1
1 0| 1 
1 1| 1
A B|A XOR B 
0 0| 0
0 1| 1
1 0| 1 
1 1| 0
A | Not A  
0 | 1
1 | 0


8-bits are organized as:     76543210
                                         01000000   = 0x40

AND  &=

Reset all but bit 6 to 0                 

P1OUT  = 0x71;    01110001
P1OUT &= 0x40;  & 01000000
P1OUT             01000000               

OR  |=

Set bit 6 to 1

P1OUT  = 0x01;    00000001
P1OUT |= 0x40;  | 01000000
P1OUT             01000001              

XOR  ^=

Toggle bit 6

P1OUT  = 0x71;    01110001
P1OUT ^= 0x40;  ^ 01000000
P1OUT             00110001               

NOT  ~

Reset only bit 6 to 0

P1OUT &= ~0x40;    or equivalently    P1OUT &= 0xBF;

P1OUT  =  0x71;    01110001
P1OUT &= ~0x40;  & 10111111
P1OUT              00110001               

Question 1 - What result is P1OUT

P1OUT = 0xC7 = 11000111

  1. P1OUT &= 0x40;
     
  2. P1OUT |= 0x08;
     
  3. P1OUT ^= 0x40;
     
  4. P1OUT &= ~0x40;
Definitions
And Or Xor Not
A B|A AND B 
0 0| 0
0 1| 0
1 0| 0 
1 1| 1
A B|A OR B 
0 0| 0
0 1| 1
1 0| 1 
1 1| 1
A B|A XOR B 
0 0| 0
0 1| 1
1 0| 1 
1 1| 0
A | Not A  
0 | 1
1 | 0

 

Input/Output Ports - Connects the CPU to external world.

Port P1      8-bit input/output port that the CPU can read or write.

P1.0 - P1.7                             Input/Output to the MSP430G2553 processor on the LaunchPad

P1.0    Red LED

P1.3    Pushbutton

P1.6    Green LED

 

Relevant ports in example:

WDTCTL

Controls the watchdog timer, used to execute a system reset when time expires, useful when CPU hangs in infinite loop, etc.

WDTPW + WDTHOLD turns it off to prevent interrupting the program execution.

 

P1DIR

Controls the direction of each of the 8 data bits of port P1. Must be initialized before input or output on data port.

Bit 6 (0x40) is the green LED.

ORing with 1 sets the direction as output.

P1DIR |= 0x40; 

               76543210
      0x40     01000000
   |= P1DIR    x1xxxxxx

P1OUT

Outputs 8 bits of data to port P1.

Bit 6 (0x40) is the green LED, P1.6.

P1OUT |= 0x40;     Green LED (P1.6) On.

            76543210
   0x40     01000000
|= P1OUT    x1xxxxxx

P1OUT &= ~0x40;   Green LED (P1.6) Off.

            76543210
   ~0x40    10111111
&= P1OUT    x0xxxxxx

 

Debugging video

1. Turn green LED On and Off

#include <msp430g2553.h>

void main(void) {

   WDTCTL = WDTPW + WDTHOLD;       // Stop watchdog timer

   P1DIR |= 0x40;                  // Set P1.6 (Green LED) to output

   while( 1 ) {

       P1OUT |= 0x40;              // Green LED On

       P1OUT &= ~0x40;             // Green LED Off
   }
}

Question 2

a. What is the result of removing:    

while( 1 )

b. What is the effect to the individual bits of:

P1OUT |= 0x40;

c. What is the effect to the individual bits of:

P1OUT &= ~0x40;

d. What changes to the above program are needed to turn the red LED On and Off?

The red LED is connected to P1.0, bit 1.

 

Relevant ports in example:

WDTCTL

Controls the watchdog timer, used to execute a system reset when time expires. WDTPW + WDTHOLD turns it off.

P1DIR

Controls the direction of each of the 8 data bits of port P1. Must be initialized before input or output on data port.

Bit 6 (0x40) is the green LED. ORing with 1 sets the direction as output.

P1DIR |= 0x40;

P1OUT

Outputs 8 bits of data to port P1.

Bit 6 (0x40) is the green LED.

0 output turns LED off.

XOR toggles the output from 1 to 0 or 0 to 1.

P1OUT ^= 0x40;           Green LED (P1.6) toggle.

            76543210
    0x40    01000000
^= P1OUT    xTxxxxxx

 

2. Toggle green LED On and Off

#include <msp430g2553.h>

unsigned int i = 0;

void main(void) {

     WDTCTL = WDTPW + WDTHOLD;        // Stop watchdog timer.

     P1DIR |= 0x40;                   // P1DIR configures direction port pin (DIR) output or input.
                                     
// 1 is output, 0 is input. 0x40 is Green LED
          while(1) {

        P1OUT ^= 0x40;                // Toggle P1.6
                                      // P1OUT register holds the status of the LED.
                                                                     
// 1 is ON, 0 is OFF. 0x40 is Green LED

               for(i=0; i < 50000; i++);     // Delay between Green LED toggles.
    
}
}

Question 3

a. What is the effect to the individual bits of:

P1OUT ^= 0x40;

b. What changes to the above program are needed to toggle the red and green LEDs simultaneously?

The red LED is connected to P1.0, bit 1.

 c. What changes to the above program are needed to toggle the green LED On or OFF approximately every 2 seconds?

       The LED is ON or OFF for about 1/2 second due to: for(i=0; i < 50000; i++);

char : signed 8-bit integer

unsigned char : unsigned 8-bit integer

int : signed 16-bit integer

unsigned int  : unsigned 16-bit integer

long : signed 32-bit integer

unsigned long  : unsigned 32-bit integer

d. Assuming:

i) the CPU clock is 1 Mhz (1,000,000 cycles per second)

ii) one iteration of the for-loop requires 10 clock cycles

How much time is required to execute:

for(i=0; i < 50000; i++);