DAOT EQU 50H;中断次数计数单元
SCNT EQU 51H;为送到0832的值
CKCH EQU 52H;存放转速
CKCN EQU 53H
SETP EQU 54H
TEMP EQU 55H
cs   equ 90h 
times equ 46h
temp1  equ 47h 
OUTBIT   equ 0e101h  ; 位控制口
CLK164   equ 0e102h  ; 段控制口(接164时钟位)
DAT164   equ 0e102h  ; 段控制口(接164数据位)
IN       equ 0e103h  ; 键盘读入口
LEDBuf   equ   39h   ; 显示缓冲
       ORG 0000H
STRT:  LJMP MAIN
       ORG 0003H       ;
       LJMP PINT0
       ORG 000BH       ;
       LJMP PTF0
       ORG 0030H       ;
PTF0:  MOV TH0,#0D0H
       PUSH ACC
       PUSH PSW
       SETB PSW.3
       inc   times
       mov  a,times
       cjne  a,#40,ptfj
       mov   times,#0
       mov  a,ckcn
       mov  temp1,a
       mov  ckcn,#0
       subb  a,setp
       jnc   tt3
       mov   a,scnt
       add   a,#2  
       mov   scnt,a
       ljmp    tt4
tt3:   mov   a,scnt
       subb  a,#2
       mov   scnt,a 
tt4:
;       mov   a,scnt
;       mov   b,#100
;       div ab
;       mov  3eh,a
;       mov  a,b
;       mov  b,#10
;       div  ab
;       mov  3dh,a 
;       mov   a,b
;       mov   3ch,a 
       mov   a,temp1   ;speed
       mov   b,#10
       div   ab
       mov   3ah,a
       mov   a,b
       mov   39h,a
PTFJ:  NOP
       POP PSW
       POP ACC
       RETI
MAIN:  mov sp,#60h
       mov dptr,#0e100h     ;8155初始化
       mov a,#03h
       movx @dptr,a
       MOV R0,#39H
       MOV R7,#06H
MLP0:  MOV @R0,#10H
       INC R0
       DJNZ R7,MLP0
       LCALL DIR
                            ;调显示
       MOV DAOT,#40H
       MOV SCNT,#01H
       MOV CKCH,#00H
       MOV CKCN,#00H
       clr  EA
       NOP
       SETB EX1
       NOP
       CLR IT1
       NOP
MLP1:  call  TestKey          ; 有键入?
       jz    MLp1            ; 无键入, 继续显示
       call  GetKey           ; 读入键码
       ANL A,#0FH
       CJNE A,#0AH,tt
       JNC MLP1
tt:    MOV 3EH,A
       LCALL DIR
MLP2:  call  TestKey          ; 有键入?
       jz    MLP2            ; 无键入, 继续显示
       call  GetKey           ; 读入键码
       ANL A,#0FH
       CJNE A,#0AH,tt1
       JNC MLP2
 tt1:  MOV 3DH,A
       MOV A,3EH
       MOV B,#0AH
       MUL AB
       ADD A,3DH
       MOV SETP,A
       mov  dptr,#0a000h
       mov  scnt,#7fh
       mov  a,scnt
       MOVX @DPTR,A
       MOV A,#1
       ORL A,TMOD
       MOV TMOD,A
       MOV TH0,#0D0H
       MOV TL0,#00H
       mov  times,#0h
       SETB TR0
       SETB EA
       SETB ET0
       SETB EX0
       SETB IT0
       SETB EX1
       CLR IT1
       NOP
       MOV IP,#04H
       mov  scnt ,#7fh
ML00P4:
       MOV DPTR,#0a000H
       mov  a,scnt
       MOVX @DPTR,A
       mov  r7,#50
       djnz  r7,$
       LCALL DIR
       LJMP ML00P4
PINT0: INC CKCN
PIPI:  RETI
TestKey:
       mov   dptr, #OUTBIT
       mov   a, #0
       movx  @dptr, a        ; 输出线置为0
       mov   dptr, #IN
       movx  a, @dptr        ; 读入键状态
       cpl   a
       anl   a, #0fh         ; 高四位不用
       ret
KeyTable:                    ; 数字键码定义
         db    00h, 01h, 04h, 07h
         db    0fh, 02h, 05h, 08h
         db    0eh, 03h, 06h, 09h
         db    0dh, 0ch, 0bh, 0ah
         db    10H,10H,10H,10H,10H
         db    10H,10H,10H,10H,10H
GetKey:            MOV   13H,#06H
                   MOV   12H,#20H
Key2:	            MOV   A,12H
                   CPL   A
                   MOV   R7,A
                   MOV   DPTR,#0E101H
                   MOV   A,R7
                   MOVX  @DPTR,A
                   MOV   A,12H
                   CLR   C
                   RRC   A
                   MOV   12H,A
                   MOV   DPTR,#0E103H
                   MOVX  A,@DPTR
                   MOV   R7,A
                   MOV   A,R7
                   CPL   A
                   MOV   R7,A
                   MOV   A,R7
                   ANL   A,#0FH
                   MOV   14H,A
                   DEC   13H
                   MOV   R7,13H
                   MOV   A,R7
                   JZ    Key1
                   MOV   A,14H
                   JZ    Key2
Key1 :             MOV   A,14H
                   JZ   GetKey6
                   MOV   A,13H
                   ADD   A,ACC
                   ADD   A,ACC
                   MOV   13H,A
                   MOV   A,14H
                   JNB   ACC.1,GetKey1
                   INC   13H
                   SJMP  GetKey3
GetKey1:           MOV   A,14H
                   JNB   ACC.2,GetKey2
                   INC   13H
                   INC   13H
                   SJMP  GetKey3
GetKey2:          MOV   A,14H
                   JNB   ACC.3,GetKey3
                   MOV   A,#03H
                   ADD   A,13H
                   MOV   13H,A
GetKey3:           MOV   DPTR,#0E101H
                   CLR   A
                   MOVX  @DPTR,A
GetKey4:           MOV   R7,#0AH
                   LCALL Delay
                   LCALL TestKey
                   MOV   A,R7
                   JNZ   GetKey4
                   MOV   R7,13H
                   MOV   A,R7
                   MOV   DPTR,#KeyTable
                   MOVC  A,@A+DPTR
                   MOV   R2,A
                   RET
GetKey6:           MOV   R2,#0FFH
                   RET
WaitRelease:
              mov   dptr, #OUTBIT  ; 等键释放
              clr   a
              movx  @dptr, a
              mov   r6, #10
              call  Delay
              call  TestKey
              jnz   WaitRelease
              mov   a, r2
              ret
DIR:    setb 0d3h
        mov   r0, #LEDBuf
        mov   r1, #6          ; 共6个八段管
        mov   r2, #00000001b  ; 从左边开始显示
Loop:
        mov   dptr, #0e101h
        mov   a, #00h
        movx  @dptr, a        ; 关所有八段管
        mov   dptr,#0a000h
        mov   a,scnt
        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, #0e102h
        movx  @dptr, a
        mov   dptr, #0e102h
        orl a,#02h
        movx  @dptr, a
        anl a,#0fDh
        movx  @dptr, a
        mov   a, r3
        djnz  B, DLP
        mov   dptr, #0e101h
        mov   a, r2
        movx  @dptr, a        ; 显示一位八段管
        mov   r6, #1
        mov   dptr,#0a000h
        mov   a,scnt
        movx  @dptr,a
        call  Delay
        mov   a, r2           ; 显示下一位
        rL    a
        mov   r2, a
        inc   r0
        djnz  r1, Loop
        mov   dptr, #0e101h
        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
       DB    00H
Delay:               ; 延时子程序
       mov   r7, #0
DelayLoop:
       djnz  r7, DelayLoop
       djnz  r6, DelayLoop
       ret
	   END
