data segment
table db 3fh,06h,5bh,4fh,66h,6dh,7dh,07h,7fh,6fh,00h
data ends
code segment
assume cs:code,ds:data
main proc
start:mov ax,data
mov ds,ax
;
push ds ;初始化中断向量
mov ax,0000h
mov ds,ax
mov ax,offset irq7
add ax,2000h
mov si,003ch
mov [si],ax
mov ax,0000h
mov si,003eh
mov [si],ax
pop ds
in al,21h ;write ocw1,允许irq7
and al,7fh
out 21h,al
;
mov al,81h ;init8255,1000 0001,PA、PB、PC0-PC3输出,PC4-PC7输入
out 63h,al
;
mov al,96h ;init 8253,选择计数器2,工作方式3
out 43h,al
mov al,01h
out 42h,al
mov si,2 ;si=2,南北红灯,东西绿灯;si=1,南北绿灯,东西红灯
again:in al,62h ;通过从PC0-PC1读得的值设置绿灯亮的时间
and al,03h
cmp al,00000001b ;k0~k1=01,绿灯亮20s
jnz c1
mov ah,01h
jmp cc
c1: cmp al,00000010b ;k0~k1=10,绿灯亮30s
jnz c2
mov ah,02h
jmp cc
c2: cmp al,00000011b ;k0~k1=11,绿灯亮60s
jnz c3
mov ah,05h
jmp cc
c3: mov ah,08h ;k0~k1=00,绿灯亮90s
cc: lea bx,table
call green ;调用绿灯亮程序
call flash ;调用绿灯闪3s
call yellow ;调用黄灯亮2s
cmp si,2
jnz goto
dec si
jmp again
goto: inc si
jmp again
;
irq7: push ax ;中断程序
push cx
push dx
mov al,77h
out 60h,al ;red light on
;
mov dh,9 ;设置显示器个位倒计时10s
mov cl,200
Norm_R:dec cl
cmp cl,0
jnz r1
mov cl,200
dec dh
cmp dh,10
ja exit_R ;dh<0,ret
jmp Norm_R
r1: mov al,dh
xlat
out 61h,al
mov al,0bfh
out 62h,al
call delay
jmp Norm_R
exit_R:mov al,20h ;使得irq7可以多次中断????
out 20h,al
pop dx
pop cx
pop ax
iret
main endp
;
green proc
push cx
push dx
;
mov dh,9 ;dh代表个位
mov cl,100
mov ch,10
Norm_G:cmp si,2 ;light on
jnz g0
mov al,7dh
out 60h,al
jmp g3
g0: mov al,0d7h
out 60h,al
g3: dec cl
cmp cl,0
jnz g1 ;cl!=0,即不够1s
mov cl,100 ;cl==0,够1s
dec ch
cmp ch,0
jz g2 ;ch==0,十位-1,个位=9
dec dh ;ch!=0,十位不变,个位-1
;
cmp ah,0
jnz Norm_G
cmp dh,3
jnz Norm_G
jmp exit ;十位=0 and 个位=3时,结束green
g1: mov al,ah
xlat
out 61h,al ;输出字模
mov al,7fh
out 62h,al ;选择数码管
call delay
mov al,dh
xlat
out 61h,al
mov al,0bfh
out 62h,al
call delay
jmp Norm_G
g2: mov ch,10
dec ah
mov al,ah
xlat
out 61h,al
mov al,7fh
out 62h,al
call delay
mov dh,9
mov al,dh
xlat
out 61h,al
mov al,0bfh
out 62h,al
call delay
jmp Norm_G
exit: pop dx
pop cx
ret
green endp
;
flash proc ;green light flash 3S
push ax
push cx
push dx
;
mov cl,60
mov dh,3
Norm_F: dec cl
cmp cl,0
jnz f1
mov cl,60
dec dh
cmp dh,10
ja exit1
jmp Norm_F
f1: cmp cl,20
jb f3
cmp si,2 ;某方向红灯亮,另一方向绿灯亮
jnz f0
mov al,7dh
out 60h,al
jmp f2
f0: mov al,0d7h
out 60h,al
f2: in al,62h ;声响
and al,00001100b
cmp al,00001100b
jnz f6
mov al,0ffh
push cx
mov cx,4000
sss: out 62h,al
loop sss
pop cx
f6: mov al,0 ;数码管显示
xlat
out 61h,al
mov al,4fh
out 62h,al
call delay
mov al,dh
xlat
out 61h,al
mov al,8fh
out 62h,al
call delay
jmp Norm_F
f3: cmp si,2 ;红灯亮,绿灯灭
jnz f4
mov al,7fh
out 60h,al
jmp f5
f4: mov al,0f7h
out 60h,al
f5: mov al,10 ;数码管灭
xlat
out 61h,al
mov al,0fh
out 62h,al
call delay
call delay
jmp Norm_F
exit1: pop dx
pop cx
pop ax
ret
flash endp
yellow proc
push ax
push cx
push dx
mov cl,100
mov dh,2
Norm_Y: cmp si,2 ;某一方向红灯亮,另一方向黄灯亮
jnz y0
mov al,7bh
out 60h,al
jmp y2
y0: mov al,0b7h
out 60h,al
y2: dec cl
cmp cl,0
jnz y1
mov cl,100
dec dh
cmp dh,10
ja exit2
jmp Norm_Y
y1: mov al,0 ;数码管亮
xlat
out 61h,al
mov al,4fh
out 62h,al
call delay
mov al,dh
xlat
out 61h,al
mov al,8fh
out 62h,al
call delay
jmp Norm_Y
exit2: pop dx
pop cx
pop ax
ret
yellow endp
delay proc ;延时程序5ms
push bx
push cx
mov bl,2
next: mov cx,1000
aa: loop aa
dec bl
jnz next
pop cx
pop bx
ret
delay endp
code ends
end start