提问人:Adel Alrowaie 提问时间:11/18/2023 最后编辑:emacs drives me nutsAdel Alrowaie 更新时间:11/18/2023 访问量:49
如何将 ATmega328P 中 PORTC 的选定引脚设置为 AVR 汇编代码中的输入?
How can i set selected pins for PORTC in ATmega328P as inputs in AVR assembly code?
问:
我正在尝试将所有 PORTD 编码为 8 位开关的输入,将所有 PORTB 编码为 LED 的输出,然后将 PINC 0 和 1 编码为选择器的输入,该选择器使 PORTD 的 4 个 MSB 根据 C1 和 C0 引脚的输入对 4 个 LSB 进行算术运算, 例如 C1C0 为 00 时的加法和 01 时的减法......等等。我知道如何将 PORTD 和 PORTB 设置为输入和输出,但我不知道如何在不需要其余端口时将 PORT 的单个 PIN 设置为输入。
LDI R16, 0x00
OUT PORTD, R16
LDI R17, 0xFF
OUT PORTB, R17
;that way i set those two ports as inputs and outputs for the switch and led
;but i want to make PINC, 0 AND PINC, 1 as inputs two for a selector in AVR assembly, how?
答:
1赞
Peter Plesník
11/18/2023
#1
你错了,你的代码根本没有将 PORTB 设置为输出。数据传输方向是使用 DDRx 寄存器设置的。上电后,该寄存器复位为零。这意味着端口的所有引脚都是输入的。正确的设置方法是这样的:
LDI R16, 0x00
OUT DDRD, R16 ;it is not necessary - it is the default state after reset
LDI R17, 0xFF
OUT DDRB, R17 ;whole PORTB is now configured as output for LED
; PORTC is still whole setup as input because it is default state after reset
LDI R16, 0x00
OUT DDRC, R16 ;of course you can set up DDRC in same way as DDRD
;example of mixed setting for port B
LDI R16, 0xFC ;0b11111100
OUT DDRB, R16 ;now PB0 and PB1 are inputs. PB2 up to PB7 are outputs
阅读 DS 中的第 13.2.3 章“在输入和输出之间切换”和 13.4 “寄存器说明”
评论
0赞
Adel Alrowaie
11/22/2023
好的,我明白了。将这两个端口设置为输入和输出 iS 正确。但是当我说类似的话时,也只将引脚 C0 和引脚 C1 作为输入。我的意思是汇编代码是这样的:CBI DDRC,0 ;这是针对 PINC 0 的
评论