2017-04-03 34 views
0

我正在爲ATmega16編寫這個程序集,我只是想讓按鈕按下時閃爍10次LED。不幸的是,當達到RET指令時,它將我帶到第一個代碼(start),而不是在調用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