MSP430 Blinklicht (ohne Timer)

Das Programm lässt eine LED alle 500ms blinken (500ms aus, 500ms an) -> Blinklicht. Ein Beispiel-Programm in Assembler, programmiert ohne Timer, sondern mit einer Verzögerungsfunktion (mit einer gewissen Genauigkeit). Das Unterprogramm kann als “Software-Timer” benutzt werden. Falls es auf keine hohe zeitliche Genauigkeit ankommen soll, können solche Delay-Funktionen/Unterprogramme sehr gut benutzt werden.

ASM-Programm

FILE: main.asm

;***********************************************
;     ___       _             _     
;    |   |_ _ _| |___ ___   _| |___
;    | | |_'_| . | -_|  _|_| . | -_|
;    |___|_,_|___|___|___|_|___|___|
;
; FILE:     main.asm
; Author:   declis (xdec.de)
;***********************************************

    .cdecls "msp430g2231.h"
time_ms .equ 500                   ;time-value in milliseconds
    .ref time_wait
    .global main
    .text

main:
    mov.w #0x280,SP                ;initialize stack pointer
    mov.w #WDTPW+WDTHOLD,&WDTCTL   ;stop watchdog timer

    bis.b #BIT6,&P1DIR             ;P1.6 -> output
    bic.b #BIT6,&P1OUT             ;clear P1.6 (LED off)

    mov.w #time_ms,R4              ;500ms

loop:
    xor.b #BIT6,&P1OUT             ;toggle P1.0 (LED2)
    call #time_wait                ;wait 500ms
    jmp loop

    .end

FILE: lib_func.asm

;***********************************************
;     ___       _             _     
;    |   |_ _ _| |___ ___   _| |___
;    | | |_'_| . | -_|  _|_| . | -_|
;    |___|_,_|___|___|___|_|___|___|
;
; FILE:     lib_func.asm
; Author:   declis (xdec.de)
;***********************************************

    .cdecls "msp430g2231.h"
    .def time_wait             ;global entry of function
    .text

;------------------------------------------
; Name:         time_wait
; Description:  wait "x" ms
;               (the inner loop consume 1ms,
;               outer loop  is multiplication
;               factor "x".
;               (1ms*200=200ms for example)
;               SMCLK=DC0=~1MHz
;               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
;               time in s = (5*195+24)*x/(1*10^6Hz)
;               example: x=500 (500ms)
;                        => 0,4995s
;               ~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Input:        R4 - "x" ms
; Returns:      nothing
; Destroys:     nothing
;------------------------------------------
time_wait:
    push.w SR          ;3 cycles
    push.w R4          ;3 cycles
    push.w R5          ;3 cycles

outer_loop:
    mov.w #195,R5      ;2 cycles
inner_loop:
    sub.w #1,R5        ;2 cycles
    nop                ;1 cycle
    jnz inner_loop     ;2 cycles
    sub.w #1,R4        ;2 cycles
    jnz outer_loop     ;2 cycles

    pop.w R5           ;3 cycles
    pop.w R4           ;3 cycles
    pop.w SR           ;3 cycles
    ret

    .end

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.