我正在尝试通过玩 Atmega128 板来学习一点组装。当按下相应的按钮时,我试图让一组 8 个 LED 单独打开/关闭。
.INCLUDE "m128def.inc"
.CSEG
.ORG $0
initialize:
ldi r16, 1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<3 | 1<<2 | 1<<1 | 1<<0
out PORTB, r16 ; Pull up resistors
ldi r16, 1<<7 | 1<<6 | 1<<5 | 1<<4 | 1<<3 | 1<<2 | 1<<1 | 1<<0
out DDRD, r16 ; set all pins on PORTD to output
test:
in r16, PINB ; input values of buttons
swap r16 ; invert so button pressed makes value low
out PORTB, r16 ; output to led
end:
rjmp test
这行得通吗?我将 LED 连接到 PortD,将按钮连接到 Port/PinB。这基本上是我想用 C 编写的。(我对 C 的了解比在汇编中要好得多。)也许这可能会让它更清楚一点。
DDRD = 0xFF; // set to output
DDRB = 0; // set to input
PORTB = 255; // enable pull-up resistors
while (1) {
PORTD = ~PINB;
}