As an Amazon Associate we earn from qualifying purchases.
Digital output is the first thing most people do with a microcontroller: set a pin high or low and watch an LED respond. On the MSP430 this takes only a few instructions, which makes it a good way to learn how the chip’s registers work. This article walks through six assembly examples, from lighting two LEDs to blinking them with delay loops and subroutines.
The examples use the msp430g2x31.inc and msp430x2xx.inc header files and the two LEDs on pins P1.0 and P1.6, as found on the MSP430 LaunchPad. If you are new to the instruction set, read our MSP430 assembly language basics first. Once outputs make sense, the next step is digital input, where you read a button instead of driving an LED.
How MSP430 digital output works
Each pin of Port 1 is controlled by bits in a few registers. For output you need only two of them:
- P1DIR sets the direction of each pin. A bit set to 1 makes that pin an output; 0 makes it an input.
- P1OUT holds the value driven on the pin. A 1 gives a logic high and a 0 gives a logic low (when the pin is an output).
Each bit number matches a pin number. P1.0 is bit 0 (value 0x01) and P1.6 is bit 6 (value 0x40). Combined, the mask is 0x41, which is why that number appears in every example below. On the LaunchPad the LEDs are wired so that a high level turns them on.
What every program has in common
- Stopping the watchdog timer. The line
mov.w #(WDTPW|WDTHOLD),&WDTCTLwrites the watchdog password and the hold bit. Without it the watchdog resets the chip after a short time, and your program restarts over and over. - Setting the start address.
org 0xf800places the code at the start of flash on these small devices. - The reset vector.
org 0xfffefollowed bydw 0xf800tells the CPU where to begin after a reset.
Example 1: turn on two LEDs (P1.0 and P1.6)
This program sets both pins as outputs, drives them high, and then sits in an endless loop so the CPU does not run off into empty memory.
.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 ;Writes logic high value to P1.0
;& P1.6
loop:
jmp loop ;Starts the program into infinity
;loop
org 0xfffe ;SETS THE ADDRESS OF Reset vector
;to be stored
dw 0xf800
Blink an LED
To blink, the program needs to change the pin state and then wait long enough for your eye to notice. The loop below counts register R4 down from 0xCD40 as a delay, then flips both pins with xor.b. XOR with 0x41 inverts only bits 0 and 6 and leaves the other pins untouched. Because CLR P1OUT starts both LEDs off, they blink together.
.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. After reset the MSP430G2 devices run at roughly 1 MHz, and each pass of the DEC/JNZ loop takes a few clock cycles, so this delay is a fraction of a second. Change the 0xCD40 value to make the blink faster or slower.
Toggle two LEDs alternately
The only change here is the starting value of P1OUT. Writing 0x40 turns on P1.6 and leaves P1.0 off. Each XOR with 0x41 then swaps the two, so the LEDs take turns.
.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
Toggle LEDs using a function call
Here the delay and toggle code is moved into a subroutine that the main loop calls. call pushes the return address on the stack, so the stack pointer must be set first. The line mov.w #0x280, SP points it at the top of RAM on these devices, and the stack grows downward from there. ret at the end of the subroutine pops the address and returns to the caller.
.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
Long delay loop
A single 16-bit counter can only count so far. To get a longer delay, this version counts R4 down from 65000 and then does the same with R5, one after the other, before toggling the LEDs. Both pins start high, so the LEDs begin on and blink together.
.INCLUDE "msp430x2xx.inc"
ORG 0xf800
MOV #(WDTPW|WDTHOLD), &WDTCTL
MOV #0x280,SP
MOV.B #0x41, &P1DIR
MOV.B #0x41, &P1OUT
LOOP1:
MOV #65000,R4
MOV #65000,R5
CALL #LOOP2
JMP LOOP1
LOOP2:
DEC R4
TST R4
JNZ LOOP2
LOOP3:
DEC R5
TST R5
JNZ LOOP3
XOR.B #0x41,&P1OUT
RET
ORG 0xfffe
DW 0xF800
The TST instructions are not strictly needed, because DEC already updates the zero flag. They do no harm, but they add cycles to each pass, which makes the delay slightly longer.
LED blinking using the BIT instruction
The BIT instruction ANDs the source and destination and sets the flags, but it does not change either operand. That makes it useful for testing bits. In this example BIT #0xFFFE,R4 checks bits 1 to 15 of R4, so the loop ends when R4 is down to 0 or 1 rather than exactly 0.
.INCLUDE "msp430x2xx.inc"
ORG 0xf800
MOV #(WDTPW|WDTHOLD), &WDTCTL
MOV #0x280,SP
MOV.B #0x41, &P1DIR
MOV.B #0, &P1OUT
LOOP2:
MOV #65000,R4
CALL #LOOP1
JMP LOOP2
LOOP1:
DEC R4
BIT #0xFFFE,R4 ;SIMPLY "AND"S BETWEEN SRC & DST.
;RESULT WILL NOT AFFECT SRC & DST.
;USED FOR BIT TESTING
JNZ LOOP1
XOR.B #0x41,&P1OUT
RET
ORG 0xfffe
DW 0xF800
Common problems
- LEDs never light up. Check that the pin is set as an output in P1DIR. Writing P1OUT alone does nothing visible on an input pin.
- The program keeps restarting. The watchdog was not disabled, or the reset vector at 0xFFFE is missing or wrong.
- The blink is too fast to see. Increase the counter value. Without a delay, the LED toggles thousands of times per second and just looks dim.
- A subroutine crashes. Make sure the stack pointer is set before the first
call. - Wrong header file. Use the include file that matches your chip, since register names and memory sizes differ between devices.
Where to go next
Software delay loops waste CPU time, but they are the simplest way to see how output works. Later you can replace them with a timer, and use internal pull-down resistors when you start reading buttons. If you would rather write C than assembly, the same registers work in the GCC toolchain. For a comparison with another architecture, see 8051 digital output.
FAQ
Why do I need to set P1DIR before writing P1OUT?
P1DIR decides whether a pin drives a signal or listens to one. Pins are inputs after reset, so you must set the bit to 1 to make the pin an output that actually drives the LED.
What does 0x41 mean in these programs?
It is a bit mask in binary 01000001. Bit 0 selects P1.0 and bit 6 selects P1.6. Using one mask lets a single instruction control both LEDs.
Why is the watchdog timer disabled first?
The watchdog starts running after reset and restarts the chip if the program does not service it. In simple examples like these, stopping it is the easiest way to keep the program running.
How can I change the blink speed?
Change the value loaded into the delay register, such as 0xCD40 or 65000. A larger number means more loop passes and a slower blink. For accurate timing, use a hardware timer instead of a software loop.