【问题标题】:AVR LED on-off program in assembly using buttons使用按钮组装的 AVR LED 开关程序
【发布时间】:2017-04-03 16:32:57
【问题描述】:

我正在使用 AtmelStudio 7 为 ATMEL ATmega16 编写汇编代码。 我只想在按下按钮时打开 LED,并且希望在再次按下时将其关闭。就像房间的灯一样。出于某种原因,此代码仅打开灯,而不会在按下按钮时熄灭(按钮在 proteus 中模拟)。谁能帮我写这个简单的代码?

start:
    /* set the PIND2 data direction to 0 for input */
    /* This one simulates the key */
    ldi R16, (0 << PD2) ; Make PD2 as input
    out DDRB,R16    ; Configure the PIND2 as input

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

OFF_MODE:
    /* Put the PORTB7 to 0 */ 
    ldi R18,(0 << PB7)
    out PORTB,R18
    call delay
    /* Skip if PIN 2 in PORT D is set */
    sbis PIND,2
    jmp OFF_MODE    ; Branch to the OFF_MODE if the key isn't pressed yet

ON_MODE:
    /* Put the PORTB to 1 */
    ldi R18,(1 << PB7)
    out PORTB,R18
    call delay
    /* Skip if PIN 2 in PORT D is set */
    sbis PIND,2
    jmp ON_MODE ; Branch to the ON_MODE if the key isn't unpressed yet
rjmp start
delay:
    ldi r16, 0xFF
delay_loop_1:
    dec r16
    cpi r16, 0x00
    jmp delay_loop_1
ret

【问题讨论】:

    标签: assembly avr atmelstudio


    【解决方案1】:

    delay 函数中有一个无限循环。介意使用:

    delay:
        ldi r16, 0xFF
    delay_loop_1:
        dec r16
        brne delay_loop_1
        ret
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多