2016-03-10 10 views
-1

J'essaye d'écrire un programme qui clignote des LED dans un modèle. Le motif est P0, P0 et P1, P1 et P2, etc ... jusqu'à P6 et P7. Chaque ensemble de DEL est censé clignoter 10 fois, puis passer à l'ensemble suivant du motif. Cela fonctionne bien la première fois, mais quand il revient à P0 après P6 et P7, P0 clignote 20 fois. Le reste des LED clignote 10 fois, donc je suis confus quant à savoir pourquoi il le fait pour P0. Quelqu'un peut-il expliquer pourquoi ou m'aider à le réparer?Comment fixer le programme d'assemblage du PIC à des DEL clignotantes désignées nombre de fois (c'est-à-dire 10 fois)?

Dans le code, le point-virgule (;) signifie que c'est un commentaire. Voici le code:

#include <p16F887.inc> 
__CONFIG _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT 
__CONFIG _CONFIG2, _WRT_OFF & _BOR21V 

cblock 0x20   ;start of general purpose registers 
    count1   ;used in delay routine 
    counta   ;used in delay routine 
    countb   ;used in delay routine 
    count2 
    Num1 
    Num2 
    Num3 
    Num4 
    Num5 
    Num6 
    Num7 
    Num8 
endc 

LEDPORT Equ PORTD  ;set constant LEDPORT = 'PORTB' 
LEDTRIS Equ TRISD  ;set constant for TRIS register 

org 0x0000   ;org sets the origin, 0x0000 for the 16F628, 
       ;this is where the program starts running 
movlw 0x07 

bsf  STATUS,  RP0 ;select bank 1 
movlw b'00000000'  ;set PortB all outputs 
movwf LEDTRIS 
bcf STATUS,  RP0 ;select bank 0 
clrf LEDPORT   ;set all outputs low  

Loop  
    movlw d'10' 
    movwf Num1 
    movlw d'10' 
    movwf Num2 
    movlw d'10' 
    movwf Num3 
    movlw d'10' 
    movwf Num4 
    movlw d'10' 
    movwf Num5 
    movlw d'10' 
    movwf Num6 
    movlw d'10' 
    movwf Num7 
    movlw d'10' 
    movwf Num8 

    call LOOP1 
    call LOOP2 
    call LOOP3 
    call LOOP4 
    call LOOP5 
    call LOOP6 
    call LOOP7 
    call LOOP8 

LOOP1: 
    movlw b'00000001' 
    movwf LEDPORT 
    call Delay   ;this waits for a while! 
    movlw b'00000000' 
    movwf LEDPORT 
    call Delay 
    DECFSZ Num1,1 
    goto LOOP1 
    clrf Num1 
    movlw d'10' 
    movwf Num1 
    return 

LOOP2: 
    movlw b'00000011' 
    movwf LEDPORT 
    call Delay   ;this waits for a while! 
    movlw b'00000000' 
    movwf LEDPORT 
    call Delay 
    DECFSZ Num2,1 
    goto LOOP2 
    ;clrf Num2 
    movlw d'10' 
    movwf Num2 
    return 

LOOP3: 
    movlw b'00000110' 
    movwf LEDPORT 
    call Delay   ;this waits for a while! 
    movlw b'00000000' 
    movwf LEDPORT 
    call Delay 
    DECFSZ Num3,1 
    goto LOOP3 
    ;clrf Num3 
    movlw d'10' 
    movwf Num3 
    return 

LOOP4: 
    movlw b'00001100' 
    movwf LEDPORT 
    call Delay   ;this waits for a while! 
    movlw b'00000000' 
    movwf LEDPORT 
    call Delay 
    DECFSZ Num4,1 
    goto LOOP4 
    movlw d'10' 
    movwf Num4 
    return  

LOOP5: 
    movlw b'00011000' 
    movwf LEDPORT 
    call Delay   ;this waits for a while! 
    movlw b'00000000' 
    movwf LEDPORT 
    call Delay 
    DECFSZ Num5,1 
    goto LOOP5 
    movlw d'10' 
    movwf Num5 
    return  

LOOP6: 
    movlw b'00110000' 
    movwf LEDPORT 
    call Delay   ;this waits for a while! 
    movlw b'00000000' 
    movwf LEDPORT 
    call Delay 
    DECFSZ Num6,1 
    goto LOOP6 
    movlw d'10' 
    movwf Num6 
    return 

LOOP7: 
    movlw b'01100000' 
    movwf LEDPORT 
    call Delay   ;this waits for a while! 
    movlw b'00000000' 
    movwf LEDPORT 
    call Delay 
    DECFSZ Num7,1 
    goto LOOP7 
    movlw d'10' 
    movwf Num7 
    return 

LOOP8: 
    movlw b'11000000' 
    movwf LEDPORT 
    call Delay   ;this waits for a while! 
    movlw b'00000000' 
    movwf LEDPORT 
    call Delay 
    DECFSZ Num8,1 
    goto LOOP8 
    movlw d'10' 
    movwf Num8 
    return  

Delay ;movlw d'4' 
     ;movwf count2 
     movlw d'250'   ;delay 250 ms (4 MHz clock) 
     movwf count1 
    d1 movlw 0xC7 
     movwf counta 
     movlw 0x01 
     movwf countb 
Delay_0 
     decfsz counta, f 
     goto $+2 
     decfsz countb, f 
     goto Delay_0 

     decfsz count1 ,f 
     goto d1 
     ;decfsz count2 ,f 
     ;goto d1 
     retlw 0x00 

end 
+0

Si vous commencez avec moins de LEDs, votre programme sera probablement plus court et vous détecterez probablement l'erreur dans le code, s'il y en a. – max630

Répondre

0

Votre BLINK LED

call LOOP8 

LOOP1: 

tombe à travers dans LOOP1: mais il devrait revenir au Loop. Cela provoque l'exécution à return à la fin de la fonction LOOP1:, mais comme il n'y a pas d'adresse de retour dans la pile d'appels interne, le périphérique place l'adresse reset dans le compteur de programme. Ainsi, LOOP1 semble s'exécuter deux fois. Vous devriez avoir

call LOOP8 
    goto Loop 

LOOP1: