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
指令。 E.g:
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(PORTB)的位3是红色吗?还是蓝色?
您的代码中一团糟。命名常量与立即值混合在一起。如果使用一些命名常量。例如:
.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
sbis
跳过下一条指令,如果Switch_Port
中的位7被设置。 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