Arduino 的新手,并尝试制作一个程序来切换连接到 PORTB 引脚 5、4 和 3(板上的 13、12 和 11(的 LED 模块。
操纵杆具有接地连接和从 SW 到 PORTD 引脚 7(板上的 7(的连接。
这个想法是使用汇编编程来做到这一点。这是我到目前为止制作的程序(当我边走边学时,它可能非常错误(
更新
我已经调整了我的代码并修复了一些东西。当我移除 apploop 部件时,设置部分运行并打开蓝色 LED。但是,当我切换操纵杆时,apploop 组件不起作用,并且不会触发红色 LED。尝试查看其他示例,但到目前为止还无法修复
任何指示将不胜感激!
#include "m328pdef.inc"
.global main
.EQU Red_Pin, 17
.EQU Green_Pin, 16
.EQU Blue_Pin, 15
.EQU Led_Port, PORTB
.EQU Switch_Pin, 11
.EQU Switch_Port, PORTD
main:
appSetup:
; Init the 3 output RGB LED's
; in
in r16, Led_Port
; sbr
sbr r16, 0b11111111
; out
out DDRB, r16
; Init the input switch, configure with internal pull up resistor
; in
in r24, DDRD
; cbr
cbr r24, 0b10000000
; out
out DDRD,r24
; in
in r24, Switch_Port
; sbr
sbr r24, 0b11111111
; out
out Switch_Port,r24
; Turn on the Blue Led to indicate Init is done!
; sbi
sbi Led_Port,3
appLoop:
; Switch pressed = Red Led on
; sbis
sbis Switch_Port, 7
; sbi
sbi Led_Port, 3
; Switch not pressed = Red Led off
; sbic
sbic Switch_Port, 7
; cbi
cbi Led_Port,3
; rjmp appLoop ; keep doing this indefinitely
rjmp appLoop
in r16, Led_Port // <-- should be in r16, DDRB
; sbr
sbr r16, 0b11111111
; out
out DDRB, r16
从 PORTB 读取,写入 DDRB。可疑。如果仅使用 3 个作为输出,为什么设置所有位?
in r24, DDRD
cbr r24, 0b10000000
out DDRD,r24
顺便说一下,要更改寄存器 0...31 中的单个比特(ATmega328P 中的所有 PINx、DDRx、PORTx 都在此范围内(,您可以使用sbi
和cbi
指令。 例如:
cbi DDRB, 7 // clear 7th bit of DDRB
; in
in r24, Switch_Port
; sbr
sbr r24, 0b11111111
; out
out Switch_Port,r24
同样,如果您只需要一个,为什么所有 8 位都设置?
; Turn on the Blue Led to indicate Init is done!
sbi Led_Port,3
...
; Switch pressed = Red Led on
sbi Led_Port, 3
那么,Led_Port的第 3 位 (PORTB( 是红色的?还是蓝色?
你的代码一团糟。与即时值混合的命名常量。如果使用某些命名常量。例:
.EQU LED_RED_pinno, 5
.EQU LED_BLUE_pinno, 3
...
; Turn on the Blue Led to indicate Init is done!
sbi Led_Port, LED_BLUE_pinno
...
; Switch pressed = Red Led on
sbi Led_Port, LED_RED_pinno
也:
; sbis
sbis Switch_Port, 7
; sbi
sbi Led_Port, 3
如果设置了Switch_Port
中的位 7,sbis
将跳过下一条指令。Switch_Port
等于PORTB
,并且它的位 7 总是被清除(除非你给它写 1(。
要检查输入端口的状态,您需要读取PINx
(而不是PORTx!
(寄存器。在这种情况下,它应该是:
; Switch pressed = Red Led on
sbis PINB, 7 // (sic!)
sbi Led_Port, 3
; Switch not pressed = Red Led off
sbic PINB, 7 // (sic!)
cbi Led_Port,3