Em đang tìm hiểu làm mạch đèn giao thông dùng nút bấm để thay đổi thời gian. Khi chạy mô phỏng thì 7 led thanh của em nó đếm ngược về 00 sau đó nó lại chạy số 85 kiểu như không thoát khỏi vòng lặp ấy. Em không biết nó bị sai ở đâu. Các bác xem lại giúp em nhé! THANK
Code chương trình
Hình ảnh mô phỏng
Code chương trình
ORG 00H
JMP MAIN
;--------------------------------------------------
ORG 0BH
INC R0
CJNE R0, #40, END_
MOV R0, #0
INC R1
CJNE R1,#100, END_
MOV R1, #0
DEC R2
DEC R3
CJNE R2, #0, END_
MOV R2, #0
CJNE R3, #0, END_
MOV R3, #0
END_:
RETI
;------------Chuong trinh dieu khien led------------
CT1: ; DO 1 - XANH 2
SETB P1.4
SETB P1.7
CALL DELAY
MOV R2, #5
MOV R3, #10
CALL MAINLOOP
RET
CT2: ; DO 1 - VANG 2
CLR P1.7
CALL DELAY
SETB P1.5
SETB P3.7
MOV R2,#5
MOV R3,#5
CALL MAINLOOP
RET
CT3: ; XANH 1 - DO 2
CLR P1.4
CLR P3.7
CALL DELAY
SETB P1.6
SETB P3.6
MOV R2, #99
MOV R3, #94
CALL MAINLOOP
RET
CT4: ; VANG 1 - DO 2
CLR P1.6
CALL DELAY
SETB P3.6
SETB P1.5
MOV R2,#5
MOV R3,#5
CALL MAINLOOP
RET
;--------------------------------------
MAIN:
MOVP1, #00H
MOV P3, #3FH
MOV P2, #00H
MOV P0, #00H
MOV TMOD, #02H
MOV TH0,#6
MOV IE, #87H
SETB TR0
CALL CT1
CALL CT2
CALL CT3
CALL CT4
JMP MAIN
;---------------------------
MAINLOOP:
MOV A, R2
MOV B, #10
DIV AB
MOV DPTR, #LEDCODE
MOVC A, @A+DPTR
MOV P2, A
SETB P1.2
CALL DELAY
CLR P1.2
MOV A, B
MOVC A, @A+DPTR
MOV P2, A
SETB P1.3
CALL DELAY
CLR P1.3
MOV A, R3
MOV B, #10
DIV AB
MOV DPTR, #LEDCODE
MOVC A, @A+DPTR
MOV P0, A
SETB P1.0
CALL DELAY
CLR P1.0
MOV A, B
MOVC A, @A+DPTR
MOV P0, A
SETB P1.1
CALL DELAY
CLR P1.1
JMP MAINLOOP
;------------------------------
LEDCODE: DB 40H, 79H, 24H, 30H, 19H, 12H, 02H, 78H, 00H, 10H
;------------------------------
DELAY:
MOV R4, #2
DELAY1:
DJNZ R4, DELAY1
RET
END
JMP MAIN
;--------------------------------------------------
ORG 0BH
INC R0
CJNE R0, #40, END_
MOV R0, #0
INC R1
CJNE R1,#100, END_
MOV R1, #0
DEC R2
DEC R3
CJNE R2, #0, END_
MOV R2, #0
CJNE R3, #0, END_
MOV R3, #0
END_:
RETI
;------------Chuong trinh dieu khien led------------
CT1: ; DO 1 - XANH 2
SETB P1.4
SETB P1.7
CALL DELAY
MOV R2, #5
MOV R3, #10
CALL MAINLOOP
RET
CT2: ; DO 1 - VANG 2
CLR P1.7
CALL DELAY
SETB P1.5
SETB P3.7
MOV R2,#5
MOV R3,#5
CALL MAINLOOP
RET
CT3: ; XANH 1 - DO 2
CLR P1.4
CLR P3.7
CALL DELAY
SETB P1.6
SETB P3.6
MOV R2, #99
MOV R3, #94
CALL MAINLOOP
RET
CT4: ; VANG 1 - DO 2
CLR P1.6
CALL DELAY
SETB P3.6
SETB P1.5
MOV R2,#5
MOV R3,#5
CALL MAINLOOP
RET
;--------------------------------------
MAIN:
MOVP1, #00H
MOV P3, #3FH
MOV P2, #00H
MOV P0, #00H
MOV TMOD, #02H
MOV TH0,#6
MOV IE, #87H
SETB TR0
CALL CT1
CALL CT2
CALL CT3
CALL CT4
JMP MAIN
;---------------------------
MAINLOOP:
MOV A, R2
MOV B, #10
DIV AB
MOV DPTR, #LEDCODE
MOVC A, @A+DPTR
MOV P2, A
SETB P1.2
CALL DELAY
CLR P1.2
MOV A, B
MOVC A, @A+DPTR
MOV P2, A
SETB P1.3
CALL DELAY
CLR P1.3
MOV A, R3
MOV B, #10
DIV AB
MOV DPTR, #LEDCODE
MOVC A, @A+DPTR
MOV P0, A
SETB P1.0
CALL DELAY
CLR P1.0
MOV A, B
MOVC A, @A+DPTR
MOV P0, A
SETB P1.1
CALL DELAY
CLR P1.1
JMP MAINLOOP
;------------------------------
LEDCODE: DB 40H, 79H, 24H, 30H, 19H, 12H, 02H, 78H, 00H, 10H
;------------------------------
DELAY:
MOV R4, #2
DELAY1:
DJNZ R4, DELAY1
RET
END
Comment