This code blinks on and off an LED on my MSP430 from TI. I've marked teh number of cycles used for each instruction, but I'm not sure where the actual 'timing' factor comes in, as the RED led blinks on and off about every half second. To me, it looks like it should just be beignn toggled as fast as the processor can go. I'm trying to get the delayloop to be 1/10 of a second so the main loop can run it 100 times, thus blinking the LED very quickly, once every 10 seconds.

;*******************************************************************************
           .cdecls C,LIST, "msp430.h"       ; MSP430

;------------------------------------------------------------------------------
            .text                           ; beginning of executable code
;------------------------------------------------------------------------------
RESET:      mov.w   #0x0280,SP              ; init stack pointer, 5 CYCLES
            mov.w   #WDTPW+WDTHOLD,&WDTCTL  ; stop WDT, 5 CYCLES
            bis.b   #0x01,&P1DIR            ; set P1.0 as output, 5 CYCLES
                                            ; Total 15 Cycles

mainloop:   xor.b   #0x01,&P1OUT            ; toggle P1.0. 5 CYLCES
            mov.w   #0,r15                  ; use R15 as delay counter, 2 CYCLES       execute 100 delayloop times
                                            ; Total 7 Cycles

delayloop:  sub.w   #1,r15                  ; delay over? 2 CYCLES
            jnz     delayloop               ; n 2 CYCLES                               go for 1/10 of a second
            jmp     mainloop                ; y, toggle led 2 CYCLES
                                            ; Total 6 Cycles
;------------------------------------------------------------------------------
;           Interrupt Vectors
;------------------------------------------------------------------------------
            .sect   ".reset"                ; MSP430 RESET Vector
            .word   RESET                   ; start address
            .end

The timing comes from 65536 decrements of the R15 register in delayloop, from

delayloop:  sub.w   #1,r15                  ; delay over? 2 CYCLES
            jnz     delayloop               ; n 2 CYCLES       

R15 has been previously loaded with a zero. First decrement will take it to 0xffff. Then it'll be deceremented till it reaches zero again.

This article has been dead for over six months. Start a new discussion instead.