Dưới đây là mạch mô phỏng và code... ai có thể giải thích giúp em code dưới nói gì... thật cụ thể nhé... em xin cảm ơn !!!
INCLUDE REG_51.PDF
LED1 EQU 126
LED2 EQU 125
Tang BIT P1.0
Giam BIT P1.1
Start BIT P1.2
Stop BIT P1.3
LJMP MAIN
ORG 000BH ; vector ngat timer0
LJMP NGAT_TIMER0
MAIN:
MOV TMOD,#11H ; khai bao ngat timer0
SETB EA ;cho phep ngat toan cuc
SETB ET0 ; khai bao ngat timer0
MOV TH0,#HIGH(-1000) ; cai dat thoi gian ngat la 1ms
MOV TL0,#LOW(-1000) ;-----
SETB TR0 ;-----
MOV R2,#90 ;cai dat gia tri thoi gian delay ban dau : T=90x 1ms
MOV R1,#1 ; gan gi� tri thanh ghi R1=1
CLR P3.0 ; ngo ra p3.0 muc thap
CLR P3.1 ;ngo ra p3.1 muc cao
MOV R3,#1 ;gan gi� tri thanh ghi R3=1 (ban dau khi moi mo nguon thi se o trang thai "STOP")
BEGIN:
JNB Tang,PRO_TANG ;kiem tra phim nhan "Tang", neu co nhan se nhay toi nhan "PRO_TANG"
JNB Giam,PRO_GIAM ;kiem tra phim nhan "Giam", neu co nhan se nhay toi nhan "PRO_GIAM"
JNB Start,PRO_START ;kiem tra phim nhan "Start", neu co nhan se nhay toi nhan "PRO_START"
JNB Stop,PRO_STOP ; kiem tra phim nhan "Stop", neu co nhan se nhay toi nhan "PRO_STOP"
CALL DOI_SO ; goi chuong trinh "DOI_SO"
LJMP BEGIN ; quay tro ve nhan "BEGIN" de lap lai vo han lan
;----chuong trinh khi phim nhan "Tang"
PRO_TANG:
INC R2 ; tang thoi gian delay len 1 don vi (tang len 10ms)
MOV A,R2
MOV R1,A
CALL DELAY
CJNE R2,#100,BEGIN ;kiem tra gia tri thanh ghi R1, neu khong bang 100 thi nhay toi nhan "BEGIN", neu bang 100 thi thuc hien lenh ke tiep
MOV R2,#1 ; gan lai gia tri thanh R1=1 (thoi gian delay 1ms)
LJMP BEGIN ; quay tro ve nhan "BEGIN" de lap lai vo han lan
;--------chuong trinh khi phim nhan "Giam"
PRO_GIAM:
DEC R2 ; giam thoi gian delay xuong 1 don vi (giam xuong 10ms)
MOV A,R2
MOV R1,A
CALL DELAY
CJNE R2,#0,BEGIN ;kiem tra gia tri thanh ghi R1, neu khong bang 0 thi nhay toi nhan "BEGIN", neu bang 0 thi thuc hien lenh ke tiep
MOV R2,#99 ; gan lai gia tri thanh R1=99 (thoi gian delay = 99x 1ms)
LJMP BEGIN ; quay tro ve nhan "BEGIN" de lap lai vo han lan
;---------chuong trinh khi phim nhan "Start"
PRO_START:
MOV R3,#0 ; gan gia tri thanh R3=0
LJMP BEGIN
;---------chuong trinh khi phim nhan "Stop"
PRO_STOP:
MOV R3,#1 ; gan gia tri thanh R3=1
LJMP BEGIN
;-----chuong trinh ngat timer0: dung de tao xung, v� quet led
NGAT_TIMER0:
MOV TH0,#HIGH(-1000)
MOV TL0,#LOW(-1000)
CJNE R3,#0,EXIT ;
DJNZ R1,X2
CPL P3.4 ; dao trang thai chan P3.4 de tao xung
MOV A,R2
MOV R1,A
LJMP X2
EXIT:
CLR P3.4
X2: ; quet led
MOV P2,LED2
SETB P3.1
CALL DELAY_QUETLED
CLR P3.1
MOV P2,LED1
SETB P3.0
CALL DELAY_QUETLED
CLR P3.0
RETI ; thoat khoi ngat
;------ chuong trinh doi so
DOI_SO:
PUSH ACC
PUSH B
PUSH 02
MOV DPTR,#BANG_MA
MOV A,R2
MOV B,#10
DIV AB
MOVC A,@A+DPTR
MOV LED1,A
MOV A,B
MOVC A,@A+DPTR
MOV LED2,A
POP 02
POP B
POP ACC
RET
;-------chuong trinh delay dung de chong doi cho phim nhan
DELAY:
PUSH 05
PUSH 07
MOV R7,#200
X1:
MOV R5,#200
DJNZ R5,$
DJNZ R7,X1
POP 07
POP 05
RET
;-----------chuong trinh delay dung de quet led
DELAY_QUETLED:
PUSH 06
MOV R6,#170
DJNZ R6,$
POP 06
RET
;-------bang ma led 7 doan
BANG_MA: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H ; BANG MA LED ANODE CHUNG
END
INCLUDE REG_51.PDF
LED1 EQU 126
LED2 EQU 125
Tang BIT P1.0
Giam BIT P1.1
Start BIT P1.2
Stop BIT P1.3
LJMP MAIN
ORG 000BH ; vector ngat timer0
LJMP NGAT_TIMER0
MAIN:
MOV TMOD,#11H ; khai bao ngat timer0
SETB EA ;cho phep ngat toan cuc
SETB ET0 ; khai bao ngat timer0
MOV TH0,#HIGH(-1000) ; cai dat thoi gian ngat la 1ms
MOV TL0,#LOW(-1000) ;-----
SETB TR0 ;-----
MOV R2,#90 ;cai dat gia tri thoi gian delay ban dau : T=90x 1ms
MOV R1,#1 ; gan gi� tri thanh ghi R1=1
CLR P3.0 ; ngo ra p3.0 muc thap
CLR P3.1 ;ngo ra p3.1 muc cao
MOV R3,#1 ;gan gi� tri thanh ghi R3=1 (ban dau khi moi mo nguon thi se o trang thai "STOP")
BEGIN:
JNB Tang,PRO_TANG ;kiem tra phim nhan "Tang", neu co nhan se nhay toi nhan "PRO_TANG"
JNB Giam,PRO_GIAM ;kiem tra phim nhan "Giam", neu co nhan se nhay toi nhan "PRO_GIAM"
JNB Start,PRO_START ;kiem tra phim nhan "Start", neu co nhan se nhay toi nhan "PRO_START"
JNB Stop,PRO_STOP ; kiem tra phim nhan "Stop", neu co nhan se nhay toi nhan "PRO_STOP"
CALL DOI_SO ; goi chuong trinh "DOI_SO"
LJMP BEGIN ; quay tro ve nhan "BEGIN" de lap lai vo han lan
;----chuong trinh khi phim nhan "Tang"
PRO_TANG:
INC R2 ; tang thoi gian delay len 1 don vi (tang len 10ms)
MOV A,R2
MOV R1,A
CALL DELAY
CJNE R2,#100,BEGIN ;kiem tra gia tri thanh ghi R1, neu khong bang 100 thi nhay toi nhan "BEGIN", neu bang 100 thi thuc hien lenh ke tiep
MOV R2,#1 ; gan lai gia tri thanh R1=1 (thoi gian delay 1ms)
LJMP BEGIN ; quay tro ve nhan "BEGIN" de lap lai vo han lan
;--------chuong trinh khi phim nhan "Giam"
PRO_GIAM:
DEC R2 ; giam thoi gian delay xuong 1 don vi (giam xuong 10ms)
MOV A,R2
MOV R1,A
CALL DELAY
CJNE R2,#0,BEGIN ;kiem tra gia tri thanh ghi R1, neu khong bang 0 thi nhay toi nhan "BEGIN", neu bang 0 thi thuc hien lenh ke tiep
MOV R2,#99 ; gan lai gia tri thanh R1=99 (thoi gian delay = 99x 1ms)
LJMP BEGIN ; quay tro ve nhan "BEGIN" de lap lai vo han lan
;---------chuong trinh khi phim nhan "Start"
PRO_START:
MOV R3,#0 ; gan gia tri thanh R3=0
LJMP BEGIN
;---------chuong trinh khi phim nhan "Stop"
PRO_STOP:
MOV R3,#1 ; gan gia tri thanh R3=1
LJMP BEGIN
;-----chuong trinh ngat timer0: dung de tao xung, v� quet led
NGAT_TIMER0:
MOV TH0,#HIGH(-1000)
MOV TL0,#LOW(-1000)
CJNE R3,#0,EXIT ;
DJNZ R1,X2
CPL P3.4 ; dao trang thai chan P3.4 de tao xung
MOV A,R2
MOV R1,A
LJMP X2
EXIT:
CLR P3.4
X2: ; quet led
MOV P2,LED2
SETB P3.1
CALL DELAY_QUETLED
CLR P3.1
MOV P2,LED1
SETB P3.0
CALL DELAY_QUETLED
CLR P3.0
RETI ; thoat khoi ngat
;------ chuong trinh doi so
DOI_SO:
PUSH ACC
PUSH B
PUSH 02
MOV DPTR,#BANG_MA
MOV A,R2
MOV B,#10
DIV AB
MOVC A,@A+DPTR
MOV LED1,A
MOV A,B
MOVC A,@A+DPTR
MOV LED2,A
POP 02
POP B
POP ACC
RET
;-------chuong trinh delay dung de chong doi cho phim nhan
DELAY:
PUSH 05
PUSH 07
MOV R7,#200
X1:
MOV R5,#200
DJNZ R5,$
DJNZ R7,X1
POP 07
POP 05
RET
;-----------chuong trinh delay dung de quet led
DELAY_QUETLED:
PUSH 06
MOV R6,#170
DJNZ R6,$
POP 06
RET
;-------bang ma led 7 doan
BANG_MA: DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H ; BANG MA LED ANODE CHUNG
END
Comment