cpp
复制代码
.MODEL SMALL
.8086
.stack
.code
.startup
NMI_init: push es ;NMI中断向量初始化
xor ax, ax
mov es, ax
mov al, 02h
xor ah, ah
shl ax, 1
shl ax, 1
mov si, ax
mov ax, offset NMI_service
mov es:[si], ax
inc si
inc si
mov bx, cs
mov es:[si], bx
pop es
mov al,10110101b ;T2 16位 mode2 bcd
mov dx,0406h
out dx,al
mov dx,0404h
mov ax,1000h ;clk=100KHz 1000分频 out=100Hz=0.01秒
out dx,al
mov al,ah ;高字节
out dx,al
lop: call key
call disp
jmp lop
disp proc ;显示子程序
mov bl,m_sec
and bx,000fh ;毫秒个位
mov si,bx
mov al,situation[si] ;毫秒个位段码
mov bl,m_sec
and bx,00f0h ;毫秒十位
mov cl,4
shr bx,cl
mov si,bx
mov ah,situation[si] ;毫秒十位段码
mov dx,0000h ;毫秒输出地址
out dx,ax
mov bl,sec
and bx,000fh
mov si,bx
mov al,situation[si]
mov bl,sec
and bx,00f0h
mov cl,4
shr bx,cl
mov si,bx
mov ah,situation[si]
mov dx,0200h ;秒输出地址
out dx,ax
ret
disp endp
key proc near
mov dx,0600h
in al,dx
test al,01h
jnz exitkey
call delay ;消抖
mov dx,0600h
in al,dx
test al,01h
jnz exitkey
inc state
waitkey:in al,dx
test al,01h
jz waitkey ;等待按键释放
cmp state,03h
jb exitkey
mov state,0
exitkey:ret
key endp
;fosc=5MHz,T=0.2us,Td ≈20ms
delay proc near ;延时子程序
push bx
push cx
mov bx,1 ;4
del1: mov cx,5882 ;4
del2: loop del2 ;17/5
dec bx ;2
jnz del1 ;16/4
pop cx
pop bx
ret
delay endp
NMI_service:
push ax
cmp state,0h ;state=0,时钟清0
je clear
cmp state,01h ;state=1,时钟走时
je start
cmp state,02h ;state=2,时钟停止
je stop
jmp exit
clear:mov m_sec,0
mov sec,0
jmp exit
start:mov al,m_sec
add al,1 ;毫秒+1
daa
mov m_sec,al
cmp m_sec,00h
jne exit
mov m_sec,0
mov al,sec
add al,1 ;秒+1
daa
mov sec,al
cmp sec,60h
jb exit
mov sec,0
jmp exit
stop:
exit: pop ax
iret
.data
state db 0;0_clear,1_start,2_stop
m_sec db 00h
sec db 00h
situation db 3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH ;共阴
sit_end=$
END