我有 8 个 LED 和 2 个按钮,最初第一个和最后一个 LED 点亮,如果我按下第一个按钮,第一个 LED 向右移动,如果我按下第二个按钮,最后一个 LED 向左移动。如果两个灯“相遇”,通过相互转换,它们就会关闭。
按下按钮后,我不知道如何更新单行上的 LED,所以我为每个按钮制作了 2 行,P1 和 P2。第一行似乎正确移动,但第二行真的搞砸了。我究竟做错了什么?
下面是我的代码:
$TITLE(5)
$MOD51
ORG 100H
START:
; 8 leds, P1.0-P1.7
; 2 buttons, P0.1 P0.2
MOV P1, #00h
MOV P2, #00h
; left LED positions
MOV 30H, #00000001b ; initial position
MOV 31H, #00000010b
MOV 32H, #00000100b
MOV 33H, #00001000b
MOV 34H, #00010000b
MOV 35H, #00100000b
MOV 36H, #01000000b
MOV 37H, #10000000b
MOV 38H, #00000000b ; leds meet
; right led positions
MOV 39H, #10000000b ; initial position
MOV 40H, #01000000b
MOV 41H, #00100000b
MOV 42H, #00010000b
MOV 43H, #00001000b
MOV 44H, #00000100b
MOV 45H, #00000010b
MOV 46H, #00000001b
MOV R1, #0
MOV R2, #0
LOOP:
JNB P0.1, INCREMENT_L ; left button pressed, led shifts right
JNB P0.2, INCREMENT_R ; right button pressed, led shifts left
CALL DISP_L ; display left led
CALL DISP_R ; display right led
JMP LOOP
INCREMENT_L:
SETB P0.1
CJNE R1, #7, INC_L
MOV R1, #0
JMP LOOP
INCREMENT_R:
SETB P0.2
CJNE R2, #7, INC_R
MOV R2, #0
JMP LOOP
INC_L:
MOV A, R2
ADD A, #39H
MOV B, A
MOV A, R1
ADD A, #30H
CJNE A, B, INCL
JMP RESET
JMP LOOP
INC_R:
MOV A, R1
ADD A, #30H
MOV B, A
MOV A, R2
ADD A, #39H
CJNE A, B, INCR
JMP RESET
JMP LOOP
INCL:
INC R1
JMP LOOP
INCR:
INC R2
JMP LOOP
DISP_L:
MOV A, R1
ADD A, #30H
MOV R0, A
MOV P1, @R0
RET
DISP_R:
MOV A, R2
ADD A, #39H
MOV R0, A
MOV P2, @R0
RET
RESET:
MOV R1, #0
MOV R2, #0
JMP LOOP
END