MSP430 Assembly Code Examples from Class

From Class Wiki
Jump to navigation Jump to search

Fibonacci Sequence (Mystery Program from ece382.com)

;-------------------------------------------------------------------------------
; MSP430 Assembler Code Template for use with TI Code Composer Studio
;
; This is the Fibonacci Sequence Program.
;-------------------------------------------------------------------------------
            .cdecls C,LIST,"msp430.h"       ; Include device header file

;-------------------------------------------------------------------------------
            .text                           ; Assemble into program memory
            .global RESET
            .retain                         ; Override ELF conditional linking
                                            ; and retain current section
            .retainrefs                     ; Additionally retain any sections
                                            ; that have references to current
                                            ; section
;-------------------------------------------------------------------------------
RESET       mov.w   #__STACK_END,SP         ; Initialize stackpointer
StopWDT     mov.w   #WDTPW|WDTHOLD,&WDTCTL  ; Stop watchdog timer

;-------------------------------------------------------------------------------
                                            ; Main loop here
;-------------------------------------------------------------------------------
            mov.w   #0x2400, r9
            mov.w   #10, r10

            mov.w   #0, r11
            mov.w   r11, 0(r9)

            mov.w   #1, r12
            incd    r9
            mov.w   r12, 0(r9)

loop        tst     r10
            jz      forever

            incd    r9
            dec     r10

            mov.w   r12, r13
            add.w   r11, r12
            mov.w   r12, 0(r9)
            mov.w   r13, r11
            jmp     loop

forever     jmp     forever
;-------------------------------------------------------------------------------
;           Stack Pointer definition
;-------------------------------------------------------------------------------
            .global __STACK_END
            .sect 	.stack

;-------------------------------------------------------------------------------
;           Interrupt Vectors
;-------------------------------------------------------------------------------
            .sect   ".reset"                ; MSP430 RESET Vector
            .short  RESET

Use S2 (P1.1) to turn on and off LED1 (P1.0).

;-------------------------------------------------------------------------------
; MSP430 Assembler Code Template for use with TI Code Composer Studio
;
;
;
;-------------------------------------------------------------------------------
            .cdecls C,LIST,"msp430.h"       ; Include device header file

;-------------------------------------------------------------------------------
            .text                           ; Assemble into program memory
            .global RESET
            .retain                         ; Override ELF conditional linking
                                            ; and retain current section
            .retainrefs                     ; Additionally retain any sections
                                            ; that have references to current
                                            ; section
;-------------------------------------------------------------------------------
RESET       mov.w   #__STACK_END,SP         ; Initialize stackpointer
StopWDT     mov.w   #WDTPW|WDTHOLD,&WDTCTL  ; Stop watchdog timer

;-------------------------------------------------------------------------------
                                            ; Main loop here
;-------------------------------------------------------------------------------
 			bis.b	#BIT0, &P1DIR			; Set the P1.0 as output for the LED.
 			bic.b	#BIT1, &P1DIR			; Set P1.1 for input.
			bis.b	#BIT1, &P1REN ; Set pull up for switch on P1.1
			bis.b	#BIT1, &P1OUT ; Make the pull up pull up not down.forever

loop		bit.b	#BIT1, &P1IN  ; Check if the button is switched.
			jnz 	off
on			bis.b	#BIT0, &P1OUT ; Toggle the LED
			jmp 	loop
off			bic.w	#BIT0, &P1OUT ; Turn the LED off to start with.
			jmp     loop
;-------------------------------------------------------------------------------
;           Stack Pointer definition
;-------------------------------------------------------------------------------
            .global __STACK_END
            .sect 	.stack

;-------------------------------------------------------------------------------
;           Interrupt Vectors
;-------------------------------------------------------------------------------
            .sect   ".reset"                ; MSP430 RESET Vector
            .short  RESET

Simple Blink the LED without Interrupts

;-------------------------------------------------------------------------------
; MSP430 Assembler Code Template for use with TI Code Composer Studio
;
; Blink the LED using no interrupts.
; Rob Frohne, 2014
;-------------------------------------------------------------------------------
            .cdecls C,LIST,"msp430.h"       ; Include device header file

;-------------------------------------------------------------------------------
            .text                           ; Assemble into program memory
            .global RESET
            .retain                         ; Override ELF conditional linking
                                            ; and retain current section
            .retainrefs                     ; Additionally retain any sections
                                            ; that have references to current
                                            ; section
;-------------------------------------------------------------------------------
RESET       mov.w   #__STACK_END,SP         ; Initialize stackpointer
StopWDT     mov.w   #WDTPW|WDTHOLD,&WDTCTL  ; Stop watchdog timer

;-------------------------------------------------------------------------------
                                            ; Main loop here
;-------------------------------------------------------------------------------
; This program is to blink the red LED on the MSP430F5529 LaunchPad.
			bis.b   #1,&PADIR
outer
			xor.b   #1,&PAOUT
			mov.w   #0x2710,r5
inner
			dec.w   r5
			tst.w   r5
			jne inner
			jmp outer
;-------------------------------------------------------------------------------
;           Stack Pointer definition
;-------------------------------------------------------------------------------
            .global __STACK_END
            .sect 	.stack

;-------------------------------------------------------------------------------
;           Interrupt Vectors
;-------------------------------------------------------------------------------
            .sect   ".reset"                ; MSP430 RESET Vector
            .short  RESET


Blink the LED Using Timer TA0 and Interrupts

;-------------------------------------------------------------------------------
; MSP430 Assembler Code Template for use with TI Code Composer Studio
;
; This program blinks the LED on P1.0 at one Hz period using TimerA0
; and interrupts.
; Rob Frohne, 11/17/2014.
;-------------------------------------------------------------------------------
            .cdecls C,LIST,"msp430.h"       ; Include device header file

;-------------------------------------------------------------------------------
            .text                           ; Assemble into program memory
            .global RESET					; Remember to set linker option to enter at RESET.
            .retain                         ; Override ELF conditional linking
                                            ; and retain current section
            .retainrefs                     ; Additionally retain any sections
                                            ; that have references to current
                                            ; section
;-------------------------------------------------------------------------------
RESET       mov.w   #__STACK_END,SP         ; Initialize stackpointer
StopWDT     mov.w   #WDTPW|WDTHOLD,&WDTCTL  ; Stop watchdog timer

;-------------------------------------------------------------------------------
                                            ; Main loop here
;-------------------------------------------------------------------------------
			bis.b	#BIT0, &P1DIR			; Set the LED at P1.0 as output.
			bic.b	#BIT0, &P1OUT			; Ensure the LED is off to start with.
			bic.w	#MC0|MC1, &TA0CTL		; Turn off timer TA0.
			bis.w	#TACLR, &TA0CTL			; Clear the timer TAR.
			bis.w	#TASSEL__ACLK, &TA0CTL	; Use ACLK (32 KHz).  This means 2 seconds a period.
			bic.w	#ID0|ID1, &TA0CTL		; Divide by 4.
			bic.w 	#TAIFG, &TA0CTL			; Clear the interrupt flag.
			bis.w	#MC0, &TA0CTL			; Set timer mode to count to TA0CCR0.
			bis.w	#TAIE, &TA0CTL			; Enable timer TA0 interrupts.
			mov.w	#0x3fff, &TA0CCR0		; Make it count up to 1/4 maximum, meaning 1 second on off period.
			nop
			bis.w	#LPM3|GIE, SR			; Put the processor in Low Power Mode 3 (ACLK still active) & Enable Interrupts.
			nop
											; No loop necessary.  All done by interrupts.
TA0_ISR
			cmp.w	#TA0IV_TAIFG, &TA0IV	; Check if the timer overflowed.  (Other opitons exist.)
			jnz		not_correct_flag		; If TA0IV not equal to 0x000e, it wasn't an overflow.
			xor.b	#BIT0, &P1OUT			; Toggle the LED at P1.0.
not_correct_flag
			bic.w	#TAIFG, &TA0CTL			; This is the ISR.  Clear interrupt flag
			reti
;-------------------------------------------------------------------------------
;           Stack Pointer definition
;-------------------------------------------------------------------------------
            .global __STACK_END
            .sect 	.stack

;-------------------------------------------------------------------------------
;           Interrupt Vectors
;-------------------------------------------------------------------------------
            .sect   ".reset"                ; MSP430 RESET Vector
            .short  RESET
	    .sect   ".int52"		    ; Timer0_A5 Interrupt Service Routine /* 0xFFE8 Timer0_A5 CC1-4, TA */
	    .short  TA0_ISR		    ; page 49, http://www.ti.com/lit/ug/slau157af/slau157af.pdf

	    .end