;定时器0作定时器,定时器1对外部输入脉冲进行计数并把计数值显示于数码管上.
OUTBIT   equ 0e101h  ; 位控制口
CLK164   equ 0e102h  ; 段控制口(接164时钟位)
DAT164   equ 0e102h  ; 段控制口(接164数据位)
IN       equ 0e103h  ; 键盘读入口
LEDBuf   equ   40h   ; 显示缓冲
        ORG 0000h
	    SJMP MAIN
	    ORG 000BH		;定时器0中断入口地址
	    LJMP INTER0
	    org 0030h
MAIN:
	     mov sp,#60h
	     mov dptr,#0e100h     ;8155初始化
	     mov a,#03h
        movx @dptr,a
	     MOV TMOD,#51H 		;定时器初始化
	     MOV TH0,#3CH
	     MOV TL0,#0B7H
	     MOV TL1,#00H
	     MOV TH1,#00H
	     MOV 23H,#00H
	     ORL IE,#82H
	     MOV TCON,#50H
	     MOV 50H,#00H
	     MOV 51H,#00H
LOOP0:   MOV R2,50H
	     MOV R3,51H
	     LCALL LOOP1		;调用二进制转十进制子程序
	     MOV R0,#40H		;转换结果送显示缓冲器
	     MOV A,R6
	     LCALL PTDS
	     MOV A,R5
	     LCALL PTDS
	     MOV A,R4
	     LCALL PTDS
	     LCALL DISPLAY		;调用显示子程序
	     SJMP LOOP0
LOOP1:	  CLR A			;二进制转十进制子程序
	     MOV R4,A
	     MOV R5,A
	     MOV R6,A
	     MOV R7,#10H
LOOP2:	 CLR C
	    MOV A,R3
	    RLC A
	    MOV R3,A
	    MOV A,R2
	    RLC A
	    MOV R2,A
	    MOV A,R6
	    ADDC A,R6
	    DA A
	    MOV R6,A
	    MOV A,R5
	    ADDC A,R5
	    DA A
	    MOV R5,A
	    MOV A,R4
	    ADDC A,R4
	    DA A
	    MOV R4,A
	    DJNZ R7,LOOP2
	    RET
PTDS:	MOV R1,A
	   ACALL PTDS1
	   MOV A,R1
	   SWAP A
PTDS1:	ANL A,#0FH
	   MOV @R0,A
	   INC R0
	   RET
Delay:
        mov   r7, #0          ; 延时子程序
DelayLoop:
           djnz  r7, DelayLoop
           djnz  r6, DelayLoop
           ret
DISPLAY: setb 0d3h
         mov   r0, #LEDBuf
         mov   r1, #6          ; 共6个八段管
         mov   r2, #00000001b  ; 从左边开始显示
Loop:
        mov   dptr, #OUTBIT
        mov   a, #00h
        movx  @dptr, a        ; 关所有八段管
        mov   a, @r0
        mov dptr,#LEDmap
        movc a,@a+dptr
        mov   B, #8           ; 送164
DLP:
        rlc   a
        mov   r3, a
        mov   acc.0, c
        anl  a,#0fdh
        mov   dptr, #DAT164
        movx  @dptr, a
        mov   dptr, #CLK164
        orl a,#03h
        movx  @dptr, a
        anl a,#0fDh
        movx  @dptr, a
        mov   a, r3
        djnz  B, DLP
        mov   dptr, #OUTBIT
        mov   a, r2
        movx  @dptr, a        ; 显示一位八段管
        mov   r6, #1
        call  Delay
        mov   a, r2           ; 显示下一位
        rl    a
        mov   r2, a
        inc   r0
        djnz  r1, Loop
        mov   dptr, #OUTBIT
        mov   a, #0
        movx  @dptr, a
        clr 0d3h        ; 关所有八段管
        ret
LEDMAP:                       ; 八段管显示码
        db    3fh, 06h, 5bh, 4fh, 66h, 6dh, 7dh, 07h
        db    7fh, 6fh, 77h, 7ch, 39h, 5eh, 79h, 71h
INTER0:  CLR TR0			;定时器0中断处理子程序
	     MOV TL0,#0B7H
	     MOV TH0,#3CH
	     INC 23H
	     MOV A,23H
	     CJNE A,#0AH,ZOO1
	     MOV 23H,#00H
	     MOV 50H,TH1
	     MOV 51H,TL1
	     MOV TL1,#00H
	     MOV TH1,#00H
ZOO1:	 SETB TR0
	    RETI
	    END
