;------------------------------------------------------------------------------- ; MSP430 Assembler Code Template for use with TI Code Composer Studio ; ; ;------------------------------------------------------------------------------- .cdecls C,LIST,"msp430.h" ; Include device header file ;------------------------------------------------------------------------------- .def RESET ; Export program entry-point to ; make it known to linker. ;------------------------------------------------------------------------------- .text ; Assemble into program memory. .retain ; Override ELF conditional linking ; and retain current section. .retainrefs ; And 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 ;------------------------------------------------------------------------------- Setup: bis.b #004h, &P2DIR ; Set P2.2 to output ; direction (0000_0100) bic.b #004h, &P2OUT ; Set P2OUT to 0x0000_0100 (ensure ; LED1 is off) bis.b #002h, &P2DIR ; Set P2.1 to output ; direction (0000_0010) bic.b #002h, &P2OUT ; Set P2OUT to 0x0000_0010 (ensure ; LED1 is off) bis.w #GIE, SR ; Enable Global Interrupts bis.b #001h, &P1IE ; Enable Port 1 interrupt from bit 0 bis.b #001h, &P1IES ; Set interrupt to call from hi to low bic.b #001h, &P1IFG ; Clear interrupt flag ;mov.w #0, R8 ; Counter for inner loop bic.b #0x04,&P2OUT ; Set P2OUT to 0x0000_0100 (LEDS off) InfLoop: mov.w #0xFFFF, R5 ; Software delay (65,535*16cc/2^20 ~ 1s) mov.w #0, R8 ; Counter for inner loop Iter: SWDelay1: nop ; 1cc (total delay is 16 cc) nop nop nop nop nop nop nop nop nop dec.w R5 ; 1cc jnz SWDelay1 ; 2cc inc.b R8 ; 3cc cmp #2, R8 ; 4cc jnz Iter ; jump to Iter xor.b #0x02, P2OUT ; toggle LED 2 jmp InfLoop ; goto InfLoop SW1_ISR: bic.b #001h, &P1IFG ; Clear interrupt flag ChkSw1: bit.b #01h, &P1IN ; Check if SW1 is pressed ; (0000_0001 on P1IN) jnz LExit ; If not zero, SW is not pressed ; loop and check again Debounce: mov.w #2000, R15 ; Set to (2000 * 10 cc ) SWD20ms: dec.w R15 ; Decrement R15 nop nop nop nop nop nop nop jnz SWD20ms ; Delay over? bit.b #00000001b,&P1IN ; Verify SW1 is still pressed jnz LExit ; If not, wait for SW1 press LEDon: xor.b #0x04,P2OUT ; Turn on LED1 LExit: reti ; Return from interrupt ;------------------------------------------------------------------------------- ; Stack Pointer definition ;------------------------------------------------------------------------------- .global __STACK_END .sect .stack ;------------------------------------------------------------------------------- ; Interrupt Vectors ;------------------------------------------------------------------------------- .sect ".reset" ; MSP430 RESET Vector .short RESET .sect ".int20" ; .short SW1_ISR ; set SW1 Interupt service routine .end