;Khai bao port va bit
P_LED DATA P0 ;port du lieu LED7seg
P_ADD DATA P2 ;port dia chi LED7seg
P_LIGHT DATA P1 ;port tin hieu den
;PORT_p1
RED1 BIT P1.0 ;thu tu cac LED don
YELLOW1 BIT P1.1
GREEN1 BIT P1.2
RED2 BIT P1.3
YELLOW2 BIT P1.4
GREEN2 BIT P1.5
;PORT_p1
LED1A_ADD BIT P2.0 ;thu tu cac LED7seg
LED1B_ADD BIT P2.1
LED2A_ADD BIT P2.2
LED2B_ADD BIT P2.3
;------------------------------------------------
;Khai bao cac bien
BSEG AT 10H
DSEG AT 20H
;Bien gia tri cac LED
LED1: DS 1 ;gia tri LED1
LED2: DS 1 ;gia tri LED2
;Bien quet LED
LOOP_VALUE: DS 1 ;so vong lap quet LED
;Bien HEX2DEC
CHUC: DS 1 ;so hang chuc
DONVI: DS 1 ;so hang don vi
CSEG AT 00H
;------------------------------------------------------
ORG 0000h
LJMP MAIN
;-------------------------------------------------------
;Khai bao cac hang so
RED_TIME EQU 33;thoi gian den do 33s
YELLOW_TIME EQU 03;thoi gian den vang 3s
GREEN_TIME EQU 30;thoi gian den xanh 30s
;------------------------------------------------------
;Chuong trinh chinh
MAIN:
;Khoi tao cac port
MOV P2,#0ffh
MOV P0,#0ffh
MOV P1,#0ffh
;Khoi tao gia tri ban dau cua cac bien
MOV LED1,#0
MOV LED2,#0
MOV LOOP_VALUE,#25 ;so vong lap quet LED
MOV CHUC,#0
MOV DONVI,#0
;Vong lap chinh
MAIN_LOOP:
;LED1: Do 33 LED2:Xanh 30
MOV LED1,#33;nap thoi gian ban dau
MOV LED2,#30
setb RED1 ; bat den do, den xanh
setb GREEN2
LOOP1A: ;giam gia tri thoi gian
CALL SCANLED
DEC LED1
DJNZ LED2,LOOP1A
clr GREEN2 ;tat den xanh
;LED1: Do 30 LED2:Vang 30
MOV LED2,#3 ; nap thoi gian ban dau
setb YELLOW2 ;giu den do, bat den vang
LOOP1B: ;giam gia tri thoi gian
CALL SCANLED
DEC LED1
DJNZ LED2,LOOP1B
clr RED1 ;tat den do
clr YELLOW2 ;tat den vang
;LED1:Xanh 0-24 LED2: Do 33-3
MOV LED2,#33 ; thoi gian ban dau
MOV LED1,#30
setb RED2 ; bat den do, den xanh
setb GREEN1
LOOP2A: ;giam gia tri thoi gian
CALL SCANLED
DEC LED2
DJNZ LED1,LOOP2A
clr GREEN1 ;tat den xanh
;LED1: Vang 4-0 LED2: Do 4-0
MOV LED1,#3 ; nap thoi gian ban dau
setb YELLOW1 ;giu den do, bat den vang
LOOP2B: ;giam gia tri thoi gian
CALL SCANLED
DEC LED2
DJNZ LED1,LOOP2B
clr RED2 ;tat den do
clr YELLOW1 ;tat den vang
LJMP MAIN_LOOP ;vong lap lien tuc
;------------------------------------------------------
;Chuong trinh quet LED
SCANLED:
;LED 1
MOV A,LED1 ;nap gia tri LED1
CALL HEX2DEC ;tach so
MOV A,CHUC ;hang chuc LED1
CALL CODE7SEG
MOV P_LED,A
clr LED1A_ADD
CALL DELAY01ms_1
setb LED1A_ADD
MOV A,DONVI ;quet hang don vi LED1
CALL CODE7SEG
MOV P_LED,A
clr LED1B_ADD
CALL DELAY01ms_1
setb LED1B_ADD
;LED2
MOV A,LED2 ;nap gia tri LED2
CALL HEX2DEC ;tach so
MOV A,CHUC ;quet hang chuc LED2
CALL CODE7SEG
MOV P_LED,A
clr LED2A_ADD
CALL DELAY01ms_1
setb LED2A_ADD
MOV A,DONVI ;quet hang don vi LED2
CALL CODE7SEG
MOV P_LED,A
clr LED2B_ADD
CALL DELAY01ms_1
setb LED2B_ADD
DJNZ LOOP_VALUE,SCANLED
MOV LOOP_VALUE,#25
RET
;-------------------------------------------------------
;Chuong trinh HEX to DEC - Tach so ra thanh dang CHUC-DONVI
;Mo ta: - Dua gia tri can tach vao A - Ket qua CHUC va DONVI
HEX2DEC:
MOV B,#10
DIV AB
MOV CHUC,A
MOV DONVI,B
RET
;-------------------------------------------------------
;Chuong trinh hien thi LED7seg
;Mo ta: Dua gia tri can quet vao A - Ket qua tra ve A
CODE7SEG:
MOV DPTR,#TBL
MOVC A,@A+DPTR
RET
TBL: DB 0C0H,0F9H,0A4H,0B0H,099H,092H,082H,0F8H,080H,090H ;anode chung
;--------------------------------------------------------
;Chuong trinh delay 10ms
DELAY01:
mov r1,#20h
DELAY01MS_1:
MOV R2,#100h
DJNZ R2,$
DJNZ R1,DELAY01MS_1
RET
;------------------------------------------------------------
END
P_LED DATA P0 ;port du lieu LED7seg
P_ADD DATA P2 ;port dia chi LED7seg
P_LIGHT DATA P1 ;port tin hieu den
;PORT_p1
RED1 BIT P1.0 ;thu tu cac LED don
YELLOW1 BIT P1.1
GREEN1 BIT P1.2
RED2 BIT P1.3
YELLOW2 BIT P1.4
GREEN2 BIT P1.5
;PORT_p1
LED1A_ADD BIT P2.0 ;thu tu cac LED7seg
LED1B_ADD BIT P2.1
LED2A_ADD BIT P2.2
LED2B_ADD BIT P2.3
;------------------------------------------------
;Khai bao cac bien
BSEG AT 10H
DSEG AT 20H
;Bien gia tri cac LED
LED1: DS 1 ;gia tri LED1
LED2: DS 1 ;gia tri LED2
;Bien quet LED
LOOP_VALUE: DS 1 ;so vong lap quet LED
;Bien HEX2DEC
CHUC: DS 1 ;so hang chuc
DONVI: DS 1 ;so hang don vi
CSEG AT 00H
;------------------------------------------------------
ORG 0000h
LJMP MAIN
;-------------------------------------------------------
;Khai bao cac hang so
RED_TIME EQU 33;thoi gian den do 33s
YELLOW_TIME EQU 03;thoi gian den vang 3s
GREEN_TIME EQU 30;thoi gian den xanh 30s
;------------------------------------------------------
;Chuong trinh chinh
MAIN:
;Khoi tao cac port
MOV P2,#0ffh
MOV P0,#0ffh
MOV P1,#0ffh
;Khoi tao gia tri ban dau cua cac bien
MOV LED1,#0
MOV LED2,#0
MOV LOOP_VALUE,#25 ;so vong lap quet LED
MOV CHUC,#0
MOV DONVI,#0
;Vong lap chinh
MAIN_LOOP:
;LED1: Do 33 LED2:Xanh 30
MOV LED1,#33;nap thoi gian ban dau
MOV LED2,#30
setb RED1 ; bat den do, den xanh
setb GREEN2
LOOP1A: ;giam gia tri thoi gian
CALL SCANLED
DEC LED1
DJNZ LED2,LOOP1A
clr GREEN2 ;tat den xanh
;LED1: Do 30 LED2:Vang 30
MOV LED2,#3 ; nap thoi gian ban dau
setb YELLOW2 ;giu den do, bat den vang
LOOP1B: ;giam gia tri thoi gian
CALL SCANLED
DEC LED1
DJNZ LED2,LOOP1B
clr RED1 ;tat den do
clr YELLOW2 ;tat den vang
;LED1:Xanh 0-24 LED2: Do 33-3
MOV LED2,#33 ; thoi gian ban dau
MOV LED1,#30
setb RED2 ; bat den do, den xanh
setb GREEN1
LOOP2A: ;giam gia tri thoi gian
CALL SCANLED
DEC LED2
DJNZ LED1,LOOP2A
clr GREEN1 ;tat den xanh
;LED1: Vang 4-0 LED2: Do 4-0
MOV LED1,#3 ; nap thoi gian ban dau
setb YELLOW1 ;giu den do, bat den vang
LOOP2B: ;giam gia tri thoi gian
CALL SCANLED
DEC LED2
DJNZ LED1,LOOP2B
clr RED2 ;tat den do
clr YELLOW1 ;tat den vang
LJMP MAIN_LOOP ;vong lap lien tuc
;------------------------------------------------------
;Chuong trinh quet LED
SCANLED:
;LED 1
MOV A,LED1 ;nap gia tri LED1
CALL HEX2DEC ;tach so
MOV A,CHUC ;hang chuc LED1
CALL CODE7SEG
MOV P_LED,A
clr LED1A_ADD
CALL DELAY01ms_1
setb LED1A_ADD
MOV A,DONVI ;quet hang don vi LED1
CALL CODE7SEG
MOV P_LED,A
clr LED1B_ADD
CALL DELAY01ms_1
setb LED1B_ADD
;LED2
MOV A,LED2 ;nap gia tri LED2
CALL HEX2DEC ;tach so
MOV A,CHUC ;quet hang chuc LED2
CALL CODE7SEG
MOV P_LED,A
clr LED2A_ADD
CALL DELAY01ms_1
setb LED2A_ADD
MOV A,DONVI ;quet hang don vi LED2
CALL CODE7SEG
MOV P_LED,A
clr LED2B_ADD
CALL DELAY01ms_1
setb LED2B_ADD
DJNZ LOOP_VALUE,SCANLED
MOV LOOP_VALUE,#25
RET
;-------------------------------------------------------
;Chuong trinh HEX to DEC - Tach so ra thanh dang CHUC-DONVI
;Mo ta: - Dua gia tri can tach vao A - Ket qua CHUC va DONVI
HEX2DEC:
MOV B,#10
DIV AB
MOV CHUC,A
MOV DONVI,B
RET
;-------------------------------------------------------
;Chuong trinh hien thi LED7seg
;Mo ta: Dua gia tri can quet vao A - Ket qua tra ve A
CODE7SEG:
MOV DPTR,#TBL
MOVC A,@A+DPTR
RET
TBL: DB 0C0H,0F9H,0A4H,0B0H,099H,092H,082H,0F8H,080H,090H ;anode chung
;--------------------------------------------------------
;Chuong trinh delay 10ms
DELAY01:
mov r1,#20h
DELAY01MS_1:
MOV R2,#100h
DJNZ R2,$
DJNZ R1,DELAY01MS_1
RET
;------------------------------------------------------------
END
Comment