一、实验原理
使用8253对1MHz时钟进行1M分频,从而实现1s精确定时。
使用8255A口控制指示灯,8255B口控制数码管倒计时,8255C口0-1控制数码管片选,7口对1s时钟进行读取。
二、连线
8253的CLK0接1MHz时钟源,OUT0接CLK1,OUT1接8255的PC7,GATE0、GATE1接电源+5V。
8255的PA0、PA1、PA2分别接路口1的绿、黄、红灯,PA5、PA6、PA7分别接路口2的绿、黄、红灯,
PB0-PB6接七段数码管的a-g口,PC0、PC1分别接数码管高、低位片选。
8253片选接280h,8255片选接288h。
三、汇编程序
1.精确延时子函数
在调用该delay子函数时,传入参数cx为需要延时的时间,单位秒。在延时过程中,使用数码管显示距离延时结束剩余的时间。该函数应该放在code段里,以下为函数代码:
delay proc near
push ax
push bx
push dx
cont: mov ax,cx
push cx
mov bx,10
div bl
mov bl,al
mov dx,io8255c
mov al,1
out dx,al
mov al,[bx]
mov dx,io8255b
out dx,al
mov cx,0ffffh
d1: loop d1
mov cx,0ffffh
d2: loop d2
mov bl,ah
mov dx,io8255c
mov al,2
out dx,al
mov al,[bx]
mov dx,io8255b
out dx,al
mov cx,0ffffh
d3: loop d3
mov cx,0ffffh
d4: loop d4
pop cx
mov dx,io8255c
in al,dx
and al,80h
mov ah,temp
mov temp,al
cmp ah,al
jz cont
cmp ah,80h
jnz cont
loop cont
pop dx
pop bx
pop ax
ret
delay endp
2.主函数
绿灯时间60s,黄灯时间3s,红灯时间63s。
data segment
ioport equ 0ff00h-0280h
io8253a equ ioport+280h
io8253b equ ioport+281h
io8253c equ ioport+282h
io8253d equ ioport+283h
io8255a equ ioport+288h
io8255b equ ioport+289h
io8255c equ ioport+28ah
io8255d equ ioport+28bh
ta db 3fh,06h,5bh,4fh,66h,6dh,7dh,07h,7fh,6fh
temp db 0
data ends
code segment
assume ds:data,cs:code
start: mov ax,data
mov ds,ax
mov dx,io8253d
mov al,36h
out dx,al
mov dx,io8253a
mov al,50h
out dx,al
mov al,0c3h
out dx,al
mov dx,io8253d
mov al,56h
out dx,al
mov dx,io8253b
mov al,20
out dx,al
mov dx,io8255d
mov al,88h
out dx,al
mov dx,io8255a
lo: mov al,24h
out dx,al
mov cx,60
call delay
mov al,44h
out dx,al
mov cx,3
call delay
mov al,81h
out dx,al
mov cx,60
call delay
mov al,82h
out dx,al
mov cx,3
call delay
jmp lo
code ends
end start