$MOD51
GIAY EQU R2
PHUT EQU R3
GIO EQU R4
B_GIAY EQU R7
B_MODE EQU R6
MODE BIT P3.0
UP BIT P3.1
D0WN BIT P3.2
ORG 000H
LJMP MAIN
ORG 00BH
LJMP ISR_T0
MAIN:
MOV TMOD,#01H ;CHE DO TIMER - MODE 1
MOV TH0,#3CH ;THOI GIAN TIMER - 50MS
MOV TL0,#0B0H
CLR TF0 ;XOA CO TRAN
SETB TR0 ;CHAY TIMER
MOV IE,#82H ;CHO PHEP NGAT TIMER0
MOV B_MODE,#0
TD:
MOV GIO,#0 ;NAP GIA TRI GIO BAN DAU
X4:
MOV PHUT,#0 ;NAP GIA TRI PHUT BAN DAU
X3:
MOV GIAY,#0 ;NAP GIA TRI GIAY BAN DAU
MOV B_MODE,#0
X2:
MOV B_GIAY,#0 ;NAP GIA TRI BAN DAU CHO BIEN DEM SO LAN NGAT TIMER
LCALL HEX_BCD ;DOI GIA TRI THOI GIAN -> BCD -> MA 7 DOAN
LCALL BCD_7DOAN
X1:
LCALL HIENTHI ;HIEN THI THOI GIAN
JNB MODE,ADJ ;KIEM TRA NUT MODE - DUNG -> PHAN CHINH THOI GIAN ADJ
CJNE B_GIAY,#20,X1 ;KIEM TRA SO LAN NGAT TIMER - 20 LAN x 50MS = 1S
INC GIAY ;TANG GIAY
CJNE GIAY,#60,X2 ;SO SANH DU 60 GIAY - CHUA DU -> QUAY LAI HIEN THI
INC PHUT ;TANG PHUT
CJNE PHUT,#60,X3 ;SO SANH DU 60 PHUT - CHUA DU -> QUAY LAI HIEN THI
INC GIO ;TANG GIO
CJNE GIO,#24,X4 ;SO SANH DU 60 GI0 - CHUA DU -> QUAY LAI HIEN THI
SJMP TD ;DU -> QUAY LAI TU DAU
;************************************************* **
;CHUONG TRINH CON DOI HEX -> BCD (TACH SO HEX)
;************************************************* **
;R2 -> 11H & 10H, R3 -> 13H & 12H, R4 -> 15H & 14H
;************************************************* **
HEX_BCD:
MOV R0,#02 ;NAP DIA CHI DAU CUA VUNG NHO CHUA SO HEX
MOV R1,#10H ;NAP DIA CHI DAU CUA VUNG NHO CHUA SO BCD
H1:
MOV B,#10 ;
MOV A,@R0 ;LAY SO HEX CAN CHUYEN DOI
DIV AB ;CHIA 10 DE TACH RIENG BYTE THAP VA BYTE CAO
MOV @R1,B ;CAT BYTE THAP
INC R1 ;TANG DIA CHI VUNG NHO CHUA SO BCD
MOV @R1,A ;CAT BYTE CAO
INC R0 ;TANG DIA CHI VUNG NHO CHUA SO HEX
INC R1 ;TANG DIA CHI VUNG NHO CHUA SO BCD
CJNE R0,#05H,H1 ;KIEM TRA HOAN TAT VIEC CHUYEN DOI
RET
;************************************************* **
;CHUONG TRINH CON DOI SO BCD -> MA 7 DOAN
;************************************************* **
;SO BCD (10H - 15H) -> MA 7 DOAN (20H - 25H)
;************************************************* **
BCD_7DOAN:
MOV DPTR,#200H ;NAP DIA CHI VUNG CHUA MA 7 DOAN
MOV R0,#10H ;NAP DIA CHI DAU TIEN CUA VUNG NHO CHUA SO BCD
MOV R1,#20H ;NAP DIA CHI DAU TIEN CUA VUNG NHO CHUA MA 7 DOAN
B1:
MOV A,@R0 ;GIAI MA
MOVC A,@A+DPTR
MOV @R1,A
INC R0 ;TANG DIA CHI VUNG NHO CHUA SO BCD
INC R1 ;TANG DIA CHI VUNG NHO CHAU MA 7 DOAN
CJNE R0,#16H,B1 ;KEM TRA HOAN TAT VIEC GIAI MA
RET
;************************************************* **
;CHUONG TRINH CON HIEN THI (20H - 25H)
;************************************************* **
HIENTHI:
MOV R0,#20H ;DIA CHI DAU TIEN CUA VUNG CHUA DU LIEU CAN HIEN THI
MOV A,#0FEH ;MA QUET DAU TIEN
HT1:
MOV P0,@R0 ;LAY DU LIEU CAN HIEN THI
MOV P1,A ;XUAT MA QUET
LCALL DELAY
MOV P1,#0FFH ;TAT LED DE CHONG LEM
LCALL DELAY
INC R0 ;CHUYEN SANG DIA DIA KE TIEP
RL A ;THAY DOI MA QUET
CJNE A,#0BFH,HT1 ;KIEM TRA QUET XONG CHUA - CHUA XONG -> QUAY VE LAM TIEP
RET ;TRO LAI KHI DA QUET VA HIEN THI XONG
;************************************************* **
ISR_T0: ;ISR TIMER0 - 50MS
CLR TR0
CLR TF0
MOV TH0,#3CH
MOV TL0,#0B0H
INC B_GIAY ;TANG BIEN DEM SO LAN NGAT - 1 LAN = 50MS
SETB TR0
RETI
;************************************************* **
ADJ: ;CHUONG TRINH CON XU LY NUT MODE
LCALL HIENTHI
JNB MODE,ADJ ;KIEM TRA NHU MODE DA DUOC THA RA HAY CHUA
INC B_MODE ;TANG SO LAN NHAN NUT MODE
CJNE B_MODE,#4,ADJ_GPG ;KIEM TRA SO LAN NHAT NUT MODE
MOV B_MODE,#0 ;DU 3 LAN -> TRO LAI TU DAU
MOV B_GIAY,#0
SJMP X2
;************************************************* **
ADJ_GPG:
LOOP1:
MOV 76H,#10 ;SO LAN CHOP TAT CAP LED DANG DUOC CHINH
LOOP2:
MOV 77H,#0FFH ;SO LAN KIEM TRA NUT NHAN
LCALL HEX_BCD
LCALL BCD_7DOAN
LOOP:
LCALL HIENTHI
CJNE B_GIAY,#20,Z1 ;KIEM TRA SO LAN NGAT - CHUA DU THI KIEM TRA NUT NHAN
LJMP TANGGIAY ;DU 20 LAN NGAT -> TANG GIAY
Z1:
JNB UP,TANG ;KIEM TRA NUT UP
JNB D0WN,GIAM ;KIEM TRA NUT DOWN
JNB MODE,ADJ ;KIEM TRA NUT MODE
DJNZ 77H,LOOP ;GIAM SO LAN KIEM TRA NUT NHAN
MOV 77H,#0FFH ;NAP SO LAN TAT (THOI GIAN TAT)
CJNE B_MODE,#1,SS11 ;KIEM TRA NHAN MODE 1 LAN - SAI -> KIEM TRA TIEP SS11
MOV 24H,#0FFH ;DUNG - NHAN MODE 1 LAN -> NAP GIA TRI TAT LED...
MOV 25H,#0FFH ;CHO PHAN "GIO"
SJMP TAT ;HIEN THI TRANG THAI LED
SS11:
CJNE B_MODE,#2,SS12 ;KIEM TRA NHAN MODE 2 LAN - SAI -> KIEM TRA TIEP SS12
MOV 22H,#0FFH ;DUNG - NHAN MODE 2 LAN -> NAP GIA TRI TAT LED...
MOV 23H,#0FFH ;CHO PHAN "PHUT"
SJMP TAT ;HIEN THI TRANG THAI LED
SS12: ;NHAN MODE 3 LAN -> NAP GIA TRI TAT LED...
MOV 20H,#0FFH ;CHO PHAN "GIAY"
MOV 21H,#0FFH
TAT:
LCALL HIENTHI ;HIEN THI
CJNE B_GIAY,#20,Z2 ;KIEM TRA SO LAN NGAT - CHUA DU THI KIEM TRA NUT NHAN
LJMP TANGGIAY ;DU 20 LAN NGAT -> TANG GIAY
Z2:
JNB UP,TANG ;KIEM TRA NUT UP
JNB D0WN,GIAM ;KIEM TRA NUT DOWN
JNB MODE,ADJ ;KIEM TRA NUT MODE
DJNZ 77H,TAT ;GIAM SO LAN TAT (THOI GIAN TAT)
DJNZ 76H,LOOP2 ;GIAM SO LAN CHOP TAT CAP LED
MOV B_MODE,#0 ;XOA SO LAN NHAN NUT MODE
LJMP X2 ;QUAY LAI
TANG:
LCALL HIENTHI
CJNE B_GIAY,#20,Z3 ;KIEM TRA SO LAN NGAT - CHUA DU THI KIEM TRA NHU NHAN
LJMP TANGGIAY ;DU 20 LAN NGAT -> TANG GIAY
Z3:
JNB UP,TANG
CJNE B_MODE,#1,SS1
INC GIO
CJNE GIO,#24,LOOP1
MOV GIO,#0
LOOP11:
LJMP LOOP1
SS1:
CJNE B_MODE,#2,SS2
INC PHUT
CJNE PHUT,#60,LOOP1
MOV PHUT,#0
SJMP LOOP1
SS2:
INC GIAY
CJNE GIAY,#60,Z5
MOV GIAY,#0
Z5:
LJMP LOOP1
GIAM:
LCALL HIENTHI
CJNE B_GIAY,#20,Z4 ;KIEM TRA SO LAN NGAT - CHUA DU THI KIEM TRA NUT NHAN
LJMP TANGGIAY ;DU 20 LAN NGAT -> TANG GIAY
Z4:
JNB D0WN,GIAM
CJNE B_MODE,#01,SS3
DEC GIO
CJNE GIO,#0FFH,K1
MOV GIO,#23
K1:
LJMP LOOP1
SS3:
CJNE B_MODE,#02,SS4
DEC PHUT
CJNE PHUT,#0FFH,K2
MOV PHUT,#59
K2:
LJMP LOOP1
SS4:
DEC GIAY
CJNE GIAY,#0FFH,K3
MOV GIAY,#59
K3:
LJMP LOOP1
;************************************************* **
TANGGIAY:
MOV B_GIAY,#0
INC GIAY
CJNE GIAY,#60,EXIT
MOV GIAY,#0
INC PHUT
CJNE PHUT,#60,EXIT
MOV PHUT,#0
EXIT:
LJMP LOOP
;************************************************* **
DELAY: ;CHUONG TRINH CON TAO THOI GIAN TRE 150US
MOV 7FH,#50H
DJNZ 7FH,$
RET
;************************************************* **
ORG 200H ;VUNG MA 7 DOAN
DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
END
Đây là code của đồng hồ số. em dùng thạch anh 11.0592 mà nó chạy chậm hơn đồng hồ điên thoại, Cứ 1 phút nó chạy chậm hơn đồng hồ điện thoại 5s.Anh nào biết sửa hộ em cho nó chạy nhanh hơn 1 tý ko.cảm ơn các bạn nhiều
GIAY EQU R2
PHUT EQU R3
GIO EQU R4
B_GIAY EQU R7
B_MODE EQU R6
MODE BIT P3.0
UP BIT P3.1
D0WN BIT P3.2
ORG 000H
LJMP MAIN
ORG 00BH
LJMP ISR_T0
MAIN:
MOV TMOD,#01H ;CHE DO TIMER - MODE 1
MOV TH0,#3CH ;THOI GIAN TIMER - 50MS
MOV TL0,#0B0H
CLR TF0 ;XOA CO TRAN
SETB TR0 ;CHAY TIMER
MOV IE,#82H ;CHO PHEP NGAT TIMER0
MOV B_MODE,#0
TD:
MOV GIO,#0 ;NAP GIA TRI GIO BAN DAU
X4:
MOV PHUT,#0 ;NAP GIA TRI PHUT BAN DAU
X3:
MOV GIAY,#0 ;NAP GIA TRI GIAY BAN DAU
MOV B_MODE,#0
X2:
MOV B_GIAY,#0 ;NAP GIA TRI BAN DAU CHO BIEN DEM SO LAN NGAT TIMER
LCALL HEX_BCD ;DOI GIA TRI THOI GIAN -> BCD -> MA 7 DOAN
LCALL BCD_7DOAN
X1:
LCALL HIENTHI ;HIEN THI THOI GIAN
JNB MODE,ADJ ;KIEM TRA NUT MODE - DUNG -> PHAN CHINH THOI GIAN ADJ
CJNE B_GIAY,#20,X1 ;KIEM TRA SO LAN NGAT TIMER - 20 LAN x 50MS = 1S
INC GIAY ;TANG GIAY
CJNE GIAY,#60,X2 ;SO SANH DU 60 GIAY - CHUA DU -> QUAY LAI HIEN THI
INC PHUT ;TANG PHUT
CJNE PHUT,#60,X3 ;SO SANH DU 60 PHUT - CHUA DU -> QUAY LAI HIEN THI
INC GIO ;TANG GIO
CJNE GIO,#24,X4 ;SO SANH DU 60 GI0 - CHUA DU -> QUAY LAI HIEN THI
SJMP TD ;DU -> QUAY LAI TU DAU
;************************************************* **
;CHUONG TRINH CON DOI HEX -> BCD (TACH SO HEX)
;************************************************* **
;R2 -> 11H & 10H, R3 -> 13H & 12H, R4 -> 15H & 14H
;************************************************* **
HEX_BCD:
MOV R0,#02 ;NAP DIA CHI DAU CUA VUNG NHO CHUA SO HEX
MOV R1,#10H ;NAP DIA CHI DAU CUA VUNG NHO CHUA SO BCD
H1:
MOV B,#10 ;
MOV A,@R0 ;LAY SO HEX CAN CHUYEN DOI
DIV AB ;CHIA 10 DE TACH RIENG BYTE THAP VA BYTE CAO
MOV @R1,B ;CAT BYTE THAP
INC R1 ;TANG DIA CHI VUNG NHO CHUA SO BCD
MOV @R1,A ;CAT BYTE CAO
INC R0 ;TANG DIA CHI VUNG NHO CHUA SO HEX
INC R1 ;TANG DIA CHI VUNG NHO CHUA SO BCD
CJNE R0,#05H,H1 ;KIEM TRA HOAN TAT VIEC CHUYEN DOI
RET
;************************************************* **
;CHUONG TRINH CON DOI SO BCD -> MA 7 DOAN
;************************************************* **
;SO BCD (10H - 15H) -> MA 7 DOAN (20H - 25H)
;************************************************* **
BCD_7DOAN:
MOV DPTR,#200H ;NAP DIA CHI VUNG CHUA MA 7 DOAN
MOV R0,#10H ;NAP DIA CHI DAU TIEN CUA VUNG NHO CHUA SO BCD
MOV R1,#20H ;NAP DIA CHI DAU TIEN CUA VUNG NHO CHUA MA 7 DOAN
B1:
MOV A,@R0 ;GIAI MA
MOVC A,@A+DPTR
MOV @R1,A
INC R0 ;TANG DIA CHI VUNG NHO CHUA SO BCD
INC R1 ;TANG DIA CHI VUNG NHO CHAU MA 7 DOAN
CJNE R0,#16H,B1 ;KEM TRA HOAN TAT VIEC GIAI MA
RET
;************************************************* **
;CHUONG TRINH CON HIEN THI (20H - 25H)
;************************************************* **
HIENTHI:
MOV R0,#20H ;DIA CHI DAU TIEN CUA VUNG CHUA DU LIEU CAN HIEN THI
MOV A,#0FEH ;MA QUET DAU TIEN
HT1:
MOV P0,@R0 ;LAY DU LIEU CAN HIEN THI
MOV P1,A ;XUAT MA QUET
LCALL DELAY
MOV P1,#0FFH ;TAT LED DE CHONG LEM
LCALL DELAY
INC R0 ;CHUYEN SANG DIA DIA KE TIEP
RL A ;THAY DOI MA QUET
CJNE A,#0BFH,HT1 ;KIEM TRA QUET XONG CHUA - CHUA XONG -> QUAY VE LAM TIEP
RET ;TRO LAI KHI DA QUET VA HIEN THI XONG
;************************************************* **
ISR_T0: ;ISR TIMER0 - 50MS
CLR TR0
CLR TF0
MOV TH0,#3CH
MOV TL0,#0B0H
INC B_GIAY ;TANG BIEN DEM SO LAN NGAT - 1 LAN = 50MS
SETB TR0
RETI
;************************************************* **
ADJ: ;CHUONG TRINH CON XU LY NUT MODE
LCALL HIENTHI
JNB MODE,ADJ ;KIEM TRA NHU MODE DA DUOC THA RA HAY CHUA
INC B_MODE ;TANG SO LAN NHAN NUT MODE
CJNE B_MODE,#4,ADJ_GPG ;KIEM TRA SO LAN NHAT NUT MODE
MOV B_MODE,#0 ;DU 3 LAN -> TRO LAI TU DAU
MOV B_GIAY,#0
SJMP X2
;************************************************* **
ADJ_GPG:
LOOP1:
MOV 76H,#10 ;SO LAN CHOP TAT CAP LED DANG DUOC CHINH
LOOP2:
MOV 77H,#0FFH ;SO LAN KIEM TRA NUT NHAN
LCALL HEX_BCD
LCALL BCD_7DOAN
LOOP:
LCALL HIENTHI
CJNE B_GIAY,#20,Z1 ;KIEM TRA SO LAN NGAT - CHUA DU THI KIEM TRA NUT NHAN
LJMP TANGGIAY ;DU 20 LAN NGAT -> TANG GIAY
Z1:
JNB UP,TANG ;KIEM TRA NUT UP
JNB D0WN,GIAM ;KIEM TRA NUT DOWN
JNB MODE,ADJ ;KIEM TRA NUT MODE
DJNZ 77H,LOOP ;GIAM SO LAN KIEM TRA NUT NHAN
MOV 77H,#0FFH ;NAP SO LAN TAT (THOI GIAN TAT)
CJNE B_MODE,#1,SS11 ;KIEM TRA NHAN MODE 1 LAN - SAI -> KIEM TRA TIEP SS11
MOV 24H,#0FFH ;DUNG - NHAN MODE 1 LAN -> NAP GIA TRI TAT LED...
MOV 25H,#0FFH ;CHO PHAN "GIO"
SJMP TAT ;HIEN THI TRANG THAI LED
SS11:
CJNE B_MODE,#2,SS12 ;KIEM TRA NHAN MODE 2 LAN - SAI -> KIEM TRA TIEP SS12
MOV 22H,#0FFH ;DUNG - NHAN MODE 2 LAN -> NAP GIA TRI TAT LED...
MOV 23H,#0FFH ;CHO PHAN "PHUT"
SJMP TAT ;HIEN THI TRANG THAI LED
SS12: ;NHAN MODE 3 LAN -> NAP GIA TRI TAT LED...
MOV 20H,#0FFH ;CHO PHAN "GIAY"
MOV 21H,#0FFH
TAT:
LCALL HIENTHI ;HIEN THI
CJNE B_GIAY,#20,Z2 ;KIEM TRA SO LAN NGAT - CHUA DU THI KIEM TRA NUT NHAN
LJMP TANGGIAY ;DU 20 LAN NGAT -> TANG GIAY
Z2:
JNB UP,TANG ;KIEM TRA NUT UP
JNB D0WN,GIAM ;KIEM TRA NUT DOWN
JNB MODE,ADJ ;KIEM TRA NUT MODE
DJNZ 77H,TAT ;GIAM SO LAN TAT (THOI GIAN TAT)
DJNZ 76H,LOOP2 ;GIAM SO LAN CHOP TAT CAP LED
MOV B_MODE,#0 ;XOA SO LAN NHAN NUT MODE
LJMP X2 ;QUAY LAI
TANG:
LCALL HIENTHI
CJNE B_GIAY,#20,Z3 ;KIEM TRA SO LAN NGAT - CHUA DU THI KIEM TRA NHU NHAN
LJMP TANGGIAY ;DU 20 LAN NGAT -> TANG GIAY
Z3:
JNB UP,TANG
CJNE B_MODE,#1,SS1
INC GIO
CJNE GIO,#24,LOOP1
MOV GIO,#0
LOOP11:
LJMP LOOP1
SS1:
CJNE B_MODE,#2,SS2
INC PHUT
CJNE PHUT,#60,LOOP1
MOV PHUT,#0
SJMP LOOP1
SS2:
INC GIAY
CJNE GIAY,#60,Z5
MOV GIAY,#0
Z5:
LJMP LOOP1
GIAM:
LCALL HIENTHI
CJNE B_GIAY,#20,Z4 ;KIEM TRA SO LAN NGAT - CHUA DU THI KIEM TRA NUT NHAN
LJMP TANGGIAY ;DU 20 LAN NGAT -> TANG GIAY
Z4:
JNB D0WN,GIAM
CJNE B_MODE,#01,SS3
DEC GIO
CJNE GIO,#0FFH,K1
MOV GIO,#23
K1:
LJMP LOOP1
SS3:
CJNE B_MODE,#02,SS4
DEC PHUT
CJNE PHUT,#0FFH,K2
MOV PHUT,#59
K2:
LJMP LOOP1
SS4:
DEC GIAY
CJNE GIAY,#0FFH,K3
MOV GIAY,#59
K3:
LJMP LOOP1
;************************************************* **
TANGGIAY:
MOV B_GIAY,#0
INC GIAY
CJNE GIAY,#60,EXIT
MOV GIAY,#0
INC PHUT
CJNE PHUT,#60,EXIT
MOV PHUT,#0
EXIT:
LJMP LOOP
;************************************************* **
DELAY: ;CHUONG TRINH CON TAO THOI GIAN TRE 150US
MOV 7FH,#50H
DJNZ 7FH,$
RET
;************************************************* **
ORG 200H ;VUNG MA 7 DOAN
DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H
END
Đây là code của đồng hồ số. em dùng thạch anh 11.0592 mà nó chạy chậm hơn đồng hồ điên thoại, Cứ 1 phút nó chạy chậm hơn đồng hồ điện thoại 5s.Anh nào biết sửa hộ em cho nó chạy nhanh hơn 1 tý ko.cảm ơn các bạn nhiều
Comment