MSP430-assembly language part-1-DIGITAL IO

Photo of author

By Jackson Taylor

Home » Hardware » MSP430-assembly language part-1-DIGITAL IO

As an Amazon Associate we earn from qualifying purchases.

This tutorial shows how to control digital I/O pins using MSP430 assembly language. You will turn on LEDs, blink them, toggle them, call a subroutine, and read a push button. All the examples use the Naken430asm assembler and an MSP430G2231 on the LaunchPad board.

Tools and hardware used

Here I am using the Naken430asm assembler and the MSP430G2231 controller.

MSP430 LaunchPad board with MSP430G2231 chip and onboard LEDs on P1.0 and P1.6

On the LaunchPad, the two LEDs are connected to P1.0 and P1.6. Each pin is one bit of the port 1 registers, so P1.0 is bit 0 (mask 0x01) and P1.6 is bit 6 (mask 0x40). Both together give 0x41, which you will see in every example below.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

The registers you need for digital I/O

  • P1DIR: sets the direction of each pin. A 1 makes the pin an output, a 0 makes it an input.
  • P1OUT: sets the output level of each pin configured as an output. A 1 drives the pin high, a 0 drives it low.
  • P1IN: read-only register that shows the current level on each pin.
  • WDTCTL: the watchdog timer control register. Writing WDTPW|WDTHOLD stops the watchdog so it does not reset the chip while your program runs.

Each program below follows the same layout. It includes the device definitions, places the code at 0xF800 (the start of flash on the G2231), stops the watchdog, configures the pins, and finally stores the start address at the reset vector 0xFFFE so the chip knows where to begin.

Example 1: turn on two LEDs

This is the simplest program. It sets P1.0 and P1.6 as outputs, drives them high, and then loops forever.

.include "msp430g2x31.inc"
 org 0xf800    ;SETS THE ADDRESS OF program to be stored
    mov.w #(WDTPW|WDTHOLD), &WDTCTL ;DISABLES WATCH DOG TIMER
 mov.b #0x41, &P1DIR ;selects the direction of the  I/O pins
 mov.b #0x41, &P1OUT ;sets the output on the corresponding I/O pin
loop:
 jmp loop            ;runs the program into infinity loop
 org 0xfffe  ;SETS THE ADDRESS OF Reset vector to be stored
 dw 0xf800

The final infinite loop matters. Without it, the processor would keep executing whatever bytes follow your code.

See also
MSP430-Digital Input

Example 2: blink an LED

To blink, the program needs a delay and a way to flip the pin state. The delay here is a simple software loop that counts register R4 down from 0xCD40 to zero. The xor.b instruction then flips both LED bits in P1OUT.

.include "msp430g2x31.inc"
 org 0xf800                       
 mov.w #(WDTPW|WDTHOLD), &WDTCTL
      CLR P1OUT 
 mov.b #0x41, &P1DIR  
 L1:   MOV.W #0xcd40,R4
DELAY:
 DEC R4
 JNZ DELAY
 xor.b #0x41, &P1OUT  
 JMP L1
 org 0xfffe
 dw 0xf800

The delay length depends on the clock speed. At the default clock of around 1 MHz, this loop gives a pause of roughly a sixth of a second, so the LEDs blink at a visible rate. Change the 0xCD40 value to make it faster or slower.

Example 3: toggle two LEDs alternately

If you start with only one LED on and then XOR both bits each time, the LEDs swap states on every pass. One is always on while the other is off.

.include "msp430g2x31.inc"
 org 0xf800                            
 mov.w #(WDTPW|WDTHOLD), &WDTCTL
 CLR  P1OUT 
 mov.b #0x41, &P1DIR
 mov.b #0x40, &P1OUT                    
L1:   MOV.W #0xCD40,R4
DELAY:
 DEC R4
 JNZ DELAY
 xor.b #0x41, &P1OUT  
 JMP L1
 org 0xfffe
 dw 0xf800

The only change from the blink example is the starting value written to P1OUT (0x40 instead of 0).

Example 4: toggle the LEDs with a function call

Assembly code gets hard to follow quickly, so it helps to split it into subroutines. Calling one uses the stack, so you must set the stack pointer first. The G2231 has 128 bytes of RAM ending at 0x027F, so the stack pointer is set to 0x280 and grows downward from there.

.include "msp430g2x31.inc"
 org 0xf800               
 mov.w #(WDTPW|WDTHOLD), &WDTCTL
 mov.w #0x280, SP
 CLR  P1OUT 
 mov.b #0x41, &P1DIR
 mov.b #0x40, &P1OUT 
L2:   call #L1  
 JMP L2               
L1:   MOV.W #0xCD40,R4
DELAY:
 DEC R4
 JNZ DELAY
 xor.b #0x41, &P1OUT
 ret 
 org 0xfffe
 dw 0xf800

The main loop at L2 calls the routine at L1, which waits and toggles the LEDs, then returns with ret. Forgetting to initialise SP is a common cause of a program that crashes as soon as it hits a call.

Example 5: read a digital input on P1.3

P1.3 is the pin wired to the push button on the LaunchPad. In this example the pin is read as an input, and the LEDs turn on when it goes high. A pull-down resistor is required so the pin reads a clean 0 when the button is not pressed. If you want to know more about pull-up and pull-down resistors, including the internal ones, see the follow-up post.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
.include "msp430g2x31.inc"
 org 0xf800                   
 mov.w #(WDTPW|WDTHOLD), &WDTCTL
 CLR.B P1OUT
 CLR R5  
 mov.b #0x41, &P1DIR 
L1:   MOV.b &P1IN,  r5
 AND.b #0x8,r5
 tst r5
 JZ L1
 mov.b #0x41, &P1OUT 
L3:   jmp L3
 org 0xfffe
 dw 0xf800

The program copies P1IN into R5 and masks all bits except bit 3 (0x08) with AND.b. If the result is zero, the button is not pressed and the jump goes back to L1. Once the bit is set, the program writes 0x41 to P1OUT to light both LEDs and stops in the L3 loop.

See also
AUTOMATIC VOLTAGE STABILIZER USING MSP430

Please see more @ http://jimmyjosep.blogspot.in/2013/12/msp430-assembly-language-part-1.html

Tips for working with these examples

  • Use mov.b for the port registers, because they are 8 bits wide.
  • Use xor to flip bits, bis to set them, and bic to clear them without touching other pins.
  • Always stop the watchdog at the start, or the chip will keep resetting.
  • Keep the reset vector at 0xFFFE pointing to your first instruction.

What to learn next

Software delay loops waste processor time, so the next step is to use a hardware timer instead. After that, you can move on to arithmetic operations in assembly. If you prefer C, the posts on MSP430 digital output and MSP430 digital input cover the same ideas.

FAQ

Why do I need to disable the watchdog timer?

The watchdog starts running at power-up and resets the chip if it is not serviced in time. Writing WDTPW|WDTHOLD to WDTCTL stops it, so simple programs can run without being restarted.

What does 0x41 mean in these programs?

It is a bit mask. In binary it is 01000001, which selects bit 0 (P1.0) and bit 6 (P1.6), the two LED pins on the LaunchPad.

Why does the toggle program need the stack pointer set?

The call instruction pushes the return address onto the stack, and ret pops it. If SP is not set to a valid RAM address, the return will not work correctly.

Can I use these examples on other MSP430 chips?

The ideas are the same, but the include file, flash start address and RAM size differ between devices. Check the data sheet and adjust the org address and stack pointer accordingly.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works