MSP430 Assembly Code Examples from Class: Difference between revisions

From Class Wiki
Jump to navigation Jump to search
Line 217: Line 217:


.end
.end
</nowiki>
=====Put PWM on Port P1.2=====
<nowiki>
;-------------------------------------------------------------------------------
; MSP430 Assembler Code Template for use with TI Code Composer Studio
;
; This program blinks the LED if you hook it up to P1.2 at one Hz period using TimerA0
; and no interrupts. The duty cycle is TA0CCR1/TA0CCR0.
; 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 #BIT2, &P1DIR ; Set the LED at P1.2 as output.
bis.b #BIT2, &P1SEL ; Select the TA0.2 instead of GPIO.
bic.w #MC0|MC1, &TA0CTL ; Turn off timer TA0.
bis.w #TACLR, &TA0CTL ; Clear the timer TAR.
bis.w #TASSEL__ACLK, &TA0CTL ; Use ACLK (32768 Hz).
bis.w #ID1, &TA0CTL ; Divide by 4. Gives 1/4096 uS per tic, because we are using up and down mode.
bic.w #ID0, &TA0CTL ; The other bit to divide by 4.
bis.w #MC0|MC1, &TA0CTL ; Set timer mode to count to TA0CCR0 and then back down.
bic.w #CAP, &TA0CCTL1 ; Set compare mode.
bis.w #OUTMOD_2, &TA0CCTL1 ; Toggle/Reset mode: this gives duty cycle of TA0CCR1/TA0CCR0.
mov.w #4096, &TA0CCR0 ; This gives a 1 second period.
mov.w #1024, &TA0CCR1 ; 25% duty cycle.
bis.w #MC0|MC1, &TA0CTL ; Turn on timer TA0.

nop
bis.w #LPM3, SR ; Put the processor in Low Power Mode 3 (ACLK still active).
nop
; No loop necessary. All done by interrupts.

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

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

.end
</nowiki>
</nowiki>

Revision as of 19:58, 30 November 2014

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

Put PWM on Port P1.2
;-------------------------------------------------------------------------------
; MSP430 Assembler Code Template for use with TI Code Composer Studio
;
; This program blinks the LED if you hook it up to P1.2 at one Hz period using TimerA0
; and no interrupts.  The duty cycle is TA0CCR1/TA0CCR0.
; 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	#BIT2, &P1DIR			; Set the LED at P1.2 as output.
			bis.b	#BIT2, &P1SEL			; Select the TA0.2 instead of GPIO.
			bic.w	#MC0|MC1, &TA0CTL		; Turn off timer TA0.
			bis.w	#TACLR, &TA0CTL			; Clear the timer TAR.
			bis.w	#TASSEL__ACLK, &TA0CTL	; Use ACLK (32768 Hz).
			bis.w	#ID1, &TA0CTL			; Divide by 4.  Gives 1/4096 uS per tic, because we are using up and down mode.
			bic.w 	#ID0, &TA0CTL			; The other bit to divide by 4.
			bis.w	#MC0|MC1, &TA0CTL		; Set timer mode to count to TA0CCR0 and then back down.
			bic.w	#CAP,	&TA0CCTL1		; Set compare mode.
			bis.w	#OUTMOD_2, &TA0CCTL1	; Toggle/Reset mode: this gives duty cycle of TA0CCR1/TA0CCR0.
			mov.w	#4096, &TA0CCR0			; This gives a 1 second period.
			mov.w	#1024, &TA0CCR1			; 25% duty cycle.
			bis.w	#MC0|MC1, &TA0CTL		; Turn on timer TA0.

			nop
			bis.w	#LPM3, SR				; Put the processor in Low Power Mode 3 (ACLK still active).
			nop
											; No loop necessary.  All done by interrupts.

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

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

			.end