简体   繁体   English

Atmega128组装项目

[英]Atmega128 Assembly Project

I'm trying to learn a little Assembly by playing around with the Atmega128 board. 我正在尝试通过玩Atmega128开发板来学习一些汇编程序。 I'm attempting to make a set of 8 LED's individually turn on/off when their appropriate button is pressed. 我试图在按下相应的按钮时分别使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

Does this work? 这样行吗? I have the LED's conected to PortD and the buttons to Port/PinB. 我将LED连接到PortD,将按钮连接到Port / PinB。 This is basically what I want to do written in C. (I'm much better versed in C than in Assembly.) Perhaps this might make it a little clearer. 基本上,这就是我想用C编写的内容。(我比C更好地精通C。)也许这可能会使它更清晰一些。

DDRD  = 0xFF; // set to output
DDRB  = 0; // set to input
PORTB = 255; // enable pull-up resistors
while (1)  {
    PORTD = ~PINB;
}

PINA and PORTA refer to the same physical pins. PINA和PORTA指的是相同的物理引脚。 AVR has two names for them to distinguish between input and output. AVR有两个名称,可以区分输入和输出。

The code above has half of the pins (0 to 3) configured as output, and the pins 4 to 7 are inputs with pullups. 上面的代码将引脚的一半(0至3)配置为输出,引脚4至7是带上拉的输入。

The next part of the code is not that clear. 代码的下一部分不清楚。 You are reading 0 to 3 as inputs, then shifting so as to write to pins 4 to 7, as well as pin 1. This is not compatible with the initial set up of the pins. 您正在读取0到3作为输入,然后进行移位以写入引脚4到7以及引脚1。这与引脚的初始设置不兼容。 Current will still flow out an input pin, but it is not designed to do it well. 电流仍会流出输入引脚,但设计得不好。

Do you have buttons and leds attached to the same pins? 您是否在相同的引脚上连接了按钮和指示灯? Then you should use one or the other on a pin, and set the DDR and pullup appropriately. 然后,您应该在一个引脚上使用一个或另一个,并适当设置DDR和上拉。 Determine how the buttons are wired. 确定按钮的接线方式。 Do they pull the input low to ground or high to Vcc? 他们是将输入拉低至地还是将高拉至Vcc?

I don't see a way to have a pin be both an input and an output at the same time with the polling code you are using. 我看不到将引脚与您正在使用的轮询代码同时用作输入和输出的方法。 If you want to have the buttons on 0 to 3 and the outputs on 4 to 7, then you need to change the code slightly. 如果要将按钮设置为0到3,将输出设置为4到7,则需要稍微更改代码。

.INCLUDE "m128def.inc" 
.CSEG 
.ORG $0

initialize:
    ldi r16, 1<<3 | 1<<2 | 1<<1 | 1<<0         ; pullup inputs
    out PORTA, r16 
    ldi r16, 1<<7 | 1<<6 | 1<<5 | 1<<4         ; 4 to 7 are output
    out DDRA, r16
test:
    in r16, PINA
    lsr r16 
    lsr r16
    lsr r16
    lsr r16
    ori r16, 1<<3 | 1<<2 | 1<<1 | 1<<0         ; pullup 0 to 3, and output to 4 to 7
    out PORTA, r16
end:
    rjmp test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM