2017-04-03 3 views
0

このアセンブリをATmega16用に書いていますが、ボタンを押したときにLEDを10回点滅させたいだけです。残念ながら、RET命令に達すると、LONG_DELAYを呼び出すときにどこに戻ったのではなく、最初のコード(開始)に移動します。RET命令がサブルーチン-AVRアセンブリと呼ばれる場所に戻っていない

誰かが私を助けてくれますか?ありがとう。

start: 
    /* set the PINB0 data direction to 0 for input */ 
    /* This one simulates the key */ 
    ldi R16, (0 << PB0) ; Load 0b00000001 in R16 
    out DDRB,R16 ; Configure the PINB0 as input 

    /* set the PORTB7 data direction to 1 for output */ 
    /* this one causes the LED to be ON/OFF */ 
    ldi R17, (1 << PB7) ; Load 0b10000000 in R17 
    out DDRB,R17 ; Configure the PORTB7 as output 

OFF_MODE: 
    /* Put the PORTB7 to 0 */ 
    ldi R18,(0 << PB7) 
    out PORTB,R18 
    /* Check the content of PINB0 */ 
    /* Wait for the PINB to get pressed by the user */ 
    sbis PINB,0 
    brne OFF_MODE ; Branch to the OFF_MODE if the key isn't pressed yet 

BLINK_MODE: 
    /* Define a counter */ 
    ldi R20,0 
    /* Turn on the LED */ 
    /* Put the PORTB7 to 1 */ 
    ldi R18,(1 << PB7) 
    out PORTB,R18 

    /* Create a delay */ 
    call LONG_DELAY 

    /* Turn off the LED */ 
    ldi R18,(0 << PB7) 
    out PORTB,R18 

    /* Increment the counter */ 
    inc R20 

    /* Check the content of the counter */ 
    cpi R20,0x0A 
    brne BLINK_MODE 
    /* Clear the input to avoid duplicate press virtualization */ 
    cbi PINB,0 
    jmp OFF_MODE 
rjmp start 

    /* Delay function */ 
LONG_DELAY: 
    ldi r25,10 
LOOP: 
    nop 
    dec R25 
    cpi R25,0 
    brne LOOP 
    ret 
+0

あなたはそれが戻っstart' 'へ行く知っていますか、一見何も悪いことを見つけることができませんが? – Jester

+0

@Jester AtmelStudio 7でデバッグしています。 – aligholamee

+1

おそらくスタックポインタを初期化してください。 – Jester

答えて

0

ミスの一部なので、ここであなたはありました:

; Replace with your application code 
start: 
    //stack setup 
    ldi r16, 0 
    out SPH, r16 
    ldi r16, 0xf0 
    out SPL, r16 
    /* set the PINB0 data direction to 0 for input */ 
    /* This one simulates the key */ 
    ldi R16, (0 << PB0) ; Load 0b00000001 in R16 
    out DDRB,R16 ; Configure the PINB0 as input 

    /* set the PORTB7 data direction to 1 for output */ 
    /* this one causes the LED to be ON/OFF */ 
    ldi R17, (1 << PB7) ; Load 0b10000000 in R17 
    out DDRB,R17 ; Configure the PORTB7 as output 

OFF_MODE: 
    /* Put the PORTB7 to 0 */ 
    ldi R18,(0 << PB7) 
    out PORTB,R18 
    /* Check the content of PINB0 */ 
    /* Wait for the PINB to get pressed by the user */ 
    sbis PINB,0 
    brne OFF_MODE ; Branch to the OFF_MODE if the key isn't pressed yet 
//Here was mistake 
    ldi R20, 10 

BLINK_MODE: 
    /* Turn on the LED */ 
    /* Put the PORTB7 to 1 */ 
    ldi R18,(1 << PB7) 
    out PORTB,R18 

    /* Create a delay */ 
    call LONG_DELAY 

    /* Turn off the LED */ 
    ldi R18,(0 << PB7) 
    out PORTB,R18 

    /* Increment the counter */ 
    dec R20 
    brne BLINK_MODE 
    /* Clear the input to avoid duplicate press virtualization */ 
    cbi PINB,0 
    jmp OFF_MODE 
rjmp start 

    /* Delay function */ 
LONG_DELAY: 
    ldi r25,10 
LOOP: 
    dec R25 
//here was redundant instruction 
    brne LOOP 
    ret 
+0

なぜcpiが冗長ですか?一緒に比較してもらえませんか? – aligholamee

+0

'dec'はzフラグを単独で変更するので、' cpi'の必要はありません。参照用に:http://www.atmel.com/webdoc/avrassembler/avrassembler.wb_DEC.html – nopasara

+0

働いて、ありがとう。 – aligholamee

関連する問題